Search in sources :

Example 1 with UserInterfaceErrorException

use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.

the class ServerManageServiceImpl method deleteServerInfo.

/**
 * {@inheritDoc}
 *
 * @see ServerManageService#deleteServerInfo(String)
 */
@Override
public JSONObject deleteServerInfo(String machineName) throws UserInterfaceErrorException {
    try {
        ConfigJson config = accessor.getByCode(createMachineConfigField(machineName), ConfigJson.class);
        if (config == null) {
            throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_CONFIG_NOT_FOUND);
        } else {
            JSONObject json = parseJson(config.getConfigContent());
            String machineIp = json.getString("machineIp");
            accessor.remove(config);
            operateLogService.writeLog(String.format("删除服务器[name=%s, ip=%s]成功。", machineName, machineIp));
            return json;
        }
    } catch (EntityAccessException ex) {
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) ConfigJson(com.ds.retl.dal.entity.ConfigJson) EntityAccessException(org.mx.dal.exception.EntityAccessException)

Example 2 with UserInterfaceErrorException

use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.

the class ServerManageServiceImpl method prepareStormService.

private void prepareStormService(JSONObject config) throws UserInterfaceErrorException {
    if (config == null) {
        return;
    }
    // services: [], zookeepers: [], nimbuses: [], dataDir: '/opt/storm/data', slots: 10, startPort: 6700
    List<String> services = config.getObject("services", List.class);
    List<String> zookeepers = config.getObject("zookeepers", List.class);
    List<String> nimbuses = config.getObject("nimbuses", List.class);
    String dataDir = config.getString("dataDir");
    int slots = config.getIntValue("slots");
    int startPort = config.getIntValue("startPort");
    if (zookeepers == null || zookeepers.isEmpty()) {
        if (logger.isErrorEnabled()) {
            logger.error("The zookeeper list not configured.");
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    if (nimbuses == null || nimbuses.isEmpty()) {
        if (logger.isErrorEnabled()) {
            logger.error("The nimbus feed list not configured.");
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    if (StringUtils.isBlank(dataDir)) {
        if (logger.isErrorEnabled()) {
            logger.error("The storm data directory not configured.");
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    // 目录准备
    new File("/opt/storm/conf").mkdirs();
    new File("/opt/storm/data").mkdirs();
    new File("/opt/storm/logs").mkdirs();
    // 准备storm配置文件
    String stormYamlName = "/opt/storm/conf/storm.yaml";
    File stormYaml = new File(stormYamlName);
    if (stormYaml.exists()) {
        stormYaml.renameTo(new File(String.format("%s.%d", stormYamlName, new Date().getTime())));
    }
    try (PrintStream ps = new PrintStream(new FileOutputStream(stormYaml))) {
        ps.println("storm.zookeeper.servers:");
        zookeepers.forEach(server -> ps.println(String.format("  - \"%s\"", server)));
        ps.println();
        for (int index = 0; index < nimbuses.size(); index++) {
            nimbuses.set(index, String.format("\"%s\"", nimbuses.get(index)));
        }
        ps.println(String.format("nimbus.seeds: [%s]", StringUtils.merge(nimbuses, ", ")));
        ps.println();
        ps.println(String.format("storm.local.dir: \"%s\"", dataDir));
        ps.println();
        ps.println("supervisor.slots.ports:");
        for (int index = 0; index < slots; index++) {
            ps.println(String.format("  - %d", startPort + index));
        }
        ps.println();
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Prepare %s fail."), ex);
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SERVICE_STORM_ZOOCFG_FAIL);
    }
    if (services != null && services.size() > 0) {
        // 准备服务列表
        String servicesConfName = "/opt/storm/conf/services.conf";
        File serviceConf = new File(servicesConfName);
        if (serviceConf.exists()) {
            serviceConf.renameTo(new File(String.format("%s.%d", servicesConfName, new Date().getTime())));
        }
        try (PrintStream ps = new PrintStream(new FileOutputStream(serviceConf))) {
            ps.println(StringUtils.merge(services, " "));
        } catch (IOException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(String.format("Prepare %s fail.", servicesConfName), ex);
            }
            throw new UserInterfaceErrorException(UserInterfaceErrors.SERVICE_STORM_ZOOCFG_FAIL);
        }
    }
    // storm service文件
    String stormService = "/usr/lib/systemd/system/storm.service";
    try (PrintStream ps = new PrintStream(new FileOutputStream(stormService))) {
        ps.println("[Unit]");
        ps.println("Description=Apache storm service");
        ps.println("After=zookeeper.target");
        ps.println();
        ps.println("[Service]");
        ps.println("Type=forking");
        ps.println("ExecStart=/opt/storm/bin/start-server.sh");
        ps.println("ExecStop=/opt/storm/bin/stop-server.sh");
        ps.println();
        ps.println("[Install]");
        ps.println("WantedBy=multi-user.target");
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Prepare %s fail.", stormService), ex);
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SERVICE_STORM_SERVICE_FAIL);
    }
    try {
        operateLogService.writeLog("写入STORM服务配置信息成功。");
    } catch (EntityAccessException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
    }
}
Also used : PrintStream(java.io.PrintStream) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) FileOutputStream(java.io.FileOutputStream) EntityAccessException(org.mx.dal.exception.EntityAccessException) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date)

Example 3 with UserInterfaceErrorException

use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.

the class ServerManageServiceImpl method prepareZookeeperService.

private void prepareZookeeperService(JSONObject config) throws UserInterfaceErrorException {
    if (config == null) {
        return;
    }
    // cluster: false, serverNo: '1', dataDir: '/opt/zookeeper/data', servers: []
    boolean cluster = config.getBooleanValue("cluster");
    int serverNo = config.getInteger("serverNo");
    String dataDir = config.getString("dataDir");
    List<String> servers = config.getObject("servers", List.class);
    if (StringUtils.isBlank(dataDir)) {
        if (logger.isErrorEnabled()) {
            logger.error("The storm data directory not configured.");
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    // 目录准备
    new File("/opt/zookeeper/conf").mkdirs();
    new File(dataDir).mkdirs();
    // 写入配置文件
    String zooCfgName = "/opt/zookeeper/conf/zoo.cfg";
    File zooCfg = new File(zooCfgName);
    if (zooCfg.exists()) {
        zooCfg.renameTo(new File(String.format("%s.%d", zooCfgName, new Date().getTime())));
    }
    try (PrintStream ps = new PrintStream(new FileOutputStream(zooCfg))) {
        ps.println("tickTime=2000");
        ps.println();
        ps.println("initLimit=5");
        ps.println();
        ps.println("syncLimit=2");
        ps.println();
        ps.println(String.format("dataDir=%s", dataDir));
        ps.println();
        ps.println("clientPort=2181");
        ps.println();
        if (cluster) {
            // 如果是集群,则写入集群列表
            for (int index = 0; index < servers.size(); index++) {
                ps.println(String.format("server.%d=%s", index + 1, servers.get(index)));
            }
            ps.println();
        }
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Prepare %s fail.", zooCfgName), ex);
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SERVICE_ZK_ZOOCFG_FAIL);
    }
    if (cluster) {
        // 写入本机myid文件
        String myidName = String.format("%s/myid", dataDir);
        File myid = new File(myidName);
        try (PrintStream ps = new PrintStream(new FileOutputStream(myid))) {
            ps.println(serverNo);
        } catch (IOException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(String.format("Prepare %s fail.", myidName), ex);
            }
            throw new UserInterfaceErrorException(UserInterfaceErrors.SERVICE_ZK_ZOOCFG_FAIL);
        }
    }
    // 写入zookeeper service文件
    String zookeeperService = "/usr/lib/systemd/system/zookeeper.service";
    try (PrintStream ps = new PrintStream(new FileOutputStream(zookeeperService))) {
        ps.println("[Unit]");
        ps.println("Description=Apache zookeeper service");
        ps.println("After=network.target remote-fs.target nss-lookup.target");
        ps.println();
        ps.println("[Service]");
        ps.println("Type=forking");
        ps.println("ExecStart=/opt/zookeeper/bin/zookeeperd.sh start");
        ps.println("ExecStop=/opt/zookeeper/bin/zookeeperd.sh stop");
        ps.println("ExecReload=/opt/zookeeper/bin/zookeeperd.sh restart");
        ps.println();
        ps.println("[Install]");
        ps.println("WantedBy=multi-user.target");
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Prepare %s fail.", zookeeperService), ex);
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SERVICE_ZK_SERVICE_FAIL);
    }
    try {
        operateLogService.writeLog("写入ZOOKEEPER服务配置信息成功。");
    } catch (EntityAccessException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
    }
}
Also used : PrintStream(java.io.PrintStream) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) FileOutputStream(java.io.FileOutputStream) EntityAccessException(org.mx.dal.exception.EntityAccessException) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date)

Example 4 with UserInterfaceErrorException

use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.

the class StormRestfulServiceClientImpl method invokeGet.

/**
 * 使用Jersey发起一次GET操作
 *
 * @param url 业务地址
 * @return GET返回的数据,JSON对象
 * @throws UserInterfaceErrorException 调用过程中发生的异常
 */
private JSONObject invokeGet(String url) throws UserInterfaceErrorException {
    String uri = prepareUri(url);
    WebTarget target = this.client.target(uri);
    Response response = target.request().get();
    String jsonString = response.readEntity(String.class);
    response.close();
    JSONObject json = JSON.parseObject(jsonString);
    if (response.getStatus() == 500) {
        throw new UserInterfaceErrorException(json.getString("error"));
    } else {
        return json;
    }
}
Also used : Response(javax.ws.rs.core.Response) JSONObject(com.alibaba.fastjson.JSONObject) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) WebTarget(javax.ws.rs.client.WebTarget)

Example 5 with UserInterfaceErrorException

use of com.ds.retl.exception.UserInterfaceErrorException in project main by JohnPeng739.

the class TopologyManageServiceImpl method kill.

/**
 * {@inheritDoc}
 *
 * @see TopologyManageServiceImpl#kill(String)
 */
@Override
public Topology kill(String id) throws UserInterfaceErrorException {
    try {
        Topology topology = accessor.getById(id, Topology.class);
        if (topology == null) {
            throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_NOT_FOUND);
        }
        if (StringUtils.isBlank(topology.getTopologyId()) || !topology.isSubmitted()) {
            throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_NOT_SUBMITTED);
        }
        JSONObject result = stormClient.getTopology(topology.getTopologyId());
        if (result == null) {
            throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_NOT_SUBMITTED);
        }
        result = stormClient.kill(topology.getTopologyId());
        if (result == null || !"success".equals(result.getString("status"))) {
            throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_KILL_FAIL);
        }
        topology.setSubmitted(false);
        topology.setSubmitInfo(null);
        topology.setTopologyId(null);
        topology = accessor.save(topology);
        operateLogService.writeLog(String.format("杀死计算拓扑[%s]操作成功。", topology.getName()));
        return topology;
    } catch (EntityAccessException ex) {
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) EntityAccessException(org.mx.dal.exception.EntityAccessException) Topology(com.ds.retl.dal.entity.Topology)

Aggregations

UserInterfaceErrorException (com.ds.retl.exception.UserInterfaceErrorException)35 JSONObject (com.alibaba.fastjson.JSONObject)17 EntityAccessException (org.mx.dal.exception.EntityAccessException)15 PaginationDataVO (org.mx.rest.vo.PaginationDataVO)13 DataVO (org.mx.rest.vo.DataVO)12 Topology (com.ds.retl.dal.entity.Topology)8 User (com.ds.retl.dal.entity.User)7 Transactional (org.springframework.transaction.annotation.Transactional)6 TopologyVO (com.ds.retl.rest.vo.topology.TopologyVO)5 ServerInfoVO (com.ds.retl.rest.vo.server.ServerInfoVO)3 UserVO (com.ds.retl.rest.vo.user.UserVO)3 IOException (java.io.IOException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 EntityInstantiationException (org.mx.dal.exception.EntityInstantiationException)3 RestInvokeException (org.mx.rest.client.RestInvokeException)3 JSONArray (com.alibaba.fastjson.JSONArray)2 ConfigJson (com.ds.retl.dal.entity.ConfigJson)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 PrintStream (java.io.PrintStream)2