Search in sources :

Example 1 with CanalInstanceConfig

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

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

Example 3 with CanalInstanceConfig

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

the class PollingConfigServiceImpl method getInstancesConfig.

public CanalInstanceConfig getInstancesConfig(String ip, Integer port, String md5) {
    NodeServer server = NodeServer.find.query().where().eq("ip", ip).eq("adminPort", port).findOne();
    if (server == null) {
        return null;
    }
    List<CanalInstanceConfig> canalInstanceConfigs;
    if (server.getClusterId() != null) {
        // 集群模式
        canalInstanceConfigs = CanalInstanceConfig.find.query().where().eq("status", "1").eq("clusterId", server.getClusterId()).findList();
    } else {
        // 单机模式
        canalInstanceConfigs = CanalInstanceConfig.find.query().where().eq("status", "1").eq("serverId", server.getId()).findList();
    }
    CanalInstanceConfig canalInstanceConfig = new CanalInstanceConfig();
    List<String> instances = canalInstanceConfigs.stream().map(CanalInstanceConfig::getName).collect(Collectors.toList());
    String data = Joiner.on(',').join(instances);
    canalInstanceConfig.setContent(data);
    if (!StringUtils.isEmpty(md5)) {
        try {
            String newMd5 = SecurityUtil.md5String(canalInstanceConfig.getContent());
            if (StringUtils.equals(md5, newMd5)) {
                canalInstanceConfig.setContent(null);
            }
        } catch (NoSuchAlgorithmException e) {
        // ignore
        }
    }
    return canalInstanceConfig;
}
Also used : CanalInstanceConfig(com.alibaba.otter.canal.admin.model.CanalInstanceConfig) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 4 with CanalInstanceConfig

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

the class CanalInstanceServiceImpl method instanceOperation.

public boolean instanceOperation(Long id, String option) {
    CanalInstanceConfig canalInstanceConfig = CanalInstanceConfig.find.byId(id);
    if (canalInstanceConfig == null) {
        return false;
    }
    if ("stop".equals(option)) {
        canalInstanceConfig.setStatus("0");
        canalInstanceConfig.update("status");
    } else if ("start".equals(option)) {
        canalInstanceConfig.setStatus("1");
        canalInstanceConfig.update("status");
    } else {
        return false;
    }
    return true;
}
Also used : CanalInstanceConfig(com.alibaba.otter.canal.admin.model.CanalInstanceConfig)

Example 5 with CanalInstanceConfig

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

the class CanalInstanceServiceImpl method remoteInstanceLog.

public Map<String, String> remoteInstanceLog(Long id, Long nodeId) {
    Map<String, String> result = new HashMap<>();
    NodeServer nodeServer = NodeServer.find.byId(nodeId);
    if (nodeServer == null) {
        return result;
    }
    CanalInstanceConfig canalInstanceConfig = CanalInstanceConfig.find.byId(id);
    if (canalInstanceConfig == null) {
        return result;
    }
    String log = SimpleAdminConnectors.execute(nodeServer.getIp(), nodeServer.getAdminPort(), adminConnector -> adminConnector.instanceLog(canalInstanceConfig.getName(), null, 100));
    result.put("instance", canalInstanceConfig.getName());
    result.put("log", log);
    return result;
}
Also used : HashMap(java.util.HashMap) CanalInstanceConfig(com.alibaba.otter.canal.admin.model.CanalInstanceConfig) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer)

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