Search in sources :

Example 1 with NamespaceZkClusterMapping

use of com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping in project Saturn by vipshop.

the class RegistryCenterServiceImpl method initMoveInNamespace.

private void initMoveInNamespace(List<String> allOnlineNamespacesTemp, String zkClusterKey, ZkCluster zkCluster, List<NamespaceZkClusterMapping> nsZkClusterMappingList, List<RegistryCenterConfiguration> regCenterConfList) {
    if (nsZkClusterMappingList == null || zkCluster.isOffline()) {
        return;
    }
    for (NamespaceZkClusterMapping mapping : nsZkClusterMappingList) {
        String namespace = mapping.getNamespace();
        String name = StringUtils.deleteWhitespace(mapping.getName());
        if (SaturnSelfNodePath.ROOT_NAME.equals(namespace)) {
            log.error("The namespace cannot be {}", SaturnSelfNodePath.ROOT_NAME);
            continue;
        }
        boolean include = false;
        if (regCenterConfList != null) {
            for (RegistryCenterConfiguration conf : regCenterConfList) {
                if (!namespace.equals(conf.getNamespace())) {
                    continue;
                }
                include = true;
                String nnsOld = conf.getNameAndNamespace();
                // update name
                conf.setName(name);
                conf.initNameAndNamespace();
                String nnsNew = conf.getNameAndNamespace();
                if (!nnsOld.equals(nnsNew)) {
                    synchronized (getNnsLock(nnsOld)) {
                        closeNamespace(nnsOld);
                        log.info("closed the namespace info because it's nns is changed, namespace is {}", namespace);
                    }
                }
                break;
            }
        }
        if (!include) {
            CuratorFramework curatorFramework = zkCluster.getCuratorFramework();
            initNamespaceZkNodeIfNecessary(namespace, curatorFramework);
            RegistryCenterConfiguration conf = new RegistryCenterConfiguration(name, namespace, zkCluster.getZkAddr());
            conf.setZkClusterKey(zkClusterKey);
            conf.setVersion(getVersion(namespace, curatorFramework));
            conf.setZkAlias(zkCluster.getZkAlias());
            NamespaceInfo namespaceInfo = getNamespaceInfo(namespace);
            if (namespaceInfo != null) {
                postConstructRegistryCenterConfiguration(conf, namespaceInfo.getContent());
            }
            zkCluster.getRegCenterConfList().add(conf);
        }
        if (!allOnlineNamespacesTemp.contains(namespace)) {
            allOnlineNamespacesTemp.add(namespace);
        }
    }
}
Also used : NamespaceZkClusterMapping(com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping) RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) CuratorFramework(org.apache.curator.framework.CuratorFramework) NamespaceInfo(com.vip.saturn.job.console.mybatis.entity.NamespaceInfo)

Example 2 with NamespaceZkClusterMapping

use of com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping in project Saturn by vipshop.

the class NamespaceZkClusterMapping4SqlServiceImpl method getAllNamespacesOfCluster.

@Transactional(readOnly = true)
@Override
public List<String> getAllNamespacesOfCluster(String zkClusterKey) {
    List<String> result = new ArrayList<String>();
    List<NamespaceZkClusterMapping> namespaceZkClusterMappings = existingAll(namespaceZkClusterMappingRepository.selectByZkClusterKey(zkClusterKey));
    for (NamespaceZkClusterMapping mapping : namespaceZkClusterMappings) {
        result.add(mapping.getNamespace());
    }
    return result;
}
Also used : NamespaceZkClusterMapping(com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with NamespaceZkClusterMapping

use of com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping in project Saturn by vipshop.

the class NamespaceZkClusterMapping4SqlServiceImpl method insert.

@Transactional
@Override
public Integer insert(String namespace, String name, String zkClusterKey, String createdBy) {
    NamespaceZkClusterMapping namespaceZkClusterMapping = namespaceZkClusterMappingRepository.selectByNamespace(namespace);
    boolean insert;
    if (namespaceZkClusterMapping == null) {
        namespaceZkClusterMapping = new NamespaceZkClusterMapping();
        insert = true;
    } else {
        insert = false;
    }
    Date now = new Date();
    namespaceZkClusterMapping.setIsDeleted(0);
    namespaceZkClusterMapping.setCreateTime(now);
    namespaceZkClusterMapping.setCreatedBy(createdBy);
    namespaceZkClusterMapping.setLastUpdateTime(now);
    namespaceZkClusterMapping.setLastUpdatedBy(createdBy);
    namespaceZkClusterMapping.setNamespace(namespace);
    namespaceZkClusterMapping.setName(name);
    namespaceZkClusterMapping.setZkClusterKey(zkClusterKey);
    if (insert) {
        return namespaceZkClusterMappingRepository.insert(namespaceZkClusterMapping);
    } else {
        return namespaceZkClusterMappingRepository.updateAllById(namespaceZkClusterMapping);
    }
}
Also used : NamespaceZkClusterMapping(com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with NamespaceZkClusterMapping

use of com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping in project Saturn by vipshop.

the class RegistryCenterServiceImpl method closeMoveOutNamespace.

private void closeMoveOutNamespace(String zkClusterKey, List<NamespaceZkClusterMapping> nsZkClusterMappingList, List<RegistryCenterConfiguration> regCenterConfList) {
    if (regCenterConfList == null) {
        return;
    }
    Iterator<RegistryCenterConfiguration> regIter = regCenterConfList.iterator();
    while (regIter.hasNext()) {
        RegistryCenterConfiguration conf = regIter.next();
        String namespace = conf.getNamespace();
        String nns = conf.getNameAndNamespace();
        boolean include = false;
        if (nsZkClusterMappingList != null) {
            for (NamespaceZkClusterMapping mapping : nsZkClusterMappingList) {
                if (namespace.equals(mapping.getNamespace())) {
                    include = true;
                    break;
                }
            }
        }
        if (!include) {
            synchronized (getNnsLock(nns)) {
                regIter.remove();
                closeNamespace(nns);
                log.info("closed the moved namespace info, namespace is {}, old zkClusterKey is {}", namespace, zkClusterKey);
            }
        }
    }
}
Also used : NamespaceZkClusterMapping(com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping) RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration)

Example 5 with NamespaceZkClusterMapping

use of com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping in project Saturn by vipshop.

the class RegistryCenterServiceImpl method initOrUpdateNamespace.

private void initOrUpdateNamespace(List<String> allOnlineNamespacesTemp, ZkCluster zkCluster, List<NamespaceZkClusterMapping> nsZkClusterMappingList, List<RegistryCenterConfiguration> regCenterConfList) {
    if (nsZkClusterMappingList == null || zkCluster.isOffline()) {
        return;
    }
    CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = zkCluster.getCuratorFrameworkOp();
    ArrayList<RegistryCenterConfiguration> newRegCenterConfList = new ArrayList<>();
    try {
        for (NamespaceZkClusterMapping mapping : nsZkClusterMappingList) {
            String namespace = mapping.getNamespace();
            // 过滤$SaturnSelf
            if (SaturnSelfNodePath.ROOT_NAME.equals(namespace)) {
                continue;
            }
            // 如果这个标记为true,意味是新域,或者是迁移过来的域
            boolean isNamespaceNotIncludeInOriginRegCenerConfList = false;
            if (isNamespaceNotIncludeInRegCenterConfList(namespace, regCenterConfList)) {
                // 对于新添加的域,需要初始化一些znode
                initNamespaceZkNodeIfNecessary(namespace, curatorFrameworkOp);
                isNamespaceNotIncludeInOriginRegCenerConfList = true;
            }
            try {
                if (isNamespaceNotIncludeInOriginRegCenerConfList || isNewerVersionSaturnNamespace(namespace, curatorFrameworkOp)) {
                    NamespaceInfo namespaceInfo = getNamespaceInfo(namespace);
                    if (namespaceInfo == null && !isNamespaceInfoNotFoundCanBeCalculate()) {
                        log.warn("No info about namespace {}, just skip it.", namespace);
                        continue;
                    }
                    RegistryCenterConfiguration conf = new RegistryCenterConfiguration(mapping.getName(), namespace, zkCluster.getZkAddr());
                    conf.setZkClusterKey(zkCluster.getZkClusterKey());
                    conf.setVersion(getVersion(namespace, curatorFrameworkOp));
                    conf.setZkAlias(zkCluster.getZkAlias());
                    if (namespaceInfo != null) {
                        postConstructRegistryCenterConfiguration(conf, namespaceInfo.getContent());
                    }
                    newRegCenterConfList.add(conf);
                    if (!allOnlineNamespacesTemp.contains(namespace)) {
                        allOnlineNamespacesTemp.add(namespace);
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    // 如果nns有改变,则需要关闭旧的
    if (regCenterConfList != null) {
        for (RegistryCenterConfiguration conf : regCenterConfList) {
            String namespace = conf.getNamespace();
            String nns = conf.getNameAndNamespace();
            for (RegistryCenterConfiguration confNew : newRegCenterConfList) {
                String namespaceNew = confNew.getNamespace();
                if (namespace.equals(namespaceNew)) {
                    String nnsNew = confNew.getNameAndNamespace();
                    if (!nns.equals(nnsNew)) {
                        synchronized (getNnsLock(nns)) {
                            closeNamespace(nns);
                            log.info("closed the namespace info because it's nns is changed, namespace is {}", namespace);
                        }
                    }
                    break;
                }
            }
        }
    }
    int oldSize = regCenterConfList != null ? regCenterConfList.size() : 0;
    if (oldSize != newRegCenterConfList.size()) {
        log.info("Zkcluster [{}] namespace size change from {} to {}", zkCluster.getZkClusterKey(), oldSize, newRegCenterConfList.size());
    }
    zkCluster.setRegCenterConfList(newRegCenterConfList);
}
Also used : NamespaceZkClusterMapping(com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping) RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) NamespaceInfo(com.vip.saturn.job.console.mybatis.entity.NamespaceInfo) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)

Aggregations

NamespaceZkClusterMapping (com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping)11 RegistryCenterConfiguration (com.vip.saturn.job.console.domain.RegistryCenterConfiguration)4 Transactional (org.springframework.transaction.annotation.Transactional)4 SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)3 SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)2 NamespaceInfo (com.vip.saturn.job.console.mybatis.entity.NamespaceInfo)2 ArrayList (java.util.ArrayList)2 NamespaceDomainInfo (com.vip.saturn.job.console.domain.NamespaceDomainInfo)1 NamespaceZkClusterMappingVo (com.vip.saturn.job.console.domain.NamespaceZkClusterMappingVo)1 ZkCluster (com.vip.saturn.job.console.domain.ZkCluster)1 CuratorRepository (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1