Search in sources :

Example 1 with QueryESClusterDetailResponse

use of beans.response.QueryESClusterDetailResponse in project MSEC by Tencent.

the class QueryESClusterDetail method exec.

public QueryESClusterDetailResponse exec(QueryESClusterDetailRequest request) {
    Logger logger = Logger.getLogger(QueryESClusterDetail.class);
    QueryESClusterDetailResponse resp = new QueryESClusterDetailResponse();
    String result = checkIdentity();
    if (!result.equals("success")) {
        resp.setStatus(99);
        resp.setMessage(result);
        return resp;
    }
    boolean all_ok = true;
    int master_num = 0;
    util = new DBUtil();
    if (util.getConnection() == null) {
        resp.setStatus(100);
        resp.setMessage("db connect failed!");
        return resp;
    }
    cluster_ips = new ArrayList<>();
    HashMap<String, QueryESClusterDetailResponse.RTInfo> info_map = new HashMap<>();
    try {
        if (updatePlan(request.getFirst_level_service_name(), request.getSecond_level_service_name())) {
            resp.setMessage("refresh");
            resp.setStatus(101);
            return resp;
        }
        getStatus(request.getFirst_level_service_name(), request.getSecond_level_service_name());
        ESHelper helper = new ESHelper();
        helper.ClusterStatus(cluster_ips, request.getFirst_level_service_name() + "." + request.getSecond_level_service_name(), resp);
        if (resp.getHealth_status() != "INIT") {
            for (Map.Entry<String, QueryESClusterDetailResponse.RTInfo> entry : resp.getInfo_map().entrySet()) {
                if (!cluster_ips.contains(entry.getKey())) {
                    // add ip to service
                    addServer(request.getSecond_level_service_name(), request.getSecond_level_service_name(), entry.getKey(), resp.getServer_port());
                    resp.setMessage("refresh");
                    resp.setStatus(101);
                    return resp;
                }
            }
            for (String ip : cluster_ips) {
                QueryESClusterDetailResponse.RTInfo rt_info = resp.getInfo_map().get(ip);
                if (rt_info != null) {
                    if (!rt_info.isOK()) {
                        logger.error(String.format("ip failed|%s", ip));
                    }
                    updateStatus(request.getFirst_level_service_name(), request.getSecond_level_service_name(), ip, rt_info.isOK());
                } else {
                    resp.getInfo_map().put(ip, new QueryESClusterDetailResponse().new RTInfo());
                    updateStatus(request.getFirst_level_service_name(), request.getSecond_level_service_name(), ip, false);
                }
            }
        }
    } catch (Exception e) {
        logger.error(e);
        e.printStackTrace();
    } finally {
        util.releaseConn();
    }
    return resp;
}
Also used : HashMap(java.util.HashMap) Logger(org.apache.log4j.Logger) SQLException(java.sql.SQLException) DBUtil(msec.org.DBUtil) Map(java.util.Map) HashMap(java.util.HashMap) QueryESClusterDetailResponse(beans.response.QueryESClusterDetailResponse)

Example 2 with QueryESClusterDetailResponse

use of beans.response.QueryESClusterDetailResponse in project MSEC by Tencent.

the class ESHelper method ClusterStatus.

public void ClusterStatus(ArrayList<String> ips, String cluster_name, QueryESClusterDetailResponse response) {
    Logger logger = Logger.getLogger(ESHelper.class);
    TransportClient client = null;
    HashSet<String> total_ips = new HashSet<>(ips);
    try {
        Settings settings = Settings.builder().put("cluster.name", cluster_name).put("client.transport.sniff", true).build();
        for (String ip : ips) {
            if (client == null) {
                client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
            } else
                client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
        }
        ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().setTimeout(TimeValue.timeValueSeconds(5)).execute().actionGet();
        if (healthResponse != null && !healthResponse.isTimedOut()) {
            response.setActive_shards(healthResponse.getActiveShards());
            response.setTotal_shards(healthResponse.getActiveShards() + healthResponse.getUnassignedShards());
            response.setHealth_status(healthResponse.getStatus().toString().toLowerCase());
            response.setServer_port(default_port);
            logger.info(healthResponse);
            if (healthResponse.getNumberOfNodes() > 0) {
                NodesStatsResponse nodeStats = client.admin().cluster().prepareNodesStats().all().get();
                if (nodeStats != null) {
                    for (NodeStats stats : nodeStats.getNodes()) {
                        QueryESClusterDetailResponse.RTInfo info = new QueryESClusterDetailResponse().new RTInfo();
                        info.setOK(true);
                        info.setAvail_disk_size(stats.getFs().getTotal().getAvailable().getBytes());
                        info.setDoc_count(stats.getIndices().getDocs().getCount());
                        info.setDoc_disk_size(stats.getIndices().getStore().getSizeInBytes());
                        response.getInfo_map().put(stats.getNode().getAddress().getHost(), info);
                        total_ips.add(stats.getNode().getAddress().getHost());
                    }
                }
            }
        }
        // update Zen settings
        client.admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().put("discovery.zen.minimum_master_nodes", total_ips.size() / 2 + 1)).setTransientSettings(Settings.builder().put("discovery.zen.minimum_master_nodes", total_ips.size() / 2 + 1)).get();
        // update template settings
        PutIndexTemplateResponse tpl_resp = client.admin().indices().preparePutTemplate("template_msec").setTemplate("msec_*").setSettings(Settings.builder().put("number_of_replicas", 1).put("refresh_interval", "30s")).addMapping("logs", MAPPING).execute().get();
        logger.info("Create mapping: " + tpl_resp.isAcknowledged());
    } catch (UnknownHostException e) {
        logger.error(e);
    } catch (Exception e) {
        logger.error(e);
    } finally {
        if (client != null)
            client.close();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UnknownHostException(java.net.UnknownHostException) Logger(org.apache.log4j.Logger) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) Settings(org.elasticsearch.common.settings.Settings) QueryESClusterDetailResponse(beans.response.QueryESClusterDetailResponse)

Aggregations

QueryESClusterDetailResponse (beans.response.QueryESClusterDetailResponse)2 Logger (org.apache.log4j.Logger)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DBUtil (msec.org.DBUtil)1 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)1 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)1 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)1 NodesStatsResponse (org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse)1 PutIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse)1 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)1 TransportClient (org.elasticsearch.client.transport.TransportClient)1 Settings (org.elasticsearch.common.settings.Settings)1 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)1 MasterNotDiscoveredException (org.elasticsearch.discovery.MasterNotDiscoveredException)1 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)1