Search in sources :

Example 11 with WriteWorker

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;
}
Also used : ByteArrayFileBuffer(com.bonree.brfs.disknode.data.write.buf.ByteArrayFileBuffer) File(java.io.File) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 12 with WriteWorker

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();
    }
}
Also used : RecordElement(com.bonree.brfs.disknode.data.write.record.RecordElement) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker)

Aggregations

WriteWorker (com.bonree.brfs.disknode.data.write.worker.WriteWorker)12 RecordFileWriter (com.bonree.brfs.disknode.data.write.RecordFileWriter)10 File (java.io.File)5 IOException (java.io.IOException)5 HandleResult (com.bonree.brfs.common.net.http.HandleResult)4 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)4 RecordElement (com.bonree.brfs.disknode.data.write.record.RecordElement)3 FileObjectSyncState (com.bonree.brfs.common.filesync.FileObjectSyncState)2 Service (com.bonree.brfs.common.service.Service)2 DiskNodeClient (com.bonree.brfs.disknode.client.DiskNodeClient)2 ByteConsumer (com.bonree.brfs.disknode.client.DiskNodeClient.ByteConsumer)2 RecordCollection (com.bonree.brfs.disknode.data.write.record.RecordCollection)2 RecordElementReader (com.bonree.brfs.disknode.data.write.record.RecordElementReader)2 FileFormater (com.bonree.brfs.disknode.fileformat.FileFormater)2 SimpleFileFormater (com.bonree.brfs.disknode.fileformat.impl.SimpleFileFormater)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)1 AsyncFileReaderCreateConfig (com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig)1