use of com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage in project BRFS by zhangnianli.
the class WriteFileMessageHandler method handleMessage.
@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
WriteFileMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), WriteFileMessage.class);
if (message == null) {
writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
return;
}
try {
String realPath = diskContext.getConcreteFilePath(message.getFilePath());
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);
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
binding.second().put(new DataWriteTask(binding, message, writer));
} catch (Exception e) {
LOG.error("EEEERRRRRR", e);
writer.write(new BaseResponse(ResponseCode.ERROR));
}
}
use of com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage 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;
}
use of com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage 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;
}
Aggregations