use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class TcpClientUtils method getClient.
public static TcpDiskNodeClient getClient(String host, int port, int export, int timeout) throws InterruptedException, IOException {
TcpClient<BaseMessage, BaseResponse> tcpClient = group.createClient(new TcpClientConfig() {
@Override
public SocketAddress remoteAddress() {
return new InetSocketAddress(host, port);
}
@Override
public int connectTimeoutMillis() {
return timeout;
}
});
TcpClient<ReadObject, FileContentPart> readerClient = group2.createClient(new AsyncFileReaderCreateConfig() {
@Override
public SocketAddress remoteAddress() {
return new InetSocketAddress(host, export);
}
@Override
public int connectTimeoutMillis() {
return timeout;
}
@Override
public int maxPendingRead() {
return 0;
}
});
return new TcpDiskNodeClient(tcpClient, readerClient);
}
use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class MetadataFetchMessageHandler 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 = context.getConcreteFilePath(path);
LOG.info("GET metadata of file[{}]", filePath);
List<RecordElement> recordInfo = getRecordElements(filePath);
if (recordInfo == null) {
LOG.info("can not get record elements of file[{}]", filePath);
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
// 获取所有文件序列号
RecordElement lastElement = null;
if (recordInfo != null) {
for (RecordElement element : recordInfo) {
if (lastElement == null) {
lastElement = element;
continue;
}
if (lastElement.getOffset() < element.getOffset()) {
lastElement = element;
}
}
}
if (lastElement == null) {
LOG.info("no available record element of file[{}]", filePath);
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
long fileLength = fileFormater.relativeOffset(lastElement.getOffset()) + lastElement.getSize();
LOG.info("get file length[{}] from file[{}]", fileLength, filePath);
BaseResponse response = new BaseResponse(ResponseCode.OK);
response.setBody(Longs.toByteArray(fileLength));
writer.write(response);
}
use of com.bonree.brfs.common.net.tcp.BaseResponse 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));
}
}
use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class ListFileMessageHandler method handleMessage.
@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
ListFileMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), ListFileMessage.class);
if (message == null) {
writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
return;
}
String dirPath = null;
try {
dirPath = context.getConcreteFilePath(message.getPath());
File dir = new File(dirPath);
if (!dir.exists()) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
if (!dir.isDirectory()) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
FileInfo dirInfo = new FileInfo();
dirInfo.setLevel(0);
dirInfo.setType(FileInfo.TYPE_DIR);
dirInfo.setPath(dirPath);
fileList.addLast(dirInfo);
ArrayList<FileInfo> fileInfoList = new ArrayList<FileInfo>();
traverse(message.getLevel(), fileInfoList);
BaseResponse response = new BaseResponse(ResponseCode.OK);
response.setBody(JsonUtils.toJsonBytes(fileInfoList));
writer.write(response);
} catch (Exception e) {
LOG.error("list dir[{}] error", dirPath, e);
writer.write(new BaseResponse(ResponseCode.ERROR));
}
}
use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class TcpDiskNodeConnectionPool method getConnection.
@Override
public DiskNodeConnection getConnection(String serviceGroup, String serviceId) {
TcpDiskNodeConnection connection = connectionCache.get(serviceGroup, serviceId);
if (connection != null) {
if (connection.isValid()) {
return connection;
}
synchronized (connectionCache) {
connectionCache.remove(serviceGroup, serviceId);
}
}
try {
synchronized (connectionCache) {
Service service = serviceManager.getServiceById(serviceGroup, serviceId);
if (service == null) {
return null;
}
TcpClient<BaseMessage, BaseResponse> client = tcpClientGroup.createClient(new TcpClientConfig() {
@Override
public SocketAddress remoteAddress() {
return new InetSocketAddress(service.getHost(), service.getPort());
}
@Override
public int connectTimeoutMillis() {
return 3000;
}
}, executor);
if (client == null) {
return null;
}
connection = new TcpDiskNodeConnection(client);
connectionCache.put(serviceGroup, serviceId, connection);
}
return connection;
} catch (Exception e) {
LOG.error("connect tcp connection to disk node error", e);
}
return null;
}
Aggregations