Search in sources :

Example 6 with BaseMessage

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);
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) TcpDiskNodeClient(com.bonree.brfs.disknode.client.TcpDiskNodeClient) TcpClientConfig(com.bonree.brfs.common.net.tcp.client.TcpClientConfig) InetSocketAddress(java.net.InetSocketAddress) ReadObject(com.bonree.brfs.common.net.tcp.file.ReadObject) FileContentPart(com.bonree.brfs.common.net.tcp.file.client.FileContentPart) AsyncFileReaderCreateConfig(com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 7 with BaseMessage

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;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) TcpClientConfig(com.bonree.brfs.common.net.tcp.client.TcpClientConfig) InetSocketAddress(java.net.InetSocketAddress) Service(com.bonree.brfs.common.service.Service) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 8 with BaseMessage

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;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) IOException(java.io.IOException)

Example 9 with BaseMessage

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;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) IOException(java.io.IOException)

Example 10 with BaseMessage

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;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) CompletableFuture(java.util.concurrent.CompletableFuture) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) IOException(java.io.IOException)

Aggregations

BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)14 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)13 IOException (java.io.IOException)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 TcpClientConfig (com.bonree.brfs.common.net.tcp.client.TcpClientConfig)2 WriteFileData (com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileData)2 WriteFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 TokenMessage (com.bonree.brfs.common.net.tcp.TokenMessage)1 ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)1 AsyncFileReaderCreateConfig (com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig)1 FileContentPart (com.bonree.brfs.common.net.tcp.file.client.FileContentPart)1 Service (com.bonree.brfs.common.service.Service)1 TcpDiskNodeClient (com.bonree.brfs.disknode.client.TcpDiskNodeClient)1 DeleteFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.DeleteFileMessage)1 FileRecoveryMessage (com.bonree.brfs.disknode.server.tcp.handler.data.FileRecoveryMessage)1 ListFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage)1 OpenFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage)1 Bootstrap (io.netty.bootstrap.Bootstrap)1