Search in sources :

Example 16 with BaseResponse

use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.

the class TcpDiskNodeClient method closeFile.

@Override
public long closeFile(String path) {
    try {
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_CLOSE_FILE);
        message.setBody(BrStringUtils.toUtf8Bytes(path));
        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("close 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) IOException(java.io.IOException)

Example 17 with BaseResponse

use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.

the class TcpDiskNodeClient method flush.

@Override
public boolean flush(String file) throws IOException {
    try {
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_FLUSH_FILE);
        message.setBody(BrStringUtils.toUtf8Bytes(file));
        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 true;
        }
    } catch (Exception e) {
        LOG.error("flush file error", e);
    }
    return false;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) IOException(java.io.IOException)

Example 18 with BaseResponse

use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.

the class TcpDiskNodeClient method getFileLength.

@Override
public long getFileLength(String path) {
    try {
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_METADATA);
        message.setBody(BrStringUtils.toUtf8Bytes(path));
        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("get meta data from file [{}] error", path, 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) IOException(java.io.IOException)

Example 19 with BaseResponse

use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.

the class TcpDiskNodeClient method writeData.

@Override
public WriteResult writeData(String path, byte[] bytes) throws IOException {
    try {
        WriteFileData data = new WriteFileData();
        data.setData(bytes);
        WriteFileMessage writeFileMessage = new WriteFileMessage();
        writeFileMessage.setFilePath(path);
        writeFileMessage.setDatas(new WriteFileData[] { data });
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_WRITE_FILE);
        message.setBody(ProtoStuffUtils.serialize(writeFileMessage));
        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) {
            WriteResultList resultList = ProtoStuffUtils.deserialize(response.getBody(), WriteResultList.class);
            WriteResult[] results = resultList.getWriteResults();
            if (results.length == 1) {
                return results[0];
            }
        }
    } catch (Exception e) {
        LOG.error("write file error", e);
    }
    return null;
}
Also used : IOException(java.io.IOException) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) WriteFileData(com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileData) WriteFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage)

Example 20 with BaseResponse

use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.

the class TcpDiskNodeClient method writeDatas.

@Override
public WriteResult[] writeDatas(String path, List<byte[]> dataList) throws IOException {
    try {
        WriteFileData[] datas = new WriteFileData[dataList.size()];
        for (int i = 0; i < datas.length; i++) {
            WriteFileData data = new WriteFileData();
            data.setData(dataList.get(i));
            datas[i] = data;
        }
        WriteFileMessage writeFileMessage = new WriteFileMessage();
        writeFileMessage.setFilePath(path);
        writeFileMessage.setDatas(datas);
        BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_WRITE_FILE);
        message.setBody(ProtoStuffUtils.serialize(writeFileMessage));
        CompletableFuture<BaseResponse> future = new CompletableFuture<BaseResponse>();
        LOG.debug("write [{}] datas to data node in file[{}]", dataList.size(), path);
        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) {
            WriteResultList resultList = ProtoStuffUtils.deserialize(response.getBody(), WriteResultList.class);
            return resultList.getWriteResults();
        }
    } catch (Exception e) {
        LOG.error("write file error", e);
    }
    return null;
}
Also used : IOException(java.io.IOException) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) WriteFileData(com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileData) WriteFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage)

Aggregations

BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)22 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)13 IOException (java.io.IOException)13 CompletableFuture (java.util.concurrent.CompletableFuture)11 RecordFileWriter (com.bonree.brfs.disknode.data.write.RecordFileWriter)4 WriteWorker (com.bonree.brfs.disknode.data.write.worker.WriteWorker)4 WriteFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage)3 File (java.io.File)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 TcpClientConfig (com.bonree.brfs.common.net.tcp.client.TcpClientConfig)2 ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)2 AsyncFileReaderCreateConfig (com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig)2 FileContentPart (com.bonree.brfs.common.net.tcp.file.client.FileContentPart)2 Service (com.bonree.brfs.common.service.Service)2 TcpDiskNodeClient (com.bonree.brfs.disknode.client.TcpDiskNodeClient)2 DeleteFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.DeleteFileMessage)2 FileRecoveryMessage (com.bonree.brfs.disknode.server.tcp.handler.data.FileRecoveryMessage)2 ListFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage)2 OpenFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage)2