use of com.bonree.brfs.disknode.data.write.worker.WriteWorker in project BRFS by zhangnianli.
the class OpenMessageHandler method handle.
@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
HandleResult result = new HandleResult();
String realPath = null;
try {
String capacityParam = msg.getParams().get("capacity");
if (capacityParam == null) {
result.setSuccess(false);
return;
}
long capacity = Long.parseLong(capacityParam);
FileFormater fileFormater = new SimpleFileFormater(Math.min(capacity, MAX_CAPACITY));
realPath = diskContext.getConcreteFilePath(msg.getPath());
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);
result.setSuccess(false);
return;
}
binding.first().write(fileFormater.fileHeader().getBytes());
binding.first().flush();
result.setData(Longs.toByteArray(fileFormater.maxBodyLength()));
result.setSuccess(true);
} catch (Exception e) {
LOG.error("write header to file[{}] error!", realPath);
result.setSuccess(false);
} finally {
callback.completed(result);
}
}
use of com.bonree.brfs.disknode.data.write.worker.WriteWorker in project BRFS by zhangnianli.
the class MetadataFetchMessageHandler method getRecordElements.
private List<RecordElement> getRecordElements(String filePath) {
List<RecordElement> recordInfo = null;
Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
if (binding != null) {
RecordElementReader recordReader = null;
try {
binding.first().flush();
writerManager.adjustFileWriter(filePath);
RecordCollection recordSet = binding.first().getRecordCollection();
recordReader = recordSet.getRecordElementReader();
recordInfo = new ArrayList<RecordElement>();
for (RecordElement element : recordReader) {
recordInfo.add(element);
}
} catch (Exception e) {
LOG.error("getSequnceNumbers from file[{}] error", filePath, e);
} finally {
CloseUtils.closeQuietly(recordReader);
}
} else {
// 到这有两种情况:
// 1、文件打开操作未成功后进行同步;
// 2、文件关闭成功后进行文件同步;
recordInfo = new ArrayList<RecordElement>();
File dataFile = new File(filePath);
if (dataFile.exists()) {
// 到这的唯一机会是,多副本文件关闭时只有部分关闭成功,当磁盘节点恢复正常
// 后,需要再次进行同步流程让所有副本文件关闭,因为没有日志文件,所以只能
// 通过解析数据文件生成序列号列表
byte[] bytes = DataFileReader.readFile(dataFile);
List<String> offsetInfos = FileDecoder.getDataFileOffsets(fileFormater.fileHeader().length(), bytes);
for (String info : offsetInfos) {
List<String> parts = Splitter.on('|').splitToList(info);
int offset = Integer.parseInt(parts.get(0));
int size = Integer.parseInt(parts.get(1));
recordInfo.add(new RecordElement(offset, size, 0));
}
}
}
return recordInfo;
}
use of com.bonree.brfs.disknode.data.write.worker.WriteWorker in project BRFS by zhangnianli.
the class FileLengthMessageHandler method getRecordElements.
private List<RecordElement> getRecordElements(String filePath) {
List<RecordElement> recordInfo = null;
try {
Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
if (binding != null) {
RecordElementReader recordReader = null;
try {
binding.first().flush();
writerManager.adjustFileWriter(filePath);
RecordCollection recordSet = binding.first().getRecordCollection();
recordReader = recordSet.getRecordElementReader();
recordInfo = new ArrayList<RecordElement>();
for (RecordElement element : recordReader) {
recordInfo.add(element);
}
} catch (Exception e) {
LOG.error("getSequnceNumbers from file[{}] error", filePath, e);
} finally {
CloseUtils.closeQuietly(recordReader);
}
} else {
// 到这有两种情况:
// 1、文件打开操作未成功后进行同步;
// 2、文件关闭操作未成功进行再次关闭;
recordInfo = new ArrayList<RecordElement>();
File dataFile = new File(filePath);
if (dataFile.exists()) {
// 到这的唯一机会是,多副本文件关闭时只有部分关闭成功,当磁盘节点恢复正常
// 后,需要再次进行同步流程让所有副本文件关闭,因为没有日志文件,所以只能
// 通过解析数据文件生成序列号列表
byte[] bytes = DataFileReader.readFile(dataFile);
List<String> offsetInfos = FileDecoder.getDataFileOffsets(fileFormater.fileHeader().length(), bytes);
for (String info : offsetInfos) {
List<String> parts = Splitter.on('|').splitToList(info);
int offset = Integer.parseInt(parts.get(0));
int size = Integer.parseInt(parts.get(1));
recordInfo.add(new RecordElement(offset, size, 0));
}
}
}
} catch (Exception e) {
LOG.error("get record element error", e);
}
return recordInfo;
}
use of com.bonree.brfs.disknode.data.write.worker.WriteWorker in project BRFS by zhangnianli.
the class WriteMessageHandler method handle.
@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
try {
String realPath = diskContext.getConcreteFilePath(msg.getPath());
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);
callback.completed(new HandleResult(false));
return;
}
binding.second().put(new DataWriteTask(binding, msg, callback));
} catch (Exception e) {
LOG.error("EEEERRRRRR", e);
HandleResult handleResult = new HandleResult();
handleResult.setSuccess(false);
handleResult.setCause(e);
callback.completed(handleResult);
}
}
use of com.bonree.brfs.disknode.data.write.worker.WriteWorker in project BRFS by zhangnianli.
the class CloseFileMessageHandler 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;
}
try {
final String filePath = diskContext.getConcreteFilePath(path);
LOG.info("CLOSE file[{}]", filePath);
Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
if (binding == null) {
LOG.info("no writer is found for file[{}], treat it as OK!", filePath);
File dataFile = new File(filePath);
if (!dataFile.exists()) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
MappedByteBuffer buffer = Files.map(dataFile);
try {
buffer.position(fileFormater.fileHeader().length());
buffer.limit(buffer.capacity() - fileFormater.fileTailer().length());
BaseResponse response = new BaseResponse(ResponseCode.OK);
response.setBody(Longs.toByteArray(ByteUtils.crc(buffer)));
BufferUtils.release(buffer);
writer.write(response);
return;
} finally {
BufferUtils.release(buffer);
}
}
binding.second().put(new WriteTask<Long>() {
@Override
protected Long execute() throws Exception {
LOG.info("start writing file tailer for {}", filePath);
binding.first().flush();
byte[] fileBytes = DataFileReader.readFile(filePath, fileFormater.fileHeader().length());
long crcCode = ByteUtils.crc(fileBytes);
LOG.info("final crc code[{}] by bytes[{}] of file[{}]", crcCode, fileBytes.length, filePath);
byte[] tailer = Bytes.concat(FileEncoder.validate(crcCode), FileEncoder.tail());
binding.first().write(tailer);
binding.first().flush();
LOG.info("close over for file[{}]", filePath);
writerManager.close(filePath);
return crcCode;
}
@Override
protected void onPostExecute(Long result) {
BaseResponse response = new BaseResponse(ResponseCode.OK);
response.setBody(Longs.toByteArray(result));
writer.write(response);
}
@Override
protected void onFailed(Throwable e) {
BaseResponse response = new BaseResponse(ResponseCode.ERROR);
writer.write(response);
}
});
} catch (IOException e) {
LOG.error("close file[{}] error!", path);
writer.write(new BaseResponse(ResponseCode.ERROR));
}
}
Aggregations