Search in sources :

Example 6 with RegistryCenterClient

use of com.vip.saturn.job.console.domain.RegistryCenterClient 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)

Example 7 with RegistryCenterClient

use of com.vip.saturn.job.console.domain.RegistryCenterClient in project Saturn by vipshop.

the class ZkDBDiffServiceImpl method initCuratorClient.

private CuratorRepository.CuratorFrameworkOp initCuratorClient(String namespace) {
    RegistryCenterConfiguration registryCenterConfiguration = registryCenterService.findConfigByNamespace(namespace);
    if (registryCenterConfiguration == null) {
        String errMsg = String.format(NAMESPACE_NOT_EXIST_TEMPLATE, namespace);
        log.warn(ERR_MSG_SKIP_DIFF, namespace, errMsg);
        return null;
    }
    RegistryCenterClient registryCenterClient = registryCenterService.connectByNamespace(namespace);
    if (registryCenterClient != null && registryCenterClient.isConnected()) {
        return curatorRepository.newCuratorFrameworkOp(registryCenterClient.getCuratorClient());
    }
    log.warn(ERR_MSG_SKIP_DIFF, namespace, "fail to connect to zk.");
    return null;
}
Also used : RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) RegistryCenterClient(com.vip.saturn.job.console.domain.RegistryCenterClient)

Example 8 with RegistryCenterClient

use of com.vip.saturn.job.console.domain.RegistryCenterClient in project Saturn by vipshop.

the class CuratorSessionClientInterceptor method preHandle.

@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
    String nns = request.getParameter(AbstractController.REQUEST_NAMESPACE_PARAM);
    if (!Strings.isNullOrEmpty(nns)) {
        RegistryCenterClient client = registryCenterService.connect(nns);
        ThreadLocalCuratorClient.setCuratorClient(client.getCuratorClient());
        RegistryCenterConfiguration conf = registryCenterService.findConfig(client.getNameAndNamespace());
        if (conf == null) {
            response.sendRedirect(request.getContextPath() + "/registry_center_page");
            return false;
        }
        request.getSession().setAttribute(SessionAttributeKeys.ACTIVATED_CONFIG_SESSION_KEY, conf);
        request.getSession().setAttribute(SessionAttributeKeys.CURRENT_ZK_CLUSTER_KEY, conf.getZkClusterKey());
        return true;
    }
    RegistryCenterConfiguration reg = (RegistryCenterConfiguration) request.getSession().getAttribute(SessionAttributeKeys.ACTIVATED_CONFIG_SESSION_KEY);
    if (reg == null) {
        response.sendRedirect(request.getContextPath() + "/registry_center_page");
        return false;
    }
    RegistryCenterClient client = registryCenterService.getCuratorByNameAndNamespace(reg.getNameAndNamespace());
    if (null == client || !client.isConnected()) {
        response.sendRedirect(request.getContextPath() + "/registry_center_page");
        return false;
    }
    ThreadLocalCuratorClient.setCuratorClient(client.getCuratorClient());
    return true;
}
Also used : RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) RegistryCenterClient(com.vip.saturn.job.console.domain.RegistryCenterClient)

Example 9 with RegistryCenterClient

use of com.vip.saturn.job.console.domain.RegistryCenterClient in project Saturn by vipshop.

the class RestApiServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    when(registryCenterService.findConfigByNamespace(anyString())).thenReturn(new RegistryCenterConfiguration());
    RegistryCenterClient registryCenterClient = new RegistryCenterClient();
    registryCenterClient.setCuratorClient(curatorFramework);
    registryCenterClient.setConnected(true);
    when(registryCenterService.connectByNamespace(anyString())).thenReturn(registryCenterClient);
    when(curatorRepository.newCuratorFrameworkOp(any(CuratorFramework.class))).thenReturn(curatorFrameworkOp);
    when(curatorFrameworkOp.checkExists(JobNodePath.getJobNodePath(anyString()))).thenReturn(true);
}
Also used : RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) CuratorFramework(org.apache.curator.framework.CuratorFramework) RegistryCenterClient(com.vip.saturn.job.console.domain.RegistryCenterClient) Before(org.junit.Before)

Example 10 with RegistryCenterClient

use of com.vip.saturn.job.console.domain.RegistryCenterClient in project Saturn by vipshop.

the class ReuseUtils method reuse.

public static <T> T reuse(String namespace, RegistryCenterService registryCenterService, CuratorRepository curatorRepository, ReuseCallBack<T> callBack) throws SaturnJobConsoleException {
    try {
        RegistryCenterConfiguration registryCenterConfiguration = registryCenterService.findConfigByNamespace(namespace);
        if (registryCenterConfiguration == null) {
            throw new SaturnJobConsoleException(ERROR_CODE_NOT_EXISTED, String.format(NAMESPACE_NOT_EXIST_TEMPLATE, namespace));
        }
        RegistryCenterClient registryCenterClient = registryCenterService.connectByNamespace(namespace);
        if (registryCenterClient != null && registryCenterClient.isConnected()) {
            CuratorFramework curatorClient = registryCenterClient.getCuratorClient();
            CuratorRepository.CuratorFrameworkOp curatorFrameworkOp = curatorRepository.newCuratorFrameworkOp(curatorClient);
            ThreadLocalCuratorClient.setCuratorClient(curatorClient);
            return callBack.call(curatorFrameworkOp);
        } else {
            throw new SaturnJobConsoleException("Connect zookeeper failed");
        }
    } catch (SaturnJobConsoleException e) {
        throw e;
    } catch (Throwable t) {
        log.error(t.getMessage(), t);
        throw new SaturnJobConsoleException(t);
    } finally {
        ThreadLocalCuratorClient.clear();
    }
}
Also used : RegistryCenterConfiguration(com.vip.saturn.job.console.domain.RegistryCenterConfiguration) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorRepository(com.vip.saturn.job.console.repository.zookeeper.CuratorRepository) SaturnJobConsoleException(com.vip.saturn.job.console.exception.SaturnJobConsoleException) RegistryCenterClient(com.vip.saturn.job.console.domain.RegistryCenterClient)

Aggregations

RegistryCenterClient (com.vip.saturn.job.console.domain.RegistryCenterClient)10 RegistryCenterConfiguration (com.vip.saturn.job.console.domain.RegistryCenterConfiguration)9 CuratorFramework (org.apache.curator.framework.CuratorFramework)6 SaturnJobConsoleException (com.vip.saturn.job.console.exception.SaturnJobConsoleException)3 CuratorRepository (com.vip.saturn.job.console.repository.zookeeper.CuratorRepository)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 SaturnJobConsoleHttpException (com.vip.saturn.job.console.exception.SaturnJobConsoleHttpException)1 Before (org.junit.Before)1