Search in sources :

Example 1 with NodeServer

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

the class CanalClusterController method clustersAndServers.

@GetMapping(value = "/clustersAndServers")
public BaseModel<List<?>> clustersAndServers(@PathVariable String env) {
    List<CanalCluster> clusters = canalClusterServic.findList(new CanalCluster());
    JSONObject group = new JSONObject();
    group.put("label", "集群");
    JSONArray jsonArray = new JSONArray();
    clusters.forEach(cluster -> {
        JSONObject item = new JSONObject();
        item.put("label", cluster.getName());
        item.put("value", "cluster:" + cluster.getId());
        jsonArray.add(item);
    });
    group.put("options", jsonArray);
    NodeServer param = new NodeServer();
    param.setClusterId(-1L);
    // 取所有standalone的节点
    List<NodeServer> servers = nodeServerService.findAll(param);
    JSONObject group2 = new JSONObject();
    group2.put("label", "单机主机");
    JSONArray jsonArray2 = new JSONArray();
    servers.forEach(server -> {
        JSONObject item = new JSONObject();
        item.put("label", server.getName());
        item.put("value", "server:" + server.getId());
        jsonArray2.add(item);
    });
    group2.put("options", jsonArray2);
    List<JSONObject> result = new ArrayList<>();
    result.add(group);
    result.add(group2);
    return BaseModel.getInstance(result);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) CanalCluster(com.alibaba.otter.canal.admin.model.CanalCluster) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with NodeServer

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

the class NodeServerServiceImpl method findList.

public Pager<NodeServer> findList(NodeServer nodeServer, Pager<NodeServer> pager) {
    Query<NodeServer> query = getBaseQuery(nodeServer);
    Query<NodeServer> queryCnt = query.copy();
    int count = queryCnt.findCount();
    pager.setCount((long) count);
    List<NodeServer> nodeServers = query.order().asc("id").setFirstRow(pager.getOffset().intValue()).setMaxRows(pager.getSize()).findList();
    pager.setItems(nodeServers);
    if (nodeServers.isEmpty()) {
        return pager;
    }
    List<Future<Boolean>> futures = new ArrayList<>(nodeServers.size());
    // get all nodes status
    for (NodeServer ns : nodeServers) {
        futures.add(Threads.executorService.submit(() -> {
            boolean status = SimpleAdminConnectors.execute(ns.getIp(), ns.getAdminPort(), AdminConnector::check);
            ns.setStatus(status ? "1" : "0");
            return !status;
        }));
    }
    for (Future<Boolean> f : futures) {
        try {
            f.get(3, TimeUnit.SECONDS);
        } catch (InterruptedException | ExecutionException e) {
        // ignore
        } catch (TimeoutException e) {
            break;
        }
    }
    return pager;
}
Also used : ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with NodeServer

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

the class NodeServerServiceImpl method delete.

public void delete(Long id) {
    NodeServer nodeServer = NodeServer.find.byId(id);
    if (nodeServer != null) {
        // 判断是否存在实例
        int cnt = CanalInstanceConfig.find.query().where().eq("serverId", nodeServer.getId()).findCount();
        if (cnt > 0) {
            throw new ServiceException("当前Server下存在Instance配置, 无法删除");
        }
        // 同时删除配置
        CanalConfig canalConfig = CanalConfig.find.query().where().eq("serverId", id).findOne();
        if (canalConfig != null) {
            canalConfig.delete();
        }
        nodeServer.delete();
    }
}
Also used : CanalConfig(com.alibaba.otter.canal.admin.model.CanalConfig) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer)

Example 4 with NodeServer

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

the class CanalInstanceServiceImpl method remoteOperation.

public boolean remoteOperation(Long id, Long nodeId, String option) {
    NodeServer nodeServer = null;
    if ("start".equals(option)) {
        if (nodeId != null) {
            nodeServer = NodeServer.find.byId(nodeId);
        } else {
            nodeServer = NodeServer.find.query().findOne();
        }
    } else {
        if (nodeId == null) {
            return false;
        }
        nodeServer = NodeServer.find.byId(nodeId);
    }
    if (nodeServer == null) {
        return false;
    }
    CanalInstanceConfig canalInstanceConfig = CanalInstanceConfig.find.byId(id);
    if (canalInstanceConfig == null) {
        return false;
    }
    Boolean result = null;
    if ("start".equals(option)) {
        if (nodeServer.getClusterId() == null) {
            // 非集群模式
            return instanceOperation(id, "start");
        } else {
            throw new ServiceException("集群模式不允许指定server启动");
        }
    } else if ("stop".equals(option)) {
        if (nodeServer.getClusterId() != null) {
            // 集群模式,通知主动释放
            result = SimpleAdminConnectors.execute(nodeServer.getIp(), nodeServer.getAdminPort(), adminConnector -> adminConnector.releaseInstance(canalInstanceConfig.getName()));
        } else {
            // 非集群模式下直接将状态置为0
            return instanceOperation(id, "stop");
        }
    } else {
        return false;
    }
    if (result == null) {
        result = false;
    }
    return result;
}
Also used : ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) CanalInstanceConfig(com.alibaba.otter.canal.admin.model.CanalInstanceConfig) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer)

Example 5 with NodeServer

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

the class CanalInstanceServiceImpl method findActiveInstanceByServerId.

/**
 * 通过Server id获取当前Server下所有运行的Instance
 *
 * @param serverId server id
 */
public List<CanalInstanceConfig> findActiveInstanceByServerId(Long serverId) {
    NodeServer nodeServer = NodeServer.find.byId(serverId);
    if (nodeServer == null) {
        return null;
    }
    String runningInstances = SimpleAdminConnectors.execute(nodeServer.getIp(), nodeServer.getAdminPort(), AdminConnector::getRunningInstances);
    if (runningInstances == null) {
        return null;
    }
    String[] instances = runningInstances.split(",");
    Object[] obj = Lists.newArrayList(instances).toArray();
    // 单机模式和集群模式区分处理
    if (nodeServer.getClusterId() != null) {
        // 集群模式
        List<CanalInstanceConfig> list = CanalInstanceConfig.find.query().setDisableLazyLoading(true).select("clusterId, serverId, name, modifiedTime").where().in("name", obj).findList();
        list.forEach(config -> config.setRunningStatus("1"));
        // 集群模式直接返回当前运行的Instances
        return list;
    } else {
        // 单机模式
        // 当前Server所配置的所有Instance
        List<CanalInstanceConfig> list = CanalInstanceConfig.find.query().setDisableLazyLoading(true).select("clusterId, serverId, name, modifiedTime").where().eq("serverId", serverId).findList();
        List<String> instanceList = Arrays.asList(instances);
        list.forEach(config -> {
            if (instanceList.contains(config.getName())) {
                config.setRunningStatus("1");
            }
        });
        return list;
    }
}
Also used : AdminConnector(com.alibaba.otter.canal.admin.connector.AdminConnector) CanalInstanceConfig(com.alibaba.otter.canal.admin.model.CanalInstanceConfig) 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