Search in sources :

Example 6 with ReadObject

use of com.bonree.brfs.common.net.tcp.file.ReadObject 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 ReadObject

use of com.bonree.brfs.common.net.tcp.file.ReadObject in project BRFS by zhangnianli.

the class DefaultStorageNameStick method readData.

@Override
public InputItem readData(String fid) throws Exception {
    Fid fidObj = FidDecoder.build(fid);
    if (fidObj.getStorageNameCode() != storageId) {
        throw new IllegalAccessException("Storage name of fid is not legal!");
    }
    StringBuilder nameBuilder = new StringBuilder(fidObj.getUuid());
    String[] serverList = new String[fidObj.getServerIdCount()];
    for (int i = 0; i < fidObj.getServerIdCount(); i++) {
        String id = fidObj.getServerId(i);
        nameBuilder.append('_').append(id);
        serverList[i] = id;
    }
    try {
        // 最大尝试副本数个server
        for (int i = 0; i < serverList.length; i++) {
            ServiceMetaInfo serviceMetaInfo = selector.selectService(fidObj.getUuid(), serverList);
            if (serviceMetaInfo.getFirstServer() == null) {
                serverList[serviceMetaInfo.getReplicatPot() - 1] = null;
                continue;
            }
            Service service = serviceMetaInfo.getFirstServer();
            LOG.info("read service[{}]", service);
            ReadConnection fileReader = connectionPool.getConnection(service);
            try {
                ReadObject readObject = new ReadObject();
                readObject.setSn(storageName);
                readObject.setIndex(serviceMetaInfo.getReplicatPot());
                readObject.setTime(fidObj.getTime());
                readObject.setDuration(fidObj.getDuration());
                readObject.setFileName(nameBuilder.toString());
                // readObject.setFilePath(FilePathBuilder.buildPath(fidObj,
                // timeCache.get(new
                // TimePair(TimeUtils.prevTimeStamp(fidObj.getTime(),
                // fidObj.getDuration()), fidObj.getDuration())),
                // storageName, serviceMetaInfo.getReplicatPot()));
                readObject.setOffset(fidObj.getOffset());
                readObject.setLength((int) fidObj.getSize());
                byte[] fileContent = fileReader.read(readObject);
                return new InputItem() {

                    @Override
                    public byte[] getBytes() {
                        return fileContent;
                    }
                };
            } catch (Exception e) {
                // 使用选择的server没有读取到数据,需要进行排除
                serverList[serviceMetaInfo.getReplicatPot() - 1] = null;
                continue;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Fid(com.bonree.brfs.common.proto.FileDataProtos.Fid) ServiceMetaInfo(com.bonree.brfs.client.route.ServiceMetaInfo) Service(com.bonree.brfs.common.service.Service) BRFSException(com.bonree.brfs.common.exception.BRFSException) IOException(java.io.IOException) InputItem(com.bonree.brfs.client.InputItem) ReadObject(com.bonree.brfs.common.net.tcp.file.ReadObject)

Aggregations

ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)7 FileContentPart (com.bonree.brfs.common.net.tcp.file.client.FileContentPart)5 AsyncFileReaderCreateConfig (com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)2 Service (com.bonree.brfs.common.service.Service)2 TcpDiskNodeClient (com.bonree.brfs.disknode.client.TcpDiskNodeClient)2 SocketAddress (java.net.SocketAddress)2 InputItem (com.bonree.brfs.client.InputItem)1 ServiceMetaInfo (com.bonree.brfs.client.route.ServiceMetaInfo)1 BRFSException (com.bonree.brfs.common.exception.BRFSException)1 FileObjectSyncState (com.bonree.brfs.common.filesync.FileObjectSyncState)1 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)1 TcpClient (com.bonree.brfs.common.net.tcp.client.TcpClient)1 TcpClientCloseListener (com.bonree.brfs.common.net.tcp.client.TcpClientCloseListener)1 TcpClientConfig (com.bonree.brfs.common.net.tcp.client.TcpClientConfig)1 Fid (com.bonree.brfs.common.proto.FileDataProtos.Fid)1 DiskNodeClient (com.bonree.brfs.disknode.client.DiskNodeClient)1 ByteConsumer (com.bonree.brfs.disknode.client.DiskNodeClient.ByteConsumer)1