All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.monitor.collect.task.ElasticSearchCollectTask Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.monitor.collect.task; // package com.taotao.cloud.health.collect;

import com.taotao.boot.common.utils.context.ContextUtils;
import com.taotao.boot.common.utils.reflect.ClassUtils;
import com.taotao.boot.common.utils.reflect.ReflectionUtils;
import com.taotao.boot.core.support.Collector;
import com.taotao.boot.monitor.annotation.FieldReport;
import com.taotao.boot.monitor.collect.AbstractCollectTask;
import com.taotao.boot.monitor.collect.CollectInfo;
import com.taotao.boot.monitor.properties.CollectTaskProperties;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.ScheduledThreadPoolExecutor;

public class ElasticSearchCollectTask extends AbstractCollectTask {
    private static final String TASK_NAME = "ttc.monitor.collect.elasticsearch";

    private final ThreadMXBean threadMXBean;
    private final CollectTaskProperties properties;
    private HashMap lastThreadUserTime = new HashMap<>();
    private final boolean classExist;

    public ElasticSearchCollectTask(CollectTaskProperties properties) {
        threadMXBean = ManagementFactory.getThreadMXBean();
        this.properties = properties;
        this.classExist = ClassUtils.isExist("com.taotao.boot.monitor.elasticsearch.impl.ElasticSearchProvider");
    }

    @Override
    public int getTimeSpan() {
        return properties.getElasticSearchTimeSpan();
    }

    @Override
    public boolean getEnabled() {
        return properties.isElasticSearchEnabled() && classExist;
    }

    @Override
    public String getDesc() {
        return this.getClass().getName();
    }

    @Override
    public String getName() {
        return TASK_NAME;
    }

    @Override
    protected CollectInfo getData() {
        ElasticSearchData data = new ElasticSearchData();
        if (ContextUtils.getBean(
                        ReflectionUtils.tryClassForName(
                                "com.taotao.boot.monitor.elasticsearch.impl.ElasticSearchProvider"),
                        false)
                != null) {
            Collector.Hook hook = (Collector.Hook) ReflectionUtils.callMethod(
                    ReflectionUtils.tryClassForName("com.taotao.boot.monitor.elasticsearch.impl.ElasticSearchMonitor"),
                    "hook",
                    null);
            if (hook != null) {
                data.hookCurrent = hook.getCurrent();
                data.hookError = hook.getLastErrorPerSecond();
                data.hookSuccess = hook.getLastSuccessPerSecond();
                data.hookList = hook.getMaxTimeSpanList().toText();
                data.hookListPerMinute = hook.getMaxTimeSpanListPerMinute().toText();
            }

            Object transportClient = ContextUtils.getBean(
                    ReflectionUtils.tryClassForName("org.elasticsearch.client.transport.TransportClient"), false);
            if (transportClient != null) {
                data.threadsTotal = 0;
                data.activateTotal = 0;
                data.queueSize = 0;
                // 线程池
                Iterator stats = ReflectionUtils.tryGetValue(transportClient, "threadPool.stats.iterator");
                if (stats != null) {
                    stats.forEachRemaining(stat -> {
                        data.threadsTotal += ReflectionUtils.tryGetValue(stat, "threads", 0);
                        data.activateTotal += ReflectionUtils.tryGetValue(stat, "active", 0);
                        data.queueSize += ReflectionUtils.tryGetValue(stat, "queue", 0);
                    });
                }
                ScheduledThreadPoolExecutor scheduler =
                        ReflectionUtils.tryGetValue(transportClient, "threadPool.scheduler");
                if (scheduler != null) {
                    data.threadsTotal += scheduler.getPoolSize();
                    data.activateTotal += scheduler.getActiveCount();
                    data.queueSize += scheduler.getQueue().size();
                }
                // 节点连接
                Object nodesService = ReflectionUtils.tryGetValue(transportClient, "nodesService");
                if (nodesService != null) {
                    data.nodesCount = ReflectionUtils.tryGetValue(nodesService, "listedNodes.size");
                    data.connectedCount = ReflectionUtils.tryGetValue(nodesService, "nodes.size");
                    data.connectionCount = 0;
                    Collection channels = ReflectionUtils.tryGetValue(
                            nodesService, "transportService.connectionManager.connectedNodes.values");
                    if (channels != null) {
                        for (Object obj : channels) {
                            data.connectionCount += ReflectionUtils.tryGetValue(obj, "channels.size", 0);
                        }
                    }
                }
            }
        }
        return data;
    }

    private static class ElasticSearchData implements CollectInfo {

        @FieldReport(name = "elasticSearch.hook.error", desc = "ElasticSearch服务拦截上一次每秒出错次数")
        private Long hookError;

        @FieldReport(name = "elasticSearch.hook.success", desc = "ElasticSearch服务拦截上一次每秒成功次数")
        private Long hookSuccess;

        @FieldReport(name = "elasticSearch.hook.current", desc = "ElasticSearch服务拦截当前执行任务数")
        private Long hookCurrent;

        @FieldReport(name = "elasticSearch.hook.list.detail", desc = "ElasticSearch服务拦截历史最大耗时任务列表")
        private String hookList;

        @FieldReport(name = "elasticSearch.hook.list.minute.detail", desc = "ElasticSearch服务拦截历史最大耗时任务列表(每分钟)")
        private String hookListPerMinute;

        @FieldReport(name = "elasticSearch.node.count", desc = "ElasticSearch集群发现节点数")
        private Integer nodesCount;

        @FieldReport(name = "elasticSearch.node.connected", desc = "ElasticSearch集群已连接节点数")
        private Integer connectedCount;

        @FieldReport(name = "elasticSearch.node.connections", desc = "ElasticSearch集群连接数")
        private Integer connectionCount;

        @FieldReport(name = "elasticSearch.pool.threads.count", desc = "ElasticSearch集群线程池线程数")
        private Integer threadsTotal;

        @FieldReport(name = "elasticSearch.pool.threads.active", desc = "ElasticSearch集群池线程活动线程数")
        private Integer activateTotal;

        @FieldReport(name = "elasticSearch.pool.queue.size", desc = "ElasticSearch集群池线程队列大小")
        private Integer queueSize;
    }
}