Search in sources :

Example 1 with NamespaceShardingManager

use of com.vip.saturn.job.sharding.NamespaceShardingManager in project Saturn by vipshop.

the class RegistryCenterServiceImpl method refreshNamespaceShardingListenerManagerMap.

/**
 * 创建或者移除namespaceShardingManager.
 */
private void refreshNamespaceShardingListenerManagerMap() {
    Iterator<Entry<String, ZkCluster>> iterator = zkClusterMap.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, ZkCluster> next = iterator.next();
        ZkCluster zkCluster = next.getValue();
        ArrayList<RegistryCenterConfiguration> regCenterConfList = zkCluster.getRegCenterConfList();
        if (regCenterConfList == null) {
            continue;
        }
        for (RegistryCenterConfiguration conf : regCenterConfList) {
            String nns = conf.getNameAndNamespace();
            if (!namespaceShardingListenerManagerMap.containsKey(nns)) {
                if (isZKClusterCanBeComputed(conf.getZkClusterKey())) {
                    createNamespaceShardingManager(conf, nns);
                }
            } else {
                NamespaceShardingManager namespaceShardingManager = namespaceShardingListenerManagerMap.get(nns);
                if (!isZKClusterCanBeComputed(conf.getZkClusterKey())) {
                    namespaceShardingManager.stopWithCurator();
                    namespaceShardingListenerManagerMap.remove(nns);
                }
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) NamespaceShardingManager(com.vip.saturn.job.sharding.NamespaceShardingManager) ZkCluster(com.vip.saturn.job.console.domain.ZkCluster)

Example 2 with NamespaceShardingManager

use of com.vip.saturn.job.sharding.NamespaceShardingManager in project Saturn by vipshop.

the class RegistryCenterServiceImpl method createNamespaceShardingManager.

private void createNamespaceShardingManager(RegistryCenterConfiguration conf, String nns) {
    try {
        log.info("Start NamespaceShardingManager {}", nns);
        String namespace = conf.getNamespace();
        String digest = conf.getDigest();
        CuratorFramework client = curatorRepository.connect(conf.getZkAddressList(), namespace, digest);
        if (client == null) {
            log.warn("fail to connect to zk during create NamespaceShardingManager");
            return;
        }
        NamespaceShardingManager namespaceShardingManager = null;
        try {
            namespaceShardingManager = new NamespaceShardingManager(client, namespace, generateShardingLeadershipHostValue(), reportAlarmService, updateJobConfigService);
            namespaceShardingManager.start();
            if (namespaceShardingListenerManagerMap.putIfAbsent(nns, namespaceShardingManager) != null) {
                // 已经存在,则关闭当前的client
                try {
                    namespaceShardingManager.stopWithCurator();
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            } else {
                log.info("Done starting NamespaceShardingManager {}", nns);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            if (namespaceShardingManager != null) {
                try {
                    namespaceShardingManager.stop();
                } catch (Exception e2) {
                    log.error(e2.getMessage(), e2);
                }
            }
            client.close();
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) NamespaceShardingManager(com.vip.saturn.job.sharding.NamespaceShardingManager) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)

Aggregations

NamespaceShardingManager (com.vip.saturn.job.sharding.NamespaceShardingManager)2 RegistryCenterConfiguration (com.vip.saturn.job.console.domain.RegistryCenterConfiguration)1 ZkCluster (com.vip.saturn.job.console.domain.ZkCluster)1 SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)1 SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)1 Entry (java.util.Map.Entry)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1