Search in sources :

Example 6 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class CanalClusterServiceImpl method delete.

public void delete(Long id) {
    // 判断集群下是否存在server信息
    int serverCnt = NodeServer.find.query().where().eq("clusterId", id).findCount();
    if (serverCnt > 0) {
        throw new ServiceException("当前集群下存在Server, 无法删除");
    }
    // 判断集群下是否存在instance信息
    int instanceCnt = CanalInstanceConfig.find.query().where().eq("clusterId", id).findCount();
    if (instanceCnt > 0) {
        throw new ServiceException("当前集群下存在Instance配置,无法删除");
    }
    CanalCluster canalCluster = CanalCluster.find.byId(id);
    if (canalCluster != null) {
        canalCluster.delete();
    }
}
Also used : ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) CanalCluster(com.alibaba.otter.canal.admin.model.CanalCluster)

Example 7 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException 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 8 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class PollingConfigServiceImpl method getChangedConfig.

public CanalConfig getChangedConfig(String ip, Integer port, String md5) {
    NodeServer server = NodeServer.find.query().where().eq("ip", ip).eq("adminPort", port).findOne();
    if (server == null) {
        return null;
    }
    CanalConfig canalConfig;
    if (server.getClusterId() != null) {
        // 集群模式
        canalConfig = CanalConfig.find.query().where().eq("clusterId", server.getClusterId()).findOne();
    } else {
        // 单机模式
        canalConfig = CanalConfig.find.query().where().eq("serverId", server.getId()).findOne();
    }
    if (canalConfig == null) {
        throw new ServiceException("canal.properties config is empty");
    }
    if (!canalConfig.getContentMd5().equals(md5)) {
        // 内容发生变化
        return canalConfig;
    }
    return null;
}
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 9 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class SimpleAdminConnector method doInstanceAdmin.

private String doInstanceAdmin(String destination, String action) {
    try {
        writeWithHeader(Packet.newBuilder().setType(PacketType.INSTANCE).setBody(InstanceAdmin.newBuilder().setDestination(destination).setAction(action).build().toByteString()).build().toByteArray());
        Packet p = Packet.parseFrom(readNextPacket());
        Ack ack = Ack.parseFrom(p.getBody());
        if (ack.getCode() > 0) {
            throw new ServiceException("failed to subscribe with reason: " + ack.getMessage());
        }
        return ack.getMessage();
    } catch (IOException e) {
        throw new ServiceException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.AdminPacket.Packet) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) Ack(com.alibaba.otter.canal.protocol.AdminPacket.Ack) IOException(java.io.IOException)

Example 10 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class CanalConfigServiceImpl method getCanalConfig.

public CanalConfig getCanalConfig(Long clusterId, Long serverId) {
    CanalConfig config = null;
    if (clusterId != null && clusterId != 0) {
        config = CanalConfig.find.query().where().eq("clusterId", clusterId).findOne();
    } else if (serverId != null && serverId != 0) {
        config = CanalConfig.find.query().where().eq("serverId", serverId).findOne();
        if (config == null) {
            NodeServer nodeServer = NodeServer.find.byId(serverId);
            if (nodeServer != null) {
                Long cid = nodeServer.getClusterId();
                if (cid != null) {
                    config = CanalConfig.find.query().where().eq("clusterId", cid).findOne();
                }
            }
        }
    } else {
        throw new ServiceException("clusterId and serverId are all empty");
    }
    if (config == null) {
        config = new CanalConfig();
        config.setName(CANAL_GLOBAL_CONFIG);
        return config;
    }
    return config;
}
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)

Aggregations

ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)14 NodeServer (com.alibaba.otter.canal.admin.model.NodeServer)5 CanalConfig (com.alibaba.otter.canal.admin.model.CanalConfig)4 Ack (com.alibaba.otter.canal.protocol.AdminPacket.Ack)4 Packet (com.alibaba.otter.canal.protocol.AdminPacket.Packet)4 IOException (java.io.IOException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 CanalCluster (com.alibaba.otter.canal.admin.model.CanalCluster)2 User (com.alibaba.otter.canal.admin.model.User)2 BaseModel (com.alibaba.otter.canal.admin.model.BaseModel)1 CanalInstanceConfig (com.alibaba.otter.canal.admin.model.CanalInstanceConfig)1 ClientAuth (com.alibaba.otter.canal.protocol.AdminPacket.ClientAuth)1 Handshake (com.alibaba.otter.canal.protocol.AdminPacket.Handshake)1 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)1 ByteString (com.google.protobuf.ByteString)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1