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