Search in sources :

Example 6 with CanalInstanceConfig

use of com.alibaba.otter.canal.admin.model.CanalInstanceConfig 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)

Aggregations

CanalInstanceConfig (com.alibaba.otter.canal.admin.model.CanalInstanceConfig)6 NodeServer (com.alibaba.otter.canal.admin.model.NodeServer)5 ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)1 AdminConnector (com.alibaba.otter.canal.admin.connector.AdminConnector)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1