Search in sources :

Example 1 with NamespaceInfo

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

the class RestApiServiceImpl method constructNamespaceInfo.

private NamespaceInfo constructNamespaceInfo(NamespaceDomainInfo namespaceDomainInfo) {
    NamespaceInfo namespaceInfo = new NamespaceInfo();
    namespaceInfo.setCreatedBy(NAMESPACE_CREATOR_NAME);
    namespaceInfo.setCreateTime(new Date());
    namespaceInfo.setIsDeleted(0);
    namespaceInfo.setLastUpdatedBy(NAMESPACE_CREATOR_NAME);
    namespaceInfo.setLastUpdateTime(new Date());
    namespaceInfo.setNamespace(namespaceDomainInfo.getNamespace());
    namespaceInfo.setContent(namespaceDomainInfo.getContent());
    return namespaceInfo;
}
Also used : NamespaceInfo(com.vip.saturn.job.console.mybatis.entity.NamespaceInfo) Date(java.util.Date)

Example 2 with NamespaceInfo

use of com.vip.saturn.job.console.mybatis.entity.NamespaceInfo 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 3 with NamespaceInfo

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

the class RestApiServiceImpl method createNamespace.

@Transactional(rollbackFor = { Exception.class })
@Override
public void createNamespace(NamespaceDomainInfo namespaceDomainInfo) throws SaturnJobConsoleException {
    String namespace = namespaceDomainInfo.getNamespace();
    String zkClusterKey = namespaceDomainInfo.getZkCluster();
    ZkCluster currentCluster = registryCenterService.getZkCluster(zkClusterKey);
    if (currentCluster == null) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), String.format(ERR_MSG_TEMPLATE_FAIL_TO_CREATE, namespace, "not found zkcluster" + zkClusterKey));
    }
    if (checkNamespaceExists(namespace)) {
        throw new SaturnJobConsoleHttpException(HttpStatus.BAD_REQUEST.value(), String.format(ERR_MSG_NS_ALREADY_EXIST, namespace));
    }
    try {
        // 创建 namespaceInfo
        NamespaceInfo namespaceInfo = constructNamespaceInfo(namespaceDomainInfo);
        namespaceInfoService.create(namespaceInfo);
        // 创建 zkcluster 和 namespaceInfo 关系
        namespaceZkClusterMapping4SqlService.insert(namespace, "", zkClusterKey, NAMESPACE_CREATOR_NAME);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), String.format(ERR_MSG_TEMPLATE_FAIL_TO_CREATE, namespace, e.getMessage()));
    }
}
Also used : ZkCluster(com.vip.saturn.job.console.domain.ZkCluster) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) NamespaceInfo(com.vip.saturn.job.console.mybatis.entity.NamespaceInfo) ReportAlarmException(com.vip.saturn.job.integrate.exception.ReportAlarmException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with NamespaceInfo

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

the class RegistryCenterServiceImpl method constructNamespaceInfo.

private NamespaceInfo constructNamespaceInfo(NamespaceDomainInfo namespaceDomainInfo) {
    NamespaceInfo namespaceInfo = new NamespaceInfo();
    namespaceInfo.setCreatedBy(NAMESPACE_CREATOR_NAME);
    namespaceInfo.setCreateTime(new Date());
    namespaceInfo.setIsDeleted(0);
    namespaceInfo.setLastUpdatedBy(NAMESPACE_CREATOR_NAME);
    namespaceInfo.setLastUpdateTime(new Date());
    namespaceInfo.setNamespace(namespaceDomainInfo.getNamespace());
    namespaceInfo.setContent(namespaceDomainInfo.getContent());
    namespaceInfo.setBusId("");
    return namespaceInfo;
}
Also used : NamespaceInfo(com.vip.saturn.job.console.mybatis.entity.NamespaceInfo)

Example 5 with NamespaceInfo

use of com.vip.saturn.job.console.mybatis.entity.NamespaceInfo 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

NamespaceInfo (com.vip.saturn.job.console.mybatis.entity.NamespaceInfo)8 SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)5 SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)5 RegistryCenterConfiguration (com.vip.saturn.job.console.domain.RegistryCenterConfiguration)2 ZkCluster (com.vip.saturn.job.console.domain.ZkCluster)2 NamespaceZkClusterMapping (com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping)2 ReportAlarmException (com.vip.saturn.job.integrate.exception.ReportAlarmException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 CuratorRepository (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository)1 Date (java.util.Date)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1