Search in sources :

Example 1 with OpenFileMessage

use of com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage in project BRFS by zhangnianli.

the class OpenFileMessageHandler method handleMessage.

@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
    OpenFileMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), OpenFileMessage.class);
    if (message == null) {
        writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
        return;
    }
    FileFormater fileFormater = new SimpleFileFormater(Math.min(message.getCapacity(), MAX_CAPACITY));
    String realPath = diskContext.getConcreteFilePath(message.getFilePath());
    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);
        writer.write(new BaseResponse(ResponseCode.ERROR));
        return;
    }
    try {
        binding.first().write(fileFormater.fileHeader().getBytes());
        binding.first().flush();
        BaseResponse response = new BaseResponse(ResponseCode.OK);
        response.setBody(Longs.toByteArray(fileFormater.maxBodyLength()));
        writer.write(response);
    } catch (Exception e) {
        LOG.error("write header to file[{}] error!", realPath);
        writer.write(new BaseResponse(ResponseCode.ERROR));
    }
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) OpenFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage) FileFormater(com.bonree.brfs.disknode.fileformat.FileFormater) SimpleFileFormater(com.bonree.brfs.disknode.fileformat.impl.SimpleFileFormater) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker) SimpleFileFormater(com.bonree.brfs.disknode.fileformat.impl.SimpleFileFormater)

Example 2 with OpenFileMessage

use of com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage in project BRFS by zhangnianli.

the class TcpDiskNodeClient method openFile.

@Override
public long openFile(String path, long capacity) {
    try {
        OpenFileMessage openFileMessage = new OpenFileMessage();
        openFileMessage.setFilePath(path);
        openFileMessage.setCapacity(capacity);
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_OPEN_FILE);
        message.setBody(ProtoStuffUtils.serialize(openFileMessage));
        CompletableFuture<BaseResponse> future = new CompletableFuture<BaseResponse>();
        client.sendMessage(message, new ResponseHandler<BaseResponse>() {

            @Override
            public void handle(BaseResponse response) {
                future.complete(response);
            }

            @Override
            public void error(Throwable e) {
                future.completeExceptionally(e);
            }
        });
        BaseResponse response = future.get();
        if (response != null && response.getCode() == ResponseCode.OK) {
            return Longs.fromByteArray(response.getBody());
        }
    } catch (Exception e) {
        LOG.error("open file error", e);
    }
    return -1;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) OpenFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage) IOException(java.io.IOException)

Aggregations

BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)2 OpenFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage)2 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)1 RecordFileWriter (com.bonree.brfs.disknode.data.write.RecordFileWriter)1 WriteWorker (com.bonree.brfs.disknode.data.write.worker.WriteWorker)1 FileFormater (com.bonree.brfs.disknode.fileformat.FileFormater)1 SimpleFileFormater (com.bonree.brfs.disknode.fileformat.impl.SimpleFileFormater)1 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1