use of com.bonree.brfs.common.net.tcp.BaseMessage 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.BaseMessage 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;
}
use of com.bonree.brfs.common.net.tcp.BaseMessage 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.BaseMessage 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.BaseMessage 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;
}
Aggregations