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));
}
}
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;
}
Aggregations