use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class FileRecoveryMessageHandler method handleMessage.
@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
FileRecoveryMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), FileRecoveryMessage.class);
if (message == null) {
LOG.error("decode recover message error");
writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
return;
}
String filePath = null;
try {
filePath = context.getConcreteFilePath(message.getFilePath());
LOG.info("starting recover file[{}]", filePath);
Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
if (binding == null) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
binding.first().position(fileFormater.absoluteOffset(message.getOffset()));
byte[] bytes = null;
for (String stateString : message.getSources()) {
FileObjectSyncState state = SyncStateCodec.fromString(stateString);
Service service = serviceManager.getServiceById(state.getServiceGroup(), state.getServiceId());
if (service == null) {
LOG.error("can not get service with[{}:{}]", state.getServiceGroup(), state.getServiceId());
continue;
}
DiskNodeClient client = null;
try {
LOG.info("get data from{} to recover...", service);
TcpClient<ReadObject, FileContentPart> readClient = clientGroup.createClient(new AsyncFileReaderCreateConfig() {
@Override
public SocketAddress remoteAddress() {
return new InetSocketAddress(service.getHost(), service.getExtraPort());
}
@Override
public int connectTimeoutMillis() {
return 3000;
}
@Override
public int maxPendingRead() {
return 0;
}
}, ForkJoinPool.commonPool());
client = new TcpDiskNodeClient(null, readClient);
long lackBytes = state.getFileLength() - message.getOffset();
CompletableFuture<byte[]> byteFuture = new CompletableFuture<byte[]>();
ByteArrayOutputStream output = new ByteArrayOutputStream();
client.readData(state.getFilePath(), message.getOffset(), (int) lackBytes, new ByteConsumer() {
@Override
public void error(Throwable e) {
byteFuture.completeExceptionally(e);
}
@Override
public void consume(byte[] bytes, boolean endOfConsume) {
try {
output.write(bytes);
if (endOfConsume) {
byteFuture.complete(output.toByteArray());
output.close();
}
} catch (Exception e) {
byteFuture.completeExceptionally(e);
}
}
});
bytes = byteFuture.get();
if (bytes != null) {
LOG.info("read bytes length[{}], require[{}]", bytes.length, lackBytes);
break;
}
} catch (Exception e) {
LOG.error("recover file[{}] error", filePath, e);
} finally {
CloseUtils.closeQuietly(client);
}
}
if (bytes == null) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
int offset = 0;
int size = 0;
while ((size = FileDecoder.getOffsets(offset, bytes)) > 0) {
LOG.info("rewrite data[offset={}, size={}] to file[{}]", offset, size, filePath);
binding.first().write(bytes, offset, size);
offset += size;
size = 0;
}
if (offset != bytes.length) {
LOG.error("perhaps datas that being recoverd is not correct! get [{}], but recoverd[{}]", bytes.length, offset);
}
writer.write(new BaseResponse(ResponseCode.OK));
} catch (Exception e) {
LOG.error("recover file[{}] error", filePath, e);
writer.write(new BaseResponse(ResponseCode.ERROR));
}
}
use of com.bonree.brfs.common.net.tcp.BaseResponse 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.common.net.tcp.BaseResponse 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.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class DeleteFileMessageHandler method handleMessage.
@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
DeleteFileMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), DeleteFileMessage.class);
if (message == null) {
writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
return;
}
try {
String filePath = diskContext.getConcreteFilePath(message.getFilePath());
LOG.info("delete file[{}], force[{}], recursive[{}]", filePath, message.isForce(), message.isRecursive());
File targetFile = new File(filePath);
if (targetFile.isFile()) {
try {
closeFile(targetFile, message.isForce());
writer.write(new BaseResponse(ResponseCode.OK));
} catch (Exception e) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
} else {
try {
closeDir(targetFile, message.isRecursive(), message.isForce());
writer.write(new BaseResponse(ResponseCode.OK));
} catch (Exception e) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
}
} catch (Exception e) {
LOG.error("delete message error", e);
writer.write(new BaseResponse(ResponseCode.ERROR));
}
}
use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class FlushFileMessageHandler method handleMessage.
@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
String path = BrStringUtils.fromUtf8Bytes(baseMessage.getBody());
if (path == null) {
writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
return;
}
String filePath = null;
try {
filePath = diskContext.getConcreteFilePath(path);
LOG.info("flush file[{}]", filePath);
writerManager.flushFile(filePath);
writer.write(new BaseResponse(ResponseCode.OK));
} catch (FileNotFoundException e) {
writer.write(new BaseResponse(ResponseCode.ERROR));
}
}
Aggregations