Search in sources :

Example 96 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class DashboardServiceImpl method refreshStatistics2DB.

private void refreshStatistics2DB(ZkCluster zkCluster) {
    HashMap<String, JobStatistics> /**
     * {jobname}-{domain}
     */
    jobMap = new HashMap<>();
    HashMap<String, ExecutorStatistics> /**
     * {executorName}-{domain}
     */
    executorMap = new HashMap<>();
    List<JobStatistics> jobList = new ArrayList<>();
    List<ExecutorStatistics> executorList = new ArrayList<>();
    List<AbnormalJob> unnormalJobList = new ArrayList<>();
    List<AbnormalJob> unableFailoverJobList = new ArrayList<>();
    List<Timeout4AlarmJob> timeout4AlarmJobList = new ArrayList<>();
    List<DomainStatistics> domainList = new ArrayList<>();
    List<AbnormalContainer> abnormalContainerList = new ArrayList<>();
    // 不同版本的域数量
    Map<String, Long> versionDomainNumber = new HashMap<>();
    // 不同版本的executor数量
    Map<String, Long> versionExecutorNumber = new HashMap<>();
    int exeInDocker = 0;
    int exeNotInDocker = 0;
    int totalCount = 0;
    int errorCount = 0;
    for (RegistryCenterConfiguration config : zkCluster.getRegCenterConfList()) {
        // 过滤非当前zk连接
        if (zkCluster.getZkAddr().equals(config.getZkAddressList())) {
            int processCountOfThisDomainAllTime = 0;
            int errorCountOfThisDomainAllTime = 0;
            int processCountOfThisDomainThisDay = 0;
            int errorCountOfThisDomainThisDay = 0;
            DomainStatistics domain = new DomainStatistics(config.getNamespace(), zkCluster.getZkAddr(), config.getNameAndNamespace());
            RegistryCenterClient registryCenterClient = registryCenterService.connect(config.getNameAndNamespace());
            try {
                if (registryCenterClient != null && registryCenterClient.isConnected()) {
                    CuratorFramework curatorClient = registryCenterClient.getCuratorClient();
                    CuratorFrameworkOp curatorFrameworkOp = curatorRepository.newCuratorFrameworkOp(curatorClient);
                    // 统计稳定性
                    if (checkExists(curatorClient, ExecutorNodePath.SHARDING_COUNT_PATH)) {
                        String countStr = getData(curatorClient, ExecutorNodePath.SHARDING_COUNT_PATH);
                        domain.setShardingCount(Integer.valueOf(countStr));
                    }
                    // 该域的版本号
                    String version = null;
                    // 该域的在线executor数量
                    long executorNumber = 0L;
                    // 统计物理容器资源,统计版本数据
                    if (null != curatorClient.checkExists().forPath(ExecutorNodePath.getExecutorNodePath())) {
                        List<String> executors = curatorClient.getChildren().forPath(ExecutorNodePath.getExecutorNodePath());
                        if (executors != null) {
                            for (String exe : executors) {
                                // 在线的才统计
                                if (null != curatorClient.checkExists().forPath(ExecutorNodePath.getExecutorIpNodePath(exe))) {
                                    // 统计是物理机还是容器
                                    String executorMapKey = exe + "-" + config.getNamespace();
                                    ExecutorStatistics executorStatistics = executorMap.get(executorMapKey);
                                    if (executorStatistics == null) {
                                        executorStatistics = new ExecutorStatistics(exe, config.getNamespace());
                                        executorStatistics.setNns(domain.getNns());
                                        executorStatistics.setIp(getData(curatorClient, ExecutorNodePath.getExecutorIpNodePath(exe)));
                                        executorMap.put(executorMapKey, executorStatistics);
                                    }
                                    // set runInDocker field
                                    if (checkExists(curatorClient, ExecutorNodePath.get$ExecutorTaskNodePath(exe))) {
                                        executorStatistics.setRunInDocker(true);
                                        exeInDocker++;
                                    } else {
                                        exeNotInDocker++;
                                    }
                                }
                                // 获取版本号
                                if (version == null) {
                                    version = getData(curatorClient, ExecutorNodePath.getExecutorVersionNodePath(exe));
                                }
                            }
                            executorNumber = executors.size();
                        }
                    }
                    // 统计版本数据
                    if (version == null) {
                        // 未知版本
                        version = "-1";
                    }
                    if (versionDomainNumber.containsKey(version)) {
                        Long domainNumber = versionDomainNumber.get(version);
                        versionDomainNumber.put(version, domainNumber + 1);
                    } else {
                        versionDomainNumber.put(version, 1L);
                    }
                    if (versionExecutorNumber.containsKey(version)) {
                        Long executorNumber0 = versionExecutorNumber.get(version);
                        versionExecutorNumber.put(version, executorNumber0 + executorNumber);
                    } else {
                        if (executorNumber != 0) {
                            versionExecutorNumber.put(version, executorNumber);
                        }
                    }
                    // 遍历所有$Jobs子节点,非系统作业
                    List<String> jobs = jobDimensionService.getAllUnSystemJobs(curatorFrameworkOp);
                    SaturnStatistics saturnStatistics = saturnStatisticsService.findStatisticsByNameAndZkList(StatisticsTableKeyConstant.UNNORMAL_JOB, zkCluster.getZkAddr());
                    List<AbnormalJob> oldAbnormalJobs = new ArrayList<>();
                    if (saturnStatistics != null) {
                        String result = saturnStatistics.getResult();
                        if (StringUtils.isNotBlank(result)) {
                            oldAbnormalJobs = JSON.parseArray(result, AbnormalJob.class);
                        }
                    }
                    saturnStatistics = saturnStatisticsService.findStatisticsByNameAndZkList(StatisticsTableKeyConstant.TIMEOUT_4_ALARM_JOB, zkCluster.getZkAddr());
                    List<Timeout4AlarmJob> oldTimeout4AlarmJobs = new ArrayList<>();
                    if (saturnStatistics != null) {
                        String result = saturnStatistics.getResult();
                        if (StringUtils.isNotBlank(result)) {
                            oldTimeout4AlarmJobs = JSON.parseArray(result, Timeout4AlarmJob.class);
                        }
                    }
                    for (String job : jobs) {
                        try {
                            Boolean localMode = Boolean.valueOf(getData(curatorClient, JobNodePath.getConfigNodePath(job, "localMode")));
                            String jobDomainKey = job + "-" + config.getNamespace();
                            JobStatistics jobStatistics = jobMap.get(jobDomainKey);
                            if (jobStatistics == null) {
                                jobStatistics = new JobStatistics(job, config.getNamespace(), config.getNameAndNamespace());
                                jobMap.put(jobDomainKey, jobStatistics);
                            }
                            String jobDegree = getData(curatorClient, JobNodePath.getConfigNodePath(job, "jobDegree"));
                            if (Strings.isNullOrEmpty(jobDegree)) {
                                jobDegree = "0";
                            }
                            jobStatistics.setJobDegree(Integer.parseInt(jobDegree));
                            // 非本地作业才参与判断
                            if (!localMode) {
                                AbnormalJob unnormalJob = new AbnormalJob(job, config.getNamespace(), config.getNameAndNamespace(), config.getDegree());
                                checkJavaOrShellJobHasProblem(oldAbnormalJobs, curatorClient, unnormalJob, jobDegree, unnormalJobList);
                            }
                            // 查找超时告警作业
                            Timeout4AlarmJob timeout4AlarmJob = new Timeout4AlarmJob(job, config.getNamespace(), config.getNameAndNamespace(), config.getDegree());
                            if (isTimeout4AlarmJob(oldTimeout4AlarmJobs, timeout4AlarmJob, curatorFrameworkOp) != null) {
                                timeout4AlarmJob.setJobDegree(jobDegree);
                                timeout4AlarmJobList.add(timeout4AlarmJob);
                            }
                            // 查找无法高可用的作业
                            AbnormalJob unableFailoverJob = new AbnormalJob(job, config.getNamespace(), config.getNameAndNamespace(), config.getDegree());
                            if (isUnableFailoverJob(curatorClient, unableFailoverJob, curatorFrameworkOp) != null) {
                                unableFailoverJob.setJobDegree(jobDegree);
                                unableFailoverJobList.add(unableFailoverJob);
                            }
                            String processCountOfThisJobAllTimeStr = getData(curatorClient, JobNodePath.getProcessCountPath(job));
                            String errorCountOfThisJobAllTimeStr = getData(curatorClient, JobNodePath.getErrorCountPath(job));
                            int processCountOfThisJobAllTime = processCountOfThisJobAllTimeStr == null ? 0 : Integer.valueOf(processCountOfThisJobAllTimeStr);
                            int errorCountOfThisJobAllTime = processCountOfThisJobAllTimeStr == null ? 0 : Integer.valueOf(errorCountOfThisJobAllTimeStr);
                            processCountOfThisDomainAllTime += processCountOfThisJobAllTime;
                            errorCountOfThisDomainAllTime += errorCountOfThisJobAllTime;
                            int processCountOfThisJobThisDay = 0;
                            int errorCountOfThisJobThisDay = 0;
                            // loadLevel of this job
                            int loadLevel = Integer.parseInt(getData(curatorClient, JobNodePath.getConfigNodePath(job, "loadLevel")));
                            int shardingTotalCount = Integer.parseInt(getData(curatorClient, JobNodePath.getConfigNodePath(job, "shardingTotalCount")));
                            List<String> servers = null;
                            if (null != curatorClient.checkExists().forPath(JobNodePath.getServerNodePath(job))) {
                                servers = curatorClient.getChildren().forPath(JobNodePath.getServerNodePath(job));
                                for (String server : servers) {
                                    // processFailureCount,用以统计作业每天的执行次数;2.统计executor的loadLevel;,
                                    if (checkExists(curatorClient, JobNodePath.getServerStatus(job, server))) {
                                        // processFailureCount,用以统计作业每天的执行次数;
                                        try {
                                            String processSuccessCountOfThisExeStr = getData(curatorClient, JobNodePath.getProcessSucessCount(job, server));
                                            String processFailureCountOfThisExeStr = getData(curatorClient, JobNodePath.getProcessFailureCount(job, server));
                                            int processSuccessCountOfThisExe = processSuccessCountOfThisExeStr == null ? 0 : Integer.valueOf(processSuccessCountOfThisExeStr);
                                            int processFailureCountOfThisExe = processFailureCountOfThisExeStr == null ? 0 : Integer.valueOf(processFailureCountOfThisExeStr);
                                            // 该作业当天运行统计
                                            processCountOfThisJobThisDay += processSuccessCountOfThisExe + processFailureCountOfThisExe;
                                            errorCountOfThisJobThisDay += processFailureCountOfThisExe;
                                            // 全部域当天的成功数与失败数
                                            totalCount += processSuccessCountOfThisExe + processFailureCountOfThisExe;
                                            errorCount += processFailureCountOfThisExe;
                                            // 全域当天运行统计
                                            processCountOfThisDomainThisDay += processCountOfThisJobThisDay;
                                            errorCountOfThisDomainThisDay += errorCountOfThisJobThisDay;
                                            // executor当天运行成功失败数
                                            String executorMapKey = server + "-" + config.getNamespace();
                                            ExecutorStatistics executorStatistics = executorMap.get(executorMapKey);
                                            if (executorStatistics == null) {
                                                executorStatistics = new ExecutorStatistics(server, config.getNamespace());
                                                executorStatistics.setNns(domain.getNns());
                                                executorStatistics.setIp(getData(curatorClient, ExecutorNodePath.getExecutorIpNodePath(server)));
                                                executorMap.put(executorMapKey, executorStatistics);
                                            }
                                            executorStatistics.setFailureCountOfTheDay(executorStatistics.getFailureCountOfTheDay() + processFailureCountOfThisExe);
                                            executorStatistics.setProcessCountOfTheDay(executorStatistics.getProcessCountOfTheDay() + processSuccessCountOfThisExe + processFailureCountOfThisExe);
                                        } catch (Exception e) {
                                            log.info(e.getMessage());
                                        }
                                        // 2.统计executor的loadLevel;
                                        try {
                                            // enabled 的作业才需要计算权重
                                            if (Boolean.valueOf(getData(curatorClient, JobNodePath.getConfigNodePath(job, "enabled")))) {
                                                String sharding = getData(curatorClient, JobNodePath.getServerSharding(job, server));
                                                if (StringUtils.isNotEmpty(sharding)) {
                                                    // 更新job的executorsAndshards
                                                    String exesAndShards = (jobStatistics.getExecutorsAndShards() == null ? "" : jobStatistics.getExecutorsAndShards()) + server + ":" + sharding + "; ";
                                                    jobStatistics.setExecutorsAndShards(exesAndShards);
                                                    // 2.统计是物理机还是容器
                                                    String executorMapKey = server + "-" + config.getNamespace();
                                                    ExecutorStatistics executorStatistics = executorMap.get(executorMapKey);
                                                    if (executorStatistics == null) {
                                                        executorStatistics = new ExecutorStatistics(server, config.getNamespace());
                                                        executorStatistics.setNns(domain.getNns());
                                                        executorStatistics.setIp(getData(curatorClient, ExecutorNodePath.getExecutorIpNodePath(server)));
                                                        executorMap.put(executorMapKey, executorStatistics);
                                                        // set runInDocker field
                                                        if (checkExists(curatorClient, ExecutorNodePath.get$ExecutorTaskNodePath(server))) {
                                                            executorStatistics.setRunInDocker(true);
                                                            exeInDocker++;
                                                        } else {
                                                            exeNotInDocker++;
                                                        }
                                                    }
                                                    if (executorStatistics.getJobAndShardings() != null) {
                                                        executorStatistics.setJobAndShardings(executorStatistics.getJobAndShardings() + job + ":" + sharding + ";");
                                                    } else {
                                                        executorStatistics.setJobAndShardings(job + ":" + sharding + ";");
                                                    }
                                                    int newLoad = executorStatistics.getLoadLevel() + (loadLevel * sharding.split(",").length);
                                                    executorStatistics.setLoadLevel(newLoad);
                                                }
                                            }
                                        } catch (Exception e) {
                                            log.info(e.getMessage());
                                        }
                                    }
                                }
                            }
                            // local-mode job = server count(regardless server status)
                            if (localMode) {
                                jobStatistics.setTotalLoadLevel(servers == null ? 0 : (servers.size() * loadLevel));
                            } else {
                                jobStatistics.setTotalLoadLevel(loadLevel * shardingTotalCount);
                            }
                            jobStatistics.setErrorCountOfAllTime(errorCountOfThisJobAllTime);
                            jobStatistics.setProcessCountOfAllTime(processCountOfThisJobAllTime);
                            jobStatistics.setFailureCountOfTheDay(errorCountOfThisJobThisDay);
                            jobStatistics.setProcessCountOfTheDay(processCountOfThisJobThisDay);
                            jobMap.put(jobDomainKey, jobStatistics);
                        } catch (Exception e) {
                            log.info("statistics namespace:{} ,jobName:{} ,exception:{}", domain.getNns(), job, e.getMessage());
                        }
                    }
                    // 遍历容器资源,获取异常资源
                    String dcosTasksNodePath = ContainerNodePath.getDcosTasksNodePath();
                    List<String> tasks = curatorFrameworkOp.getChildren(dcosTasksNodePath);
                    if (tasks != null && !tasks.isEmpty()) {
                        for (String taskId : tasks) {
                            AbnormalContainer abnormalContainer = new AbnormalContainer(taskId, config.getNamespace(), config.getNameAndNamespace(), config.getDegree());
                            if (isContainerInstanceMismatch(abnormalContainer, curatorFrameworkOp) != null) {
                                abnormalContainerList.add(abnormalContainer);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                log.info("refreshStatistics2DB namespace:{} ,exception:{}", domain.getNns(), e.getMessage());
            }
            domain.setErrorCountOfAllTime(errorCountOfThisDomainAllTime);
            domain.setProcessCountOfAllTime(processCountOfThisDomainAllTime);
            domain.setErrorCountOfTheDay(errorCountOfThisDomainThisDay);
            domain.setProcessCountOfTheDay(processCountOfThisDomainThisDay);
            domainList.add(domain);
        }
    }
    jobList.addAll(jobMap.values());
    executorList.addAll(executorMap.values());
    // 全域当天处理总数,失败总数
    saveOrUpdateDomainProcessCount(new ZkStatistics(totalCount, errorCount), zkCluster.getZkAddr());
    // 失败率Top10的域列表
    saveOrUpdateTop10FailDomain(domainList, zkCluster.getZkAddr());
    // 稳定性最差的Top10的域列表
    saveOrUpdateTop10UnstableDomain(domainList, zkCluster.getZkAddr());
    // 稳定性最差的Top10的executor列表
    saveOrUpdateTop10FailExecutor(executorList, zkCluster.getZkAddr());
    // 根据失败率Top10的作业列表
    saveOrUpdateTop10FailJob(jobList, zkCluster.getZkAddr());
    // 最活跃作业Top10的作业列表(即当天执行次数最多的作业)
    saveOrUpdateTop10ActiveJob(jobList, zkCluster.getZkAddr());
    // 负荷最重的Top10的作业列表
    saveOrUpdateTop10LoadJob(jobList, zkCluster.getZkAddr());
    // 负荷最重的Top10的Executor列表
    saveOrUpdateTop10LoadExecutor(executorList, zkCluster.getZkAddr());
    // 异常作业列表 (如下次调度时间已经过了,但是作业没有被调度)
    saveOrUpdateAbnormalJob(unnormalJobList, zkCluster.getZkAddr());
    // 超时告警的作业列表
    saveOrUpdateTimeout4AlarmJob(timeout4AlarmJobList, zkCluster.getZkAddr());
    // 无法高可用的作业列表
    saveOrUpdateUnableFailoverJob(unableFailoverJobList, zkCluster.getZkAddr());
    // 异常容器资源列表,包含实例数不匹配的资源列表
    saveOrUpdateAbnormalContainer(abnormalContainerList, zkCluster.getZkAddr());
    // 不同版本的域数量
    saveOrUpdateVersionDomainNumber(versionDomainNumber, zkCluster.getZkAddr());
    // 不同版本的executor数量
    saveOrUpdateVersionExecutorNumber(versionExecutorNumber, zkCluster.getZkAddr());
    // 不同作业等级的作业数量
    saveOrUpdateJobRankDistribution(jobList, zkCluster.getZkAddr());
    // 容器executor数量
    saveOrUpdateExecutorInDockerCount(exeInDocker, zkCluster.getZkAddr());
    // 物理机executor数量
    saveOrUpdateExecutorNotInDockerCount(exeNotInDocker, zkCluster.getZkAddr());
    // 作业数量
    saveOrUpdateJobCount(jobList.size(), zkCluster.getZkAddr());
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CuratorFramework(org.apache.curator.framework.CuratorFramework) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) JobConsoleException(com.vip.saturn.job.console.exception.JobConsoleException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) SaturnStatistics(com.vip.saturn.job.console.mybatis.entity.SaturnStatistics) CuratorFrameworkOp(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp)

Example 97 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class DashboardServiceImpl method cleanAllJobAnalyse.

@Override
public void cleanAllJobAnalyse(String nns) throws Exception {
    // 获取当前连接
    RegistryCenterClient registryCenterClient = registryCenterService.connect(nns);
    if (registryCenterClient.isConnected()) {
        CuratorFramework curatorClient = registryCenterClient.getCuratorClient();
        CuratorFrameworkOp curatorFrameworkOp = curatorRepository.newCuratorFrameworkOp(curatorClient);
        // 遍历所有$Jobs子节点,非系统作业
        List<String> jobs = jobDimensionService.getAllUnSystemJobs(curatorFrameworkOp);
        for (String job : jobs) {
            resetOneJobAnalyse(job, curatorClient);
            // reset analyse data.
            updateResetValue(curatorClient, job, ResetCountType.RESET_ANALYSE);
        }
        asyncForceRefreshStatistics();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorFrameworkOp(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp)

Example 98 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class NamespaceZkClusterMappingServiceImpl method moveNamespaceTo.

@Transactional(rollbackFor = { SaturnJobConsoleException.class })
@Override
public void moveNamespaceTo(String namespace, String zkClusterKeyNew, String lastUpdatedBy, boolean updateDBOnly) throws SaturnJobConsoleException {
    try {
        log.info("start move {} to {}", namespace, zkClusterKeyNew);
        if (updateDBOnly) {
            namespaceZkclusterMapping4SqlService.update(namespace, null, zkClusterKeyNew, lastUpdatedBy);
        } else {
            String zkClusterKey = namespaceZkclusterMapping4SqlService.getZkClusterKey(namespace);
            if (zkClusterKey != null && zkClusterKey.equals(zkClusterKeyNew)) {
                // see
                throw new SaturnJobConsoleException("The namespace(" + namespace + ") is in " + zkClusterKey);
            // moveNamespaceBatchTo
            // before
            // modify
            }
            ZkCluster zkCluster = registryCenterService.getZkCluster(zkClusterKeyNew);
            if (zkCluster == null) {
                throw new SaturnJobConsoleException("The " + zkClusterKeyNew + " is not exists");
            }
            if (zkCluster.isOffline()) {
                throw new SaturnJobConsoleException("The " + zkClusterKeyNew + " zkCluster is offline");
            }
            String zkAddr = zkCluster.getZkAddr();
            CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.connectOnly(zkAddr, null);
            if (curatorFrameworkOp == null) {
                throw new SaturnJobConsoleException("The " + zkClusterKeyNew + " zkCluster is offline");
            }
            CuratorFramework curatorFramework = curatorFrameworkOp.getCuratorFramework();
            CuratorRepository.CuratorFrameworkOp curatorFrameworkOpByNamespace = registryCenterService.connectOnly(zkAddr, namespace);
            CuratorFramework curatorFrameworkByNamespace = curatorFrameworkOpByNamespace.getCuratorFramework();
            try {
                String namespaceNodePath = "/" + namespace;
                if (curatorFramework.checkExists().forPath(namespaceNodePath) != null) {
                    curatorFramework.delete().deletingChildrenIfNeeded().forPath(namespaceNodePath);
                }
                String jobsNodePath = namespaceNodePath + JobNodePath.get$JobsNodePath();
                curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(jobsNodePath);
                List<CurrentJobConfig> configs = currentJobConfigService.findConfigsByNamespace(namespace);
                log.info("get configs success, {}", namespace);
                if (configs != null) {
                    for (CurrentJobConfig jobConfig : configs) {
                        jobOperationService.persistJobFromDB(jobConfig, curatorFrameworkOpByNamespace);
                        log.info("move {}-{} to zk success", namespace, jobConfig.getJobName());
                    }
                }
            } finally {
                curatorFramework.close();
                curatorFrameworkByNamespace.close();
            }
            log.info("move {} to zk {} success", namespace, zkClusterKeyNew);
            namespaceZkclusterMapping4SqlService.update(namespace, null, zkClusterKeyNew, lastUpdatedBy);
            log.info("update mapping table success, {}-{}", namespace, zkClusterKeyNew);
        }
    } catch (SaturnJobConsoleException e) {
        log.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    } finally {
        log.info("end move {} to {}", namespace, zkClusterKeyNew);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CurrentJobConfig(com.vip.saturn.job.console.mybatis.entity.CurrentJobConfig) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) ZkCluster(com.vip.saturn.job.console.domain.ZkCluster) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 99 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class RegistryCenterServiceImpl method connect.

@Override
public RegistryCenterClient connect(final String nameAndNameSpace) {
    final RegistryCenterClient registryCenterClient = new RegistryCenterClient();
    registryCenterClient.setNameAndNamespace(nameAndNameSpace);
    if (nameAndNameSpace == null) {
        return registryCenterClient;
    }
    synchronized (getNnsLock(nameAndNameSpace)) {
        if (!registryCenterClientMap.containsKey(nameAndNameSpace)) {
            RegistryCenterConfiguration registryCenterConfiguration = findConfig(nameAndNameSpace);
            if (registryCenterConfiguration == null) {
                return registryCenterClient;
            }
            String zkAddressList = registryCenterConfiguration.getZkAddressList();
            String namespace = registryCenterConfiguration.getNamespace();
            String digest = registryCenterConfiguration.getDigest();
            registryCenterClient.setZkAddr(zkAddressList);
            CuratorFramework client = curatorRepository.connect(zkAddressList, namespace, digest);
            if (client == null) {
                return registryCenterClient;
            }
            registryCenterClient.setConnected(client.getZookeeperClient().isConnected());
            registryCenterClient.setCuratorClient(client);
            registryCenterClientMap.put(nameAndNameSpace, registryCenterClient);
            return registryCenterClient;
        } else {
            RegistryCenterClient registryCenterClient2 = registryCenterClientMap.get(nameAndNameSpace);
            if (registryCenterClient2 != null) {
                if (registryCenterClient2.getCuratorClient() != null) {
                    registryCenterClient2.setConnected(registryCenterClient2.getCuratorClient().getZookeeperClient().isConnected());
                } else {
                    registryCenterClient2.setConnected(false);
                }
                return registryCenterClient2;
            }
            return registryCenterClient;
        }
    }
}
Also used : RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) CuratorFramework(org.apache.curator.framework.CuratorFramework) RegistryCenterClient(com.vip.saturn.job.console.domain.RegistryCenterClient)

Example 100 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.

the class RegistryCenterServiceImpl method connectByNamespace.

@Override
public RegistryCenterClient connectByNamespace(String namespace) {
    RegistryCenterConfiguration registryCenterConfiguration = findConfigByNamespace(namespace);
    if (registryCenterConfiguration == null) {
        return new RegistryCenterClient();
    }
    String nns = registryCenterConfiguration.getNameAndNamespace();
    if (nns == null) {
        return new RegistryCenterClient();
    }
    String zkAddressList = registryCenterConfiguration.getZkAddressList();
    String digest = registryCenterConfiguration.getDigest();
    synchronized (getNnsLock(nns)) {
        if (!registryCenterClientMap.containsKey(nns)) {
            final RegistryCenterClient registryCenterClient = new RegistryCenterClient();
            registryCenterClient.setNameAndNamespace(nns);
            registryCenterClient.setZkAddr(zkAddressList);
            CuratorFramework client = curatorRepository.connect(zkAddressList, namespace, digest);
            if (client == null) {
                return registryCenterClient;
            }
            registryCenterClient.setConnected(client.getZookeeperClient().isConnected());
            registryCenterClient.setCuratorClient(client);
            registryCenterClientMap.put(nns, registryCenterClient);
            return registryCenterClient;
        } else {
            RegistryCenterClient registryCenterClient = registryCenterClientMap.get(nns);
            if (registryCenterClient == null) {
                registryCenterClient = new RegistryCenterClient();
                registryCenterClient.setNameAndNamespace(namespace);
                registryCenterClient.setZkAddr(zkAddressList);
            } else {
                if (registryCenterClient.getCuratorClient() != null) {
                    registryCenterClient.setConnected(registryCenterClient.getCuratorClient().getZookeeperClient().isConnected());
                } else {
                    registryCenterClient.setConnected(false);
                }
            }
            return registryCenterClient;
        }
    }
}
Also used : RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) CuratorFramework(org.apache.curator.framework.CuratorFramework) RegistryCenterClient(com.vip.saturn.job.console.domain.RegistryCenterClient)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)824 Test (org.testng.annotations.Test)290 RetryOneTime (org.apache.curator.retry.RetryOneTime)267 Test (org.junit.Test)168 Timing (org.apache.curator.test.Timing)147 CountDownLatch (java.util.concurrent.CountDownLatch)120 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)98 KeeperException (org.apache.zookeeper.KeeperException)84 IOException (java.io.IOException)75 ConnectionState (org.apache.curator.framework.state.ConnectionState)70 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)52 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)50 RetryNTimes (org.apache.curator.retry.RetryNTimes)48 ExecutorService (java.util.concurrent.ExecutorService)47 ArrayList (java.util.ArrayList)44 RetryPolicy (org.apache.curator.RetryPolicy)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 Stat (org.apache.zookeeper.data.Stat)35 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)33 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)32