Search in sources :

Example 16 with Service

use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.

the class DefaultBRFileSystem method createStorageName.

@Override
public boolean createStorageName(String storageName, Map<String, Object> attrs) {
    try {
        Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
        if (serviceList.length == 0) {
            throw new BRFSException("none disknode!!!");
        }
        for (Service service : serviceList) {
            URIBuilder uriBuilder = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getStorageUrlRoot() + "/" + storageName);
            for (Entry<String, Object> attr : attrs.entrySet()) {
                uriBuilder.addParameter(attr.getKey(), String.valueOf(attr.getValue()));
            }
            HttpResponse response = null;
            try {
                response = httpClient.executePut(uriBuilder.build(), defaultHeaders);
            } catch (Exception e) {
                LOG.warn("createStorageName http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for createStorageName!");
            }
            if (response.isReponseOK()) {
                return true;
            }
        }
    } catch (Exception e) {
        LOG.error("createStorageName error", e);
    }
    return false;
}
Also used : Service(com.bonree.brfs.common.service.Service) HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) BRFSException(com.bonree.brfs.common.exception.BRFSException) BRFSException(com.bonree.brfs.common.exception.BRFSException) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Example 17 with Service

use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.

the class DefaultBRFileSystem method openStorageName.

@Override
public StorageNameStick openStorageName(String storageName) {
    StorageNameStick stick = null;
    try {
        Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
        if (serviceList.length == 0) {
            throw new BRFSException("none disknode!!!");
        }
        for (Service service : serviceList) {
            URI uri = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getStorageUrlRoot() + "/" + storageName).build();
            HttpResponse response = null;
            try {
                response = httpClient.executeGet(uri, defaultHeaders);
            } catch (Exception e) {
                LOG.warn("openStorageName http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for deleteStorageName!");
            }
            if (response.isReponseOK()) {
                int storageId = Ints.fromByteArray(response.getResponseBody());
                stick = new DefaultStorageNameStick(storageName, storageId, httpClient, serviceSelectorManager.useDiskSelector(storageId), regionNodeSelector, config);
                return stick;
            }
        }
    } catch (Exception e) {
        LOG.error("openStorageName error", e);
    }
    return stick;
}
Also used : StorageNameStick(com.bonree.brfs.client.StorageNameStick) Service(com.bonree.brfs.common.service.Service) HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) BRFSException(com.bonree.brfs.common.exception.BRFSException) URI(java.net.URI) BRFSException(com.bonree.brfs.common.exception.BRFSException) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Example 18 with Service

use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.

the class CreateSystemTask method getServerIds.

public static List<String> getServerIds(List<Service> sList) {
    List<String> sids = new ArrayList<>();
    if (sList == null || sList.isEmpty()) {
        return sids;
    }
    String sid;
    for (Service server : sList) {
        sid = server.getServiceId();
        if (BrStringUtils.isEmpty(sid)) {
            continue;
        }
        sids.add(sid);
    }
    return sids;
}
Also used : ArrayList(java.util.ArrayList) Service(com.bonree.brfs.common.service.Service)

Example 19 with Service

use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.

the class CopyRecovery method recoveryFileByName.

/**
 * 概述:恢复单个文件
 * @param sm
 * @param sim
 * @param parser
 * @param snNode
 * @param fileName
 * @param dirName
 * @return
 * @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
 */
public static boolean recoveryFileByName(ServiceManager sm, ServerIDManager sim, SecondIDParser parser, StorageRegion snNode, String fileName, String dirName, String dataPath, String operation) {
    String[] sss;
    String remoteName;
    Service remoteService;
    String localPath;
    int remoteIndex;
    int localIndex;
    String remotePath;
    boolean isSuccess = true;
    String snName = snNode.getName();
    int snId = snNode.getId();
    sss = parser.getAliveSecondID(fileName);
    if (sss == null) {
        LOG.warn("alive second Ids is empty");
        return false;
    }
    String secondId = sim.getSecondServerID(snId);
    if (BrStringUtils.isEmpty(secondId)) {
        LOG.warn("{} {} secondid is empty ", snName, snId);
        return false;
    }
    localIndex = isContain(sss, secondId);
    if (-1 == localIndex) {
        LOG.info("{} {} {} is not mine !! skip", secondId, snName, fileName);
        return true;
    }
    localPath = "/" + snName + "/" + localIndex + "/" + dirName + "/" + fileName;
    String localDir = "/" + snName + "/" + localIndex + "/" + dirName + "/";
    File dir = new File(dataPath + localDir);
    if (!dir.exists()) {
        boolean createFlag = dir.mkdirs();
        LOG.debug("create dir :{}, stat:{}", localDir, createFlag);
    }
    if (CopyCheckJob.RECOVERY_CRC.equals(operation)) {
        boolean flag = FileCollection.check(dataPath + localPath);
        LOG.debug("locaPath : {}, CRCSTATUS: {}", dataPath + localPath, flag);
        if (flag) {
            return true;
        } else {
            boolean status = FileUtils.deleteFile(dataPath + localPath);
            LOG.warn("{} crc is error!! delete {}", localPath, status);
        }
    } else {
        File file = new File(dataPath + localPath);
        if (file.exists()) {
            LOG.debug("{} {} is exists, skip", snName, fileName);
            return true;
        }
    }
    remoteIndex = 0;
    for (String snsid : sss) {
        remoteIndex++;
        // 排除自己
        if (secondId.equals(snsid)) {
            LOG.debug(" my son is right,not need to do {} {} {}", fileName, secondId, snsid);
            continue;
        }
        remoteName = sim.getOtherFirstID(snsid, snId);
        if (BrStringUtils.isEmpty(remoteName)) {
            LOG.warn("remote name is empty");
            continue;
        }
        remoteService = sm.getServiceById(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME), remoteName);
        if (remoteService == null) {
            LOG.warn("remote service is empty");
            continue;
        }
        remotePath = "/" + snName + "/" + remoteIndex + "/" + dirName + "/" + fileName;
        isSuccess = copyFrom(remoteService.getHost(), remoteService.getPort(), remoteService.getExtraPort(), 5000, remotePath, dataPath + localPath);
        LOG.info("remote address [{}: {} :{}], remote [{}], local [{}], stat [{}]", remoteService.getHost(), remoteService.getPort(), remoteService.getExtraPort(), remotePath, localPath, isSuccess ? "success" : "fail");
        if (isSuccess) {
            return true;
        }
    }
    return isSuccess;
}
Also used : Service(com.bonree.brfs.common.service.Service) File(java.io.File)

Example 20 with Service

use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.

the class DefaultStorageNameStick method writeData.

@Override
public String[] writeData(InputItem[] itemArrays) {
    WriteDataMessage dataMessage = new WriteDataMessage();
    dataMessage.setStorageNameId(storageId);
    DataItem[] dataItems = new DataItem[itemArrays.length];
    for (int i = 0; i < dataItems.length; i++) {
        dataItems[i] = new DataItem();
        dataItems[i].setBytes(itemArrays[i].getBytes());
    }
    dataMessage.setItems(dataItems);
    try {
        Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
        if (serviceList.length == 0) {
            throw new BRFSException("none disknode!!!");
        }
        for (Service service : serviceList) {
            URI uri = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getDuplicateUrlRoot() + "/").build();
            HttpResponse response = null;
            try {
                response = client.executePost(uri, defaultHeaders, ProtoStuffUtils.serialize(dataMessage));
            } catch (Exception e) {
                LOG.warn("write data http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for writeData!");
            }
            if (response.isReponseOK()) {
                List<String> results = JSON.parseArray(BrStringUtils.fromUtf8Bytes(response.getResponseBody()), String.class);
                String[] fids = new String[results.size()];
                return results.toArray(fids);
            }
        }
    } catch (Exception e) {
        LOG.error("write data error", e);
    }
    return null;
}
Also used : WriteDataMessage(com.bonree.brfs.common.write.data.WriteDataMessage) DataItem(com.bonree.brfs.common.write.data.DataItem) Service(com.bonree.brfs.common.service.Service) HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) URI(java.net.URI) BRFSException(com.bonree.brfs.common.exception.BRFSException) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder) BRFSException(com.bonree.brfs.common.exception.BRFSException)

Aggregations

Service (com.bonree.brfs.common.service.Service)35 IOException (java.io.IOException)10 ExecutorService (java.util.concurrent.ExecutorService)8 BRFSException (com.bonree.brfs.common.exception.BRFSException)7 HttpResponse (com.bonree.brfs.common.net.http.client.HttpResponse)6 URIBuilder (com.bonree.brfs.common.net.http.client.URIBuilder)6 ServiceManager (com.bonree.brfs.common.service.ServiceManager)4 StorageRegion (com.bonree.brfs.duplication.storageregion.StorageRegion)4 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 ReturnCode (com.bonree.brfs.common.ReturnCode)3 HandleResult (com.bonree.brfs.common.net.http.HandleResult)3 DefaultServiceManager (com.bonree.brfs.common.service.impl.DefaultServiceManager)3 DiskNodeClient (com.bonree.brfs.disknode.client.DiskNodeClient)3 DiskNodeConnection (com.bonree.brfs.duplication.datastream.connection.DiskNodeConnection)3 DuplicateNode (com.bonree.brfs.duplication.filenode.duplicates.DuplicateNode)3 SimpleAuthentication (com.bonree.brfs.authentication.SimpleAuthentication)2 UserModel (com.bonree.brfs.authentication.model.UserModel)2 ZookeeperPaths (com.bonree.brfs.common.ZookeeperPaths)2 FileObjectSyncState (com.bonree.brfs.common.filesync.FileObjectSyncState)2