Search in sources :

Example 11 with NodeServer

use of com.alibaba.otter.canal.admin.model.NodeServer in project canal by alibaba.

the class CanalInstanceServiceImpl method findList.

public Pager<CanalInstanceConfig> findList(CanalInstanceConfig canalInstanceConfig, Pager<CanalInstanceConfig> pager) {
    Query<CanalInstanceConfig> query = CanalInstanceConfig.find.query().setDisableLazyLoading(true).select("clusterId, serverId, name, modifiedTime").fetch("canalCluster", "name").fetch("nodeServer", "name,ip,adminPort");
    if (canalInstanceConfig != null) {
        if (StringUtils.isNotEmpty(canalInstanceConfig.getName())) {
            query.where().like("name", "%" + canalInstanceConfig.getName() + "%");
        }
        if (StringUtils.isNotEmpty(canalInstanceConfig.getClusterServerId())) {
            if (canalInstanceConfig.getClusterServerId().startsWith("cluster:")) {
                query.where().eq("clusterId", Long.parseLong(canalInstanceConfig.getClusterServerId().substring(8)));
            } else if (canalInstanceConfig.getClusterServerId().startsWith("server:")) {
                query.where().eq("serverId", Long.parseLong(canalInstanceConfig.getClusterServerId().substring(7)));
            }
        }
    }
    Query<CanalInstanceConfig> queryCnt = query.copy();
    int count = queryCnt.findCount();
    pager.setCount((long) count);
    query.setFirstRow(pager.getOffset().intValue()).setMaxRows(pager.getSize()).order().asc("id");
    List<CanalInstanceConfig> canalInstanceConfigs = query.findList();
    pager.setItems(canalInstanceConfigs);
    if (canalInstanceConfigs.isEmpty()) {
        return pager;
    }
    // check all canal instances running status
    List<Future<Void>> futures = new ArrayList<>(canalInstanceConfigs.size());
    for (CanalInstanceConfig canalInstanceConfig1 : canalInstanceConfigs) {
        futures.add(Threads.executorService.submit(() -> {
            List<NodeServer> nodeServers;
            if (canalInstanceConfig1.getClusterId() != null) {
                // 集群模式
                nodeServers = NodeServer.find.query().where().eq("clusterId", canalInstanceConfig1.getClusterId()).findList();
            } else if (canalInstanceConfig1.getServerId() != null) {
                // 单机模式
                nodeServers = Collections.singletonList(canalInstanceConfig1.getNodeServer());
            } else {
                return null;
            }
            for (NodeServer nodeServer : nodeServers) {
                String runningInstances = SimpleAdminConnectors.execute(nodeServer.getIp(), nodeServer.getAdminPort(), AdminConnector::getRunningInstances);
                if (runningInstances == null) {
                    continue;
                }
                String[] instances = runningInstances.split(",");
                for (String instance : instances) {
                    if (instance.equals(canalInstanceConfig1.getName())) {
                        // 集群模式下server对象为空
                        if (canalInstanceConfig1.getNodeServer() == null) {
                            canalInstanceConfig1.setNodeServer(nodeServer);
                        }
                        canalInstanceConfig1.setRunningStatus("1");
                        break;
                    }
                }
            }
            return null;
        }));
    }
    for (Future<Void> f : futures) {
        try {
            f.get(3, TimeUnit.SECONDS);
        } catch (InterruptedException | ExecutionException e) {
        // ignore
        } catch (TimeoutException e) {
            break;
        }
    }
    return pager;
}
Also used : CanalInstanceConfig(com.alibaba.otter.canal.admin.model.CanalInstanceConfig) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with NodeServer

use of com.alibaba.otter.canal.admin.model.NodeServer in project canal by alibaba.

the class PollingConfigServiceImpl method autoRegister.

public boolean autoRegister(String ip, Integer adminPort, String cluster, String name) {
    NodeServer server = NodeServer.find.query().where().eq("ip", ip).eq("adminPort", adminPort).findOne();
    if (server == null) {
        server = new NodeServer();
        server.setName(Optional.ofNullable(name).orElse(ip));
        server.setIp(ip);
        server.setAdminPort(adminPort);
        server.setTcpPort(adminPort + 1);
        server.setMetricPort(adminPort + 2);
        if (StringUtils.isNotEmpty(cluster)) {
            CanalCluster clusterConfig = CanalCluster.find.query().where().eq("name", cluster).findOne();
            if (clusterConfig == null) {
                throw new ServiceException("auto cluster : " + cluster + " is not found.");
            }
            server.setClusterId(clusterConfig.getId());
        }
        nodeServerService.save(server);
    }
    return true;
}
Also used : ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) CanalCluster(com.alibaba.otter.canal.admin.model.CanalCluster) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer)

Aggregations

NodeServer (com.alibaba.otter.canal.admin.model.NodeServer)12 ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)5 CanalInstanceConfig (com.alibaba.otter.canal.admin.model.CanalInstanceConfig)5 CanalConfig (com.alibaba.otter.canal.admin.model.CanalConfig)3 ArrayList (java.util.ArrayList)3 AdminConnector (com.alibaba.otter.canal.admin.connector.AdminConnector)2 CanalCluster (com.alibaba.otter.canal.admin.model.CanalCluster)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 TimeoutException (java.util.concurrent.TimeoutException)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1