Search in sources :

Example 6 with RecordFileWriter

use of com.bonree.brfs.disknode.data.write.RecordFileWriter in project BRFS by zhangnianli.

the class OpenMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    HandleResult result = new HandleResult();
    String realPath = null;
    try {
        String capacityParam = msg.getParams().get("capacity");
        if (capacityParam == null) {
            result.setSuccess(false);
            return;
        }
        long capacity = Long.parseLong(capacityParam);
        FileFormater fileFormater = new SimpleFileFormater(Math.min(capacity, MAX_CAPACITY));
        realPath = diskContext.getConcreteFilePath(msg.getPath());
        LOG.info("open file [{}]", realPath);
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(realPath, true);
        if (binding == null) {
            LOG.error("get file writer for file[{}] error!", realPath);
            result.setSuccess(false);
            return;
        }
        binding.first().write(fileFormater.fileHeader().getBytes());
        binding.first().flush();
        result.setData(Longs.toByteArray(fileFormater.maxBodyLength()));
        result.setSuccess(true);
    } catch (Exception e) {
        LOG.error("write header to file[{}] error!", realPath);
        result.setSuccess(false);
    } finally {
        callback.completed(result);
    }
}
Also used : FileFormater(com.bonree.brfs.disknode.fileformat.FileFormater) SimpleFileFormater(com.bonree.brfs.disknode.fileformat.impl.SimpleFileFormater) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) HandleResult(com.bonree.brfs.common.net.http.HandleResult) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker) SimpleFileFormater(com.bonree.brfs.disknode.fileformat.impl.SimpleFileFormater)

Example 7 with RecordFileWriter

use of com.bonree.brfs.disknode.data.write.RecordFileWriter in project BRFS by zhangnianli.

the class MetadataFetchMessageHandler method getRecordElements.

private List<RecordElement> getRecordElements(String filePath) {
    List<RecordElement> recordInfo = null;
    Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
    if (binding != null) {
        RecordElementReader recordReader = null;
        try {
            binding.first().flush();
            writerManager.adjustFileWriter(filePath);
            RecordCollection recordSet = binding.first().getRecordCollection();
            recordReader = recordSet.getRecordElementReader();
            recordInfo = new ArrayList<RecordElement>();
            for (RecordElement element : recordReader) {
                recordInfo.add(element);
            }
        } catch (Exception e) {
            LOG.error("getSequnceNumbers from file[{}] error", filePath, e);
        } finally {
            CloseUtils.closeQuietly(recordReader);
        }
    } else {
        // 到这有两种情况:
        // 1、文件打开操作未成功后进行同步;
        // 2、文件关闭成功后进行文件同步;
        recordInfo = new ArrayList<RecordElement>();
        File dataFile = new File(filePath);
        if (dataFile.exists()) {
            // 到这的唯一机会是,多副本文件关闭时只有部分关闭成功,当磁盘节点恢复正常
            // 后,需要再次进行同步流程让所有副本文件关闭,因为没有日志文件,所以只能
            // 通过解析数据文件生成序列号列表
            byte[] bytes = DataFileReader.readFile(dataFile);
            List<String> offsetInfos = FileDecoder.getDataFileOffsets(fileFormater.fileHeader().length(), bytes);
            for (String info : offsetInfos) {
                List<String> parts = Splitter.on('|').splitToList(info);
                int offset = Integer.parseInt(parts.get(0));
                int size = Integer.parseInt(parts.get(1));
                recordInfo.add(new RecordElement(offset, size, 0));
            }
        }
    }
    return recordInfo;
}
Also used : RecordElementReader(com.bonree.brfs.disknode.data.write.record.RecordElementReader) RecordElement(com.bonree.brfs.disknode.data.write.record.RecordElement) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) RecordCollection(com.bonree.brfs.disknode.data.write.record.RecordCollection) File(java.io.File) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker)

Example 8 with RecordFileWriter

use of com.bonree.brfs.disknode.data.write.RecordFileWriter in project BRFS by zhangnianli.

the class FileLengthMessageHandler method getRecordElements.

private List<RecordElement> getRecordElements(String filePath) {
    List<RecordElement> recordInfo = null;
    try {
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
        if (binding != null) {
            RecordElementReader recordReader = null;
            try {
                binding.first().flush();
                writerManager.adjustFileWriter(filePath);
                RecordCollection recordSet = binding.first().getRecordCollection();
                recordReader = recordSet.getRecordElementReader();
                recordInfo = new ArrayList<RecordElement>();
                for (RecordElement element : recordReader) {
                    recordInfo.add(element);
                }
            } catch (Exception e) {
                LOG.error("getSequnceNumbers from file[{}] error", filePath, e);
            } finally {
                CloseUtils.closeQuietly(recordReader);
            }
        } else {
            // 到这有两种情况:
            // 1、文件打开操作未成功后进行同步;
            // 2、文件关闭操作未成功进行再次关闭;
            recordInfo = new ArrayList<RecordElement>();
            File dataFile = new File(filePath);
            if (dataFile.exists()) {
                // 到这的唯一机会是,多副本文件关闭时只有部分关闭成功,当磁盘节点恢复正常
                // 后,需要再次进行同步流程让所有副本文件关闭,因为没有日志文件,所以只能
                // 通过解析数据文件生成序列号列表
                byte[] bytes = DataFileReader.readFile(dataFile);
                List<String> offsetInfos = FileDecoder.getDataFileOffsets(fileFormater.fileHeader().length(), bytes);
                for (String info : offsetInfos) {
                    List<String> parts = Splitter.on('|').splitToList(info);
                    int offset = Integer.parseInt(parts.get(0));
                    int size = Integer.parseInt(parts.get(1));
                    recordInfo.add(new RecordElement(offset, size, 0));
                }
            }
        }
    } catch (Exception e) {
        LOG.error("get record element error", e);
    }
    return recordInfo;
}
Also used : RecordElementReader(com.bonree.brfs.disknode.data.write.record.RecordElementReader) RecordElement(com.bonree.brfs.disknode.data.write.record.RecordElement) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) RecordCollection(com.bonree.brfs.disknode.data.write.record.RecordCollection) File(java.io.File) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker)

Example 9 with RecordFileWriter

use of com.bonree.brfs.disknode.data.write.RecordFileWriter in project BRFS by zhangnianli.

the class WriteMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    try {
        String realPath = diskContext.getConcreteFilePath(msg.getPath());
        LOG.debug("writing to file [{}]", realPath);
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(realPath, false);
        if (binding == null) {
            // 运行到这,可能时打开文件时失败,导致写数据节点找不到writer
            LOG.warn("no file writer is found, maybe the file[{}] is not opened.", realPath);
            callback.completed(new HandleResult(false));
            return;
        }
        binding.second().put(new DataWriteTask(binding, msg, callback));
    } catch (Exception e) {
        LOG.error("EEEERRRRRR", e);
        HandleResult handleResult = new HandleResult();
        handleResult.setSuccess(false);
        handleResult.setCause(e);
        callback.completed(handleResult);
    }
}
Also used : RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) HandleResult(com.bonree.brfs.common.net.http.HandleResult) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker) IOException(java.io.IOException)

Example 10 with RecordFileWriter

use of com.bonree.brfs.disknode.data.write.RecordFileWriter in project BRFS by zhangnianli.

the class CloseFileMessageHandler method handleMessage.

@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
    String path = BrStringUtils.fromUtf8Bytes(baseMessage.getBody());
    if (path == null) {
        writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
        return;
    }
    try {
        final String filePath = diskContext.getConcreteFilePath(path);
        LOG.info("CLOSE file[{}]", filePath);
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
        if (binding == null) {
            LOG.info("no writer is found for file[{}], treat it as OK!", filePath);
            File dataFile = new File(filePath);
            if (!dataFile.exists()) {
                writer.write(new BaseResponse(ResponseCode.ERROR));
                return;
            }
            MappedByteBuffer buffer = Files.map(dataFile);
            try {
                buffer.position(fileFormater.fileHeader().length());
                buffer.limit(buffer.capacity() - fileFormater.fileTailer().length());
                BaseResponse response = new BaseResponse(ResponseCode.OK);
                response.setBody(Longs.toByteArray(ByteUtils.crc(buffer)));
                BufferUtils.release(buffer);
                writer.write(response);
                return;
            } finally {
                BufferUtils.release(buffer);
            }
        }
        binding.second().put(new WriteTask<Long>() {

            @Override
            protected Long execute() throws Exception {
                LOG.info("start writing file tailer for {}", filePath);
                binding.first().flush();
                byte[] fileBytes = DataFileReader.readFile(filePath, fileFormater.fileHeader().length());
                long crcCode = ByteUtils.crc(fileBytes);
                LOG.info("final crc code[{}] by bytes[{}] of file[{}]", crcCode, fileBytes.length, filePath);
                byte[] tailer = Bytes.concat(FileEncoder.validate(crcCode), FileEncoder.tail());
                binding.first().write(tailer);
                binding.first().flush();
                LOG.info("close over for file[{}]", filePath);
                writerManager.close(filePath);
                return crcCode;
            }

            @Override
            protected void onPostExecute(Long result) {
                BaseResponse response = new BaseResponse(ResponseCode.OK);
                response.setBody(Longs.toByteArray(result));
                writer.write(response);
            }

            @Override
            protected void onFailed(Throwable e) {
                BaseResponse response = new BaseResponse(ResponseCode.ERROR);
                writer.write(response);
            }
        });
    } catch (IOException e) {
        LOG.error("close file[{}] error!", path);
        writer.write(new BaseResponse(ResponseCode.ERROR));
    }
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) MappedByteBuffer(java.nio.MappedByteBuffer) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) File(java.io.File) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker)

Aggregations

RecordFileWriter (com.bonree.brfs.disknode.data.write.RecordFileWriter)10 WriteWorker (com.bonree.brfs.disknode.data.write.worker.WriteWorker)10 HandleResult (com.bonree.brfs.common.net.http.HandleResult)4 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)4 File (java.io.File)4 IOException (java.io.IOException)4 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 RecordElement (com.bonree.brfs.disknode.data.write.record.RecordElement)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