Search in sources :

Example 1 with ZkClusterInfo

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

the class RegistryCenterServiceImpl method getZkClusterInfo.

private Map<String, ZkCluster> getZkClusterInfo() {
    LinkedHashMap<String, ZkCluster> newClusterMap = new LinkedHashMap<>();
    List<ZkClusterInfo> allZkClusterInfoList = zkClusterInfoService.getAllZkClusterInfo();
    if (allZkClusterInfoList != null) {
        for (ZkClusterInfo zkClusterInfo : allZkClusterInfoList) {
            ZkCluster zkCluster = new ZkCluster();
            zkCluster.setZkClusterKey(zkClusterInfo.getZkClusterKey());
            zkCluster.setZkAlias(zkClusterInfo.getAlias());
            zkCluster.setZkAddr(zkClusterInfo.getConnectString());
            newClusterMap.put(zkClusterInfo.getZkClusterKey(), zkCluster);
        }
    }
    return newClusterMap;
}
Also used : ZkCluster(com.vip.saturn.job.console.domain.ZkCluster) ZkClusterInfo(com.vip.saturn.job.console.mybatis.entity.ZkClusterInfo)

Example 2 with ZkClusterInfo

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

the class NamespaceZkClusterMappingServiceImpl method initNamespaceZkClusterMapping.

@Override
public void initNamespaceZkClusterMapping(String createdBy) throws SaturnJobConsoleException {
    try {
        List<ZkClusterInfo> allZkClusterInfo = zkClusterInfoService.getAllZkClusterInfo();
        if (allZkClusterInfo != null) {
            for (ZkClusterInfo zkClusterInfo : allZkClusterInfo) {
                String zkClusterKey = zkClusterInfo.getZkClusterKey();
                String connectString = zkClusterInfo.getConnectString();
                CuratorFramework curatorFramework = null;
                CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = registryCenterService.connectOnly(connectString, null);
                if (curatorFrameworkOp != null) {
                    curatorFramework = curatorFrameworkOp.getCuratorFramework();
                }
                if (curatorFramework != null) {
                    // not offline
                    try {
                        List<String> namespaces = curatorFramework.getChildren().forPath("/");
                        if (namespaces != null) {
                            for (String namespace : namespaces) {
                                if (registryCenterService.namespaceIsCorrect(namespace, curatorFramework)) {
                                    namespaceZkclusterMapping4SqlService.insert(namespace, "", zkClusterKey, createdBy);
                                }
                            }
                        }
                    } finally {
                        curatorFramework.close();
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new SaturnJobConsoleException(e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) ZkClusterInfo(com.vip.saturn.job.console.mybatis.entity.ZkClusterInfo) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException)

Example 3 with ZkClusterInfo

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

the class RegistryCenterServiceImpl method refreshRegistryCenter.

private void refreshRegistryCenter() throws IOException {
    LinkedHashMap<String, ZkCluster> newClusterMap = new LinkedHashMap<>();
    // 获取新的zkClusters
    List<ZkClusterInfo> allZkClusterInfo = zkClusterInfoService.getAllZkClusterInfo();
    if (allZkClusterInfo != null) {
        for (ZkClusterInfo zkClusterInfo : allZkClusterInfo) {
            ZkCluster zkCluster = new ZkCluster();
            zkCluster.setZkClusterKey(zkClusterInfo.getZkClusterKey());
            zkCluster.setZkAlias(zkClusterInfo.getAlias());
            zkCluster.setZkAddr(zkClusterInfo.getConnectString());
            newClusterMap.put(zkClusterInfo.getZkClusterKey(), zkCluster);
        }
    }
    // 对比旧的。不包含的,关闭操作;包含的,检查属性是否相同,如果相同,则直接赋值,否则,关闭旧的
    Iterator<Entry<String, ZkCluster>> iterator = zkClusterMap.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, ZkCluster> next = iterator.next();
        String zkClusterKey = next.getKey();
        ZkCluster zkCluster = next.getValue();
        if (!newClusterMap.containsKey(zkClusterKey)) {
            iterator.remove();
            closeZkCluster(zkCluster);
        } else {
            ZkCluster newZkCluster = newClusterMap.get(zkClusterKey);
            if (zkCluster.equals(newZkCluster)) {
                newClusterMap.put(zkClusterKey, zkCluster);
            } else {
                iterator.remove();
                closeZkCluster(zkCluster);
            }
        }
    }
    // 完善curatorFramework。如果没有,则新建
    Iterator<Entry<String, ZkCluster>> iterator2 = newClusterMap.entrySet().iterator();
    while (iterator2.hasNext()) {
        Entry<String, ZkCluster> next = iterator2.next();
        ZkCluster zkCluster = next.getValue();
        CuratorFramework curatorFramework = zkCluster.getCuratorFramework();
        if (curatorFramework == null) {
            createNewConnect(zkCluster);
        }
    }
    // 完善ZkCluster中的注册中心信息,关闭迁移了的域,新建迁移过来的域
    Iterator<Entry<String, ZkCluster>> iterator3 = newClusterMap.entrySet().iterator();
    while (iterator3.hasNext()) {
        Entry<String, ZkCluster> next = iterator3.next();
        String zkClusterKey = next.getKey();
        ZkCluster zkCluster = next.getValue();
        List<NamespaceZkClusterMapping> allMappingsOfCluster = namespaceZkClusterMapping4SqlService.getAllMappingsOfCluster(zkClusterKey);
        ArrayList<RegistryCenterConfiguration> regCenterConfList = zkCluster.getRegCenterConfList();
        if (regCenterConfList != null) {
            Iterator<RegistryCenterConfiguration> regIter = regCenterConfList.iterator();
            while (regIter.hasNext()) {
                RegistryCenterConfiguration conf = regIter.next();
                String namespace = conf.getNamespace();
                String nns = conf.getNameAndNamespace();
                boolean include = false;
                if (allMappingsOfCluster != null) {
                    for (NamespaceZkClusterMapping mapping : allMappingsOfCluster) {
                        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);
                    }
                }
            }
        }
        if (allMappingsOfCluster != null && !zkCluster.isOffline()) {
            for (NamespaceZkClusterMapping mapping : allMappingsOfCluster) {
                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())) {
                            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());
                    zkCluster.getRegCenterConfList().add(conf);
                }
            }
        }
    }
    // 直接赋值新的
    zkClusterMap = newClusterMap;
}
Also used : NamespaceZkClusterMapping(com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping) RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) ZkCluster(com.vip.saturn.job.console.domain.ZkCluster) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) CuratorFramework(org.apache.curator.framework.CuratorFramework) ZkClusterInfo(com.vip.saturn.job.console.mybatis.entity.ZkClusterInfo)

Example 4 with ZkClusterInfo

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

the class NamespaceZkClusterMappingRestApiController method discoverZk.

@RequestMapping(value = "/discoverZk", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Object> discoverZk(String namespace, HttpServletRequest request) throws SaturnJobConsoleException {
    HttpHeaders headers = new HttpHeaders();
    try {
        checkMissingParameter("namespace", namespace);
        String zkClusterKey = namespaceZkclusterMapping4SqlService.getZkClusterKey(namespace);
        if (zkClusterKey == null) {
            throw new SaturnJobConsoleHttpException(HttpStatus.NOT_FOUND.value(), "The NamespaceZkClusterMapping is not configured in db for " + namespace);
        }
        ZkClusterInfo zkClusterInfo = zkClusterInfoService.getByClusterKey(zkClusterKey);
        if (zkClusterInfo == null) {
            throw new SaturnJobConsoleHttpException(HttpStatus.NOT_FOUND.value(), "The clusterKey(" + zkClusterKey + ") is not configured in db for " + namespace);
        }
        return new ResponseEntity<Object>(zkClusterInfo.getConnectString(), headers, HttpStatus.OK);
    } catch (SaturnJobConsoleException e) {
        throw e;
    } catch (Exception e) {
        throw new SaturnJobConsoleHttpException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) ZkClusterInfo(com.vip.saturn.job.console.mybatis.entity.ZkClusterInfo) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) SaturnJobConsoleHttpException(com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ZkClusterInfo

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

the class ZkClusterInfoController method getAllZkClusterInfo.

@RequestMapping(value = "getAllZkClusterInfo", method = RequestMethod.GET)
public RequestResult getAllZkClusterInfo(HttpServletRequest request) {
    RequestResult requestResult = new RequestResult();
    try {
        List<ZkClusterInfo> allZkClusterInfo = zkClusterInfoService.getAllZkClusterInfo();
        requestResult.setObj(allZkClusterInfo);
        requestResult.setSuccess(true);
    } catch (Throwable t) {
        LOGGER.error(t.getMessage(), t);
        requestResult.setSuccess(false);
        requestResult.setMessage(t.toString());
    }
    return requestResult;
}
Also used : RequestResult(com.vip.saturn.job.console.domain.RequestResult) ZkClusterInfo(com.vip.saturn.job.console.mybatis.entity.ZkClusterInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ZkClusterInfo (com.vip.saturn.job.console.mybatis.entity.ZkClusterInfo)7 SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ZkCluster (com.vip.saturn.job.console.domain.ZkCluster)2 SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ResponseEntity (org.springframework.http.ResponseEntity)2 Audit (com.vip.saturn.job.console.aop.annotation.Audit)1 RegistryCenterConfiguration (com.vip.saturn.job.console.domain.RegistryCenterConfiguration)1 RequestResult (com.vip.saturn.job.console.domain.RequestResult)1 NamespaceZkClusterMapping (com.vip.saturn.job.console.mybatis.entity.NamespaceZkClusterMapping)1 CuratorRepository (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 Transactional (org.springframework.transaction.annotation.Transactional)1