use of com.bonree.brfs.disknode.data.write.worker.WriteWorker in project BRFS by zhangnianli.
the class FileWriterManager method buildDiskWriter.
private Pair<RecordFileWriter, WriteWorker> buildDiskWriter(String filePath) {
Pair<RecordFileWriter, WriteWorker> binding = runningWriters.get(filePath);
if (binding == null) {
synchronized (runningWriters) {
binding = runningWriters.get(filePath);
if (binding == null) {
try {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
RecordFileWriter writer = new RecordFileWriter(recorderManager.getRecordCollection(filePath, false, recordCacheSize, true), new BufferedFileWriter(filePath, new ByteArrayFileBuffer(dataCacheSize)));
binding = new Pair<RecordFileWriter, WriteWorker>(writer, workerSelector.select(workerGroup.getWorkerList()));
runningWriters.put(filePath, binding);
} catch (Exception e) {
LOG.error("build disk writer error", e);
}
}
}
}
return binding;
}
use of com.bonree.brfs.disknode.data.write.worker.WriteWorker in project BRFS by zhangnianli.
the class FileWriterManager method adjustFileWriter.
public void adjustFileWriter(String filePath) throws IOException {
Pair<RecordFileWriter, WriteWorker> binding = runningWriters.get(filePath);
if (binding == null) {
throw new IllegalStateException("no writer of " + filePath + " is found for adjust");
}
List<RecordElement> originElements = binding.first().getRecordCollection().getRecordElementList();
LOG.info("adjust get [{}] records from record collection of [{}]", originElements.size(), filePath);
if (originElements.isEmpty()) {
// 没有数据写入成功,不需要任何协调
return;
}
List<RecordElement> elements = validElements(filePath, originElements);
LOG.info("adjust file get elements size[{}] for file[{}]", elements.size(), filePath);
RecordElement lastElement = elements.get(elements.size() - 1);
long validPosition = lastElement.getOffset() + lastElement.getSize();
LOG.debug("last element : {}", lastElement);
boolean needFlush = false;
if (validPosition != binding.first().position()) {
LOG.info("rewrite file content of file[{}] from[{}] to [{}]", filePath, binding.first().position(), validPosition);
// 数据文件的内容和日志信息不一致,需要调整数据文件
binding.first().position(validPosition);
needFlush = true;
}
if (elements.size() != originElements.size()) {
LOG.info("rewrite file records of file[{}]", filePath);
binding.first().getRecordCollection().clear();
for (RecordElement element : elements) {
binding.first().getRecordCollection().put(element);
}
needFlush = true;
}
if (needFlush) {
binding.first().flush();
}
}
Aggregations