Search in sources :

Example 1 with InputItem

use of com.bonree.brfs.client.InputItem 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

InputItem (com.bonree.brfs.client.InputItem)1 ServiceMetaInfo (com.bonree.brfs.client.route.ServiceMetaInfo)1 BRFSException (com.bonree.brfs.common.exception.BRFSException)1 ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)1 Fid (com.bonree.brfs.common.proto.FileDataProtos.Fid)1 Service (com.bonree.brfs.common.service.Service)1 IOException (java.io.IOException)1