Search in sources :

Example 6 with CuratorClient

use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.

the class CopyCountCheck method collectionSnFiles.

/**
 * 概述:获取集群对应目录的文件
 * @param services
 * @param snList
 * @return
 * @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
 */
public static Map<StorageRegion, List<String>> collectionSnFiles(List<Service> services, List<StorageRegion> snList, final Map<String, Long> snTimes) throws Exception {
    Map<StorageRegion, List<String>> snMap = new HashMap<>();
    DiskNodeClient client = null;
    int reCount;
    String snName = null;
    String path;
    List<String> strs;
    long time;
    String dirName;
    String sid;
    ManagerContralFactory mcf = ManagerContralFactory.getInstance();
    ServerIDManager sim = mcf.getSim();
    CuratorClient zkClient = mcf.getClient();
    SecondIDParser parser;
    String basePath = mcf.getZkPath().getBaseRoutePath();
    int timeout = 10000;
    for (Service service : services) {
        try {
            client = TcpClientUtils.getClient(service.getHost(), service.getPort(), service.getExtraPort(), timeout);
            long granule;
            for (StorageRegion sn : snList) {
                parser = new SecondIDParser(zkClient, sn.getId(), basePath);
                parser.updateRoute();
                sid = sim.getOtherSecondID(service.getServiceId(), sn.getId());
                granule = Duration.parse(sn.getFilePartitionDuration()).toMillis();
                reCount = sn.getReplicateNum();
                snName = sn.getName();
                if (!snTimes.containsKey(snName)) {
                    LOG.debug("sntime don't contain {}", snName);
                    continue;
                }
                time = snTimes.get(snName);
                dirName = TimeUtils.timeInterval(time, granule);
                for (int i = 1; i <= reCount; i++) {
                    path = "/" + snName + "/" + i + "/" + dirName;
                    LOG.debug("path :{}", path);
                    strs = getFileList(parser, client, path, sid);
                    if (strs == null || strs.isEmpty()) {
                        LOG.debug("files is empty {}", path);
                        continue;
                    }
                    LOG.debug("Collection dirName :{},{} size :{}", dirName, path, strs.size());
                    if (!snMap.containsKey(sn)) {
                        snMap.put(sn, new ArrayList<>());
                    }
                    snMap.get(sn).addAll(strs);
                }
            }
        } catch (Exception e) {
            EmailPool emailPool = EmailPool.getInstance();
            MailWorker.Builder builder = MailWorker.newBuilder(emailPool.getProgramInfo());
            builder.setModel("collect file execute 模块服务发生问题");
            builder.setException(e);
            builder.setMessage(mcf.getGroupName() + "(" + mcf.getServerId() + ")服务 执行任务时发生问题");
            Map<String, String> map = new HashedMap();
            map.put("remote ", service.getHost());
            map.put("connectTimeout", String.valueOf(timeout));
            map.put("sn", StringUtils.isEmpty(snName) ? "" : snName);
            if (snTimes != null && !snTimes.isEmpty()) {
                map.put("sntime", JSON.toJSONString(snTimes));
            }
            builder.setVariable(map);
            emailPool.sendEmail(builder);
            throw e;
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    LOG.error("{}", e);
                }
            }
        }
    }
    return clearUnLawFiles(snMap);
}
Also used : SecondIDParser(com.bonree.brfs.rebalance.route.SecondIDParser) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient) Service(com.bonree.brfs.common.service.Service) EmailPool(com.bonree.brfs.email.EmailPool) IOException(java.io.IOException) ServerIDManager(com.bonree.brfs.server.identification.ServerIDManager) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) StorageRegion(com.bonree.brfs.duplication.storageregion.StorageRegion) IOException(java.io.IOException) DiskNodeClient(com.bonree.brfs.disknode.client.DiskNodeClient) HashedMap(org.apache.commons.collections.map.HashedMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashedMap(org.apache.commons.collections.map.HashedMap)

Example 7 with CuratorClient

use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.

the class ReadMetaData method main.

public static void main(String[] args) throws JsonException {
    String path = "/brfs/test1/rebalance/changes/0";
    Map<String, List<ChangeSummary>> csMap = Maps.newHashMap();
    CuratorClient curatorClient = CuratorClient.getClientInstance("192.168.107.13");
    List<String> childNodeList = curatorClient.getChildren(path);
    Collections.sort(childNodeList);
    for (String node : childNodeList) {
        String nodePath = ZKPaths.makePath(path, node);
        ChangeSummary cs = JsonUtils.toObject(curatorClient.getData(nodePath), ChangeSummary.class);
        if (csMap.get(cs.getChangeServer()) == null) {
            List<ChangeSummary> csList = Lists.newArrayList();
            csList.add(cs);
            csMap.put(cs.getChangeServer(), csList);
        } else {
            csMap.get(cs.getChangeServer()).add(cs);
        }
    }
    for (Entry<String, List<ChangeSummary>> entry : csMap.entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
        System.out.println(entry.getKey() + ":" + entry.getValue().stream().map(x -> x.getChangeType()).collect(Collectors.toList()));
    }
    curatorClient.close();
}
Also used : Maps(org.apache.curator.shaded.com.google.common.collect.Maps) List(java.util.List) ZKPaths(org.apache.curator.utils.ZKPaths) JsonException(com.bonree.brfs.common.utils.JsonUtils.JsonException) Lists(com.google.common.collect.Lists) JsonUtils(com.bonree.brfs.common.utils.JsonUtils) Map(java.util.Map) Entry(java.util.Map.Entry) CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient) List(java.util.List)

Example 8 with CuratorClient

use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.

the class GatherResourceJob method operation.

@Override
public void operation(JobExecutionContext context) throws Exception {
    if (mountPoints == null) {
        String[] mounts = StringUtils.split(Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_UNMONITOR_PARTITION), ",");
        if (mounts != null) {
            mountPoints = new ArrayList<>(mounts.length);
            for (String mount : mounts) {
                if (BrStringUtils.isEmpty(mount)) {
                    continue;
                }
                mountPoints.add(mount.trim());
            }
        }
    }
    JobDataMap data = context.getJobDetail().getJobDataMap();
    if (data == null || data.isEmpty()) {
        throw new NullPointerException("job data map is empty");
    }
    String dataDir = data.getString(JobDataMapConstract.DATA_PATH);
    String zkPath = data.getString(JobDataMapConstract.BASE_SERVER_ID_PATH);
    String ip = data.getString(JobDataMapConstract.IP);
    ManagerContralFactory mcf = ManagerContralFactory.getInstance();
    CuratorClient client = mcf.getClient();
    if (client == null) {
        LOG.error("zookeeper is empty !!!!");
        return;
    }
    String basePath = zkPath + "/" + mcf.getGroupName();
    String bPath = basePath + "/base/" + mcf.getServerId();
    if (!client.checkExists(bPath)) {
        saveLocal(client, mcf.getServerId(), dataDir, bPath);
    }
    long gatherInveral = data.getLongValueFromString(JobDataMapConstract.GATHER_INVERAL_TIME);
    int count = data.getIntFromString(JobDataMapConstract.CALC_RESOURCE_COUNT);
    StateMetaServerModel metaSource = GatherResource.gatherResource(dataDir, ip, mountPoints);
    if (metaSource != null) {
        queue.add(metaSource);
        LOG.info("gather stat info !!! {}", queue.size());
    }
    int queueSize = queue.size();
    if (queueSize < count) {
        return;
    }
    // 更新任务的可执行资源
    StatServerModel sum = calcStateServer(gatherInveral, dataDir, count);
    if (sum != null) {
        RunnableTaskInterface rt = mcf.getRt();
        rt.update(sum);
    }
    // 计算可用服务
    BaseMetaServerModel base = getClusterBases(client, basePath + "/base", mcf.getGroupName());
    if (base == null) {
        return;
    }
    String serverId = mcf.getServerId();
    // 计算资源值
    ResourceModel resource = GatherResource.calcResourceValue(base, sum, serverId, ip);
    if (resource == null) {
        LOG.warn("calc resource value is null !!!");
        return;
    }
    sendWarnEmail(resource, mcf.getLimitServerResource());
    resource.setServerId(serverId);
    Map<Integer, String> snIds = getStorageNameIdWithName();
    resource.setSnIds(snIds);
    byte[] rdata = JsonUtils.toJsonBytesQuietly(resource);
    String rPath = basePath + "/resource/" + serverId;
    if (!saveDataToZK(client, rPath, rdata)) {
        LOG.error("resource content :{} save to zk fail !!!", JsonUtils.toJsonStringQuietly(resource));
    } else {
        LOG.info("resource: succefull !!!");
    }
    BaseMetaServerModel local = GatherResource.gatherBase(serverId, dataDir, mountPoints);
    if (local == null) {
        LOG.error("gather base data is empty !!!");
        return;
    }
    byte[] bData = JsonUtils.toJsonBytesQuietly(local);
    if (!saveDataToZK(client, bPath, bData)) {
        LOG.error("base content : {} save to zk fail!!!", JsonUtils.toJsonStringQuietly(local));
    }
    saveLocal(client, mcf.getServerId(), dataDir, bPath);
}
Also used : JobDataMap(org.quartz.JobDataMap) CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) RunnableTaskInterface(com.bonree.brfs.schedulers.task.manager.RunnableTaskInterface)

Example 9 with CuratorClient

use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.

the class TaskDispatcher method taskTerminal.

public void taskTerminal(CuratorFramework client, TreeCacheEvent event) {
    if (leaderLath.hasLeadership()) {
        CuratorClient curatorClient = CuratorClient.wrapClient(client);
        LOG.info("leaderLath:" + getLeaderLatch().hasLeadership());
        LOG.info("task Dispatch event detail:" + RebalanceUtils.convertEvent(event));
        if (event.getType() == Type.NODE_UPDATED) {
            if (event.getData() != null && event.getData().getData() != null) {
                // 此处会检测任务是否完成
                String eventPath = event.getData().getPath();
                if (eventPath.substring(eventPath.lastIndexOf('/') + 1, eventPath.length()).equals(Constants.TASK_NODE)) {
                    return;
                }
                String parentPath = StringUtils.substring(eventPath, 0, eventPath.lastIndexOf('/'));
                // 节点已经删除,则忽略
                if (!curatorClient.checkExists(parentPath)) {
                    return;
                }
                BalanceTaskSummary bts = JsonUtils.toObjectQuietly(curatorClient.getData(parentPath), BalanceTaskSummary.class);
                List<String> serverIds = curatorClient.getChildren(parentPath);
                // 判断是否所有的节点做完任务
                boolean finishFlag = true;
                if (serverIds != null) {
                    if (serverIds.isEmpty()) {
                        LOG.info("taskoperation is not execute task!!!");
                        finishFlag = false;
                    } else {
                        for (String serverId : serverIds) {
                            String nodePath = ZKPaths.makePath(parentPath, serverId);
                            TaskDetail td = JsonUtils.toObjectQuietly(curatorClient.getData(nodePath), TaskDetail.class);
                            if (td.getStatus() != DataRecover.ExecutionStatus.FINISH) {
                                finishFlag = false;
                                break;
                            }
                        }
                    }
                }
                // 所有的服务都则发布迁移规则,并清理任务
                if (finishFlag) {
                    // 先更新任务状态为finish
                    updateTaskStatus(bts, TaskStatus.FINISH);
                    // 发布路由规则
                    if (bts.getTaskType() == RecoverType.VIRTUAL) {
                        LOG.info("one virtual task finish,detail:" + RebalanceUtils.convertEvent(event));
                        String virtualRouteNode = ZKPaths.makePath(virtualRoutePath, String.valueOf(bts.getStorageIndex()), bts.getId());
                        VirtualRoute route = new VirtualRoute(bts.getChangeID(), bts.getStorageIndex(), bts.getServerId(), bts.getInputServers().get(0), TaskVersion.V1);
                        LOG.info("add virtual route:" + route);
                        addRoute(virtualRouteNode, JsonUtils.toJsonBytesQuietly(route));
                        // 因共享节点,所以得将余下的所有virtual server id,注册新迁移的server。不足之处,可能为导致副本数的恢复大于服务数。
                        // String firstID = idManager.getOtherFirstID(bts.getInputServers().get(0), bts.getStorageIndex());
                        // List<String> normalVirtualIDs = idManager.listNormalVirtualID(bts.getStorageIndex());
                        // if (normalVirtualIDs != null && !normalVirtualIDs.isEmpty()) {
                        // for (String virtualID : normalVirtualIDs) {
                        // idManager.registerFirstID(bts.getStorageIndex(), virtualID, firstID);
                        // }
                        // }
                        // 删除virtual server ID
                        LOG.info("delete the virtual server id:" + bts.getServerId());
                        idManager.deleteVirtualID(bts.getStorageIndex(), bts.getServerId());
                    } else if (bts.getTaskType() == RecoverType.NORMAL) {
                        LOG.info("one normal task finish,detail:" + RebalanceUtils.convertEvent(event));
                        String normalRouteNode = ZKPaths.makePath(normalRoutePath, String.valueOf(bts.getStorageIndex()), bts.getId());
                        NormalRoute route = new NormalRoute(bts.getChangeID(), bts.getStorageIndex(), bts.getServerId(), bts.getInputServers(), TaskVersion.V1);
                        LOG.info("add normal route:" + route);
                        addRoute(normalRouteNode, JsonUtils.toJsonBytesQuietly(route));
                    }
                    List<ChangeSummary> changeSummaries = cacheSummaryCache.get(bts.getStorageIndex());
                    // 清理zk上的变更
                    removeChange2History(bts);
                    // 清理change cache缓存
                    if (changeSummaries != null) {
                        Iterator<ChangeSummary> it = changeSummaries.iterator();
                        while (it.hasNext()) {
                            ChangeSummary cs = it.next();
                            if (cs.getChangeID().equals(bts.getChangeID())) {
                                changeSummaries.remove(cs);
                            }
                        }
                    }
                    // 删除zk上的任务节点
                    if (delBalanceTask(bts)) {
                        // 清理task缓存
                        removeRunTask(bts.getStorageIndex());
                    }
                }
            }
        }
    }
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Example 10 with CuratorClient

use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.

the class Test method main.

public static void main(String[] args) throws InterruptedException {
    // ExecutorService threads = Executors.newFixedThreadPool(5);
    // 
    // for(int i = 0;i<10;i++) {
    // final int index = i;
    // threads.execute(new Runnable() {
    // 
    // @Override
    // public void run() {
    // CuratorClient client = CuratorClient.getClientInstance("192.168.101.86:2181");
    // System.out.println("/brfs/wz/cache/"+index);
    // //                    client.createEphemeral("/brfs/wz/cache/"+index, false);
    // client.createPersistent("/brfs/wz/cache/"+index, false);
    // //                    client.delete("/brfs/wz/cache/"+index, false);
    // client.close();
    // }
    // });
    // }
    // 
    // threads.shutdown();
    // threads.awaitTermination(1, TimeUnit.DAYS);
    CuratorClient client = CuratorClient.getClientInstance("192.168.101.86:2181");
    client.setData("/brfs/wz/cache/6", "1213213".getBytes());
    client.close();
// client.createPersistent("/brfs/wz/cache/aaaa", true);
// client.delete("/brfs/wz/cache/aaaa",false);
// client.createEphemeral("/brfs/wz/cache/aaaa", true);
}
Also used : CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient)

Aggregations

CuratorClient (com.bonree.brfs.common.zookeeper.curator.CuratorClient)19 UserModel (com.bonree.brfs.authentication.model.UserModel)4 ManagerContralFactory (com.bonree.brfs.schedulers.ManagerContralFactory)4 StorageRegion (com.bonree.brfs.duplication.storageregion.StorageRegion)3 ServerIDManager (com.bonree.brfs.server.identification.ServerIDManager)3 SimpleAuthentication (com.bonree.brfs.authentication.SimpleAuthentication)2 ZookeeperPaths (com.bonree.brfs.common.ZookeeperPaths)2 Service (com.bonree.brfs.common.service.Service)2 ServiceManager (com.bonree.brfs.common.service.ServiceManager)2 StorageRegionManager (com.bonree.brfs.duplication.storageregion.StorageRegionManager)2 EmailPool (com.bonree.brfs.email.EmailPool)2 SecondIDParser (com.bonree.brfs.rebalance.route.SecondIDParser)2 IOException (java.io.IOException)2 ProcessFinalizer (com.bonree.brfs.common.process.ProcessFinalizer)1 NormalRoute (com.bonree.brfs.common.rebalance.route.NormalRoute)1 VirtualRoute (com.bonree.brfs.common.rebalance.route.VirtualRoute)1 DefaultServiceManager (com.bonree.brfs.common.service.impl.DefaultServiceManager)1 JsonUtils (com.bonree.brfs.common.utils.JsonUtils)1 JsonException (com.bonree.brfs.common.utils.JsonUtils.JsonException)1 CuratorLocksClient (com.bonree.brfs.common.zookeeper.curator.locking.CuratorLocksClient)1