ElasticSearch 7.10 怎么查询集群节点负载信息

165 阅读6分钟

背景

历史代码用的 6.x TransportClient 直接查询出来 List<NodeStats> .

升级 7.10.0 后, 阿里云这个版本集群, 直接不支持 TransportClient .

TransportClient client = clusterInfo.getClient();
RestHighLevelClient client = clusterInfo.getRestHighLevelClient();
NodesStatsResponse nodesStatsResponse = client.admin().cluster().prepareNodesStats().all().execute().actionGet();
List<NodeStats> nodes = nodesStatsResponse.getNodes();
return nodes;

解决

考虑 GET _nodes/stats 能够返回我需要的节点负载信息.

那么我是不是可以利用 http client 发送这个请求呢?

可以的.

自己用 http client 请求, 编码麻烦呢, 考虑到还有账户密码验证.

于是, 考虑拿到 es 自己的 RestClient 去请求.

ArrayList<EsNodeStat> nodeStatsList = new ArrayList<>();
RestClient restClient = clusterInfo.getRestHighLevelClient().getLowLevelClient();

// GET _nodes/stats
// 构建 ES 需要的 Request 对象
Request request = new Request("GET", "_nodes/stats");
Response response = null;
try {
		// http client 发送请求
    response = restClient.performRequest(request);
} catch (Exception e) {
    log.error("getNodeStats error", e);
}
if (response != null) {
    if (response.getStatusLine().getStatusCode() == 200) {
        String s = EntityUtils.toString(response.getEntity());
        JSONObject data = JSON.parseObject(s, Feature.OrderedField);
        // 节点id -> 节点信息
        JSONObject nodes = data.getJSONObject("nodes");
        for (String nodeId : nodes.keySet()) {
            EsNodeStat nodeStats = nodes.getObject(nodeId, EsNodeStat.class);
            nodeStatsList.add(nodeStats);
        }
    } else {
        log.error("getNodeStats 返回非200 {}", response);
    }
}

return nodeStatsList;

GET _nodes/stats 结果例子

{
    "_nodes": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "cluster_name": "elasticsearch",
    "nodes": {
        "w-4PYyVOQ2iTEVbyRbIGZg": {
            "timestamp": 1690337153710,
            "name": "7f28ba157643",
            "transport_address": "172.18.0.2:9300",
            "host": "172.18.0.2",
            "ip": "172.18.0.2:9300",
            "roles": [
                "data",
                "data_cold",
                "data_content",
                "data_hot",
                "data_warm",
                "ingest",
                "master",
                "ml",
                "remote_cluster_client",
                "transform"
            ],
            "attributes": {
                "ml.machine_memory": "42021007360",
                "xpack.installed": "true",
                "transform.node": "true",
                "ml.max_open_jobs": "20"
            },
            "indices": {
                "docs": {
                    "count": 1197113,
                    "deleted": 3414
                },
                "store": {
                    "size_in_bytes": 709651047,
                    "reserved_in_bytes": 0
                },
                "indexing": {
                    "index_total": 1200521,
                    "index_time_in_millis": 606471,
                    "index_current": 0,
                    "index_failed": 0,
                    "delete_total": 31,
                    "delete_time_in_millis": 85,
                    "delete_current": 0,
                    "noop_update_total": 0,
                    "is_throttled": false,
                    "throttle_time_in_millis": 0
                },
                "get": {
                    "total": 16491,
                    "time_in_millis": 2564,
                    "exists_total": 15377,
                    "exists_time_in_millis": 2488,
                    "missing_total": 1114,
                    "missing_time_in_millis": 76,
                    "current": 0
                },
                "search": {
                    "open_contexts": 0,
                    "query_total": 27778,
                    "query_time_in_millis": 67384,
                    "query_current": 0,
                    "fetch_total": 27677,
                    "fetch_time_in_millis": 890,
                    "fetch_current": 0,
                    "scroll_total": 22971,
                    "scroll_time_in_millis": 154790,
                    "scroll_current": 0,
                    "suggest_total": 0,
                    "suggest_time_in_millis": 0,
                    "suggest_current": 0
                },
                "merges": {
                    "current": 0,
                    "current_docs": 0,
                    "current_size_in_bytes": 0,
                    "total": 271,
                    "total_time_in_millis": 53039,
                    "total_docs": 1035311,
                    "total_size_in_bytes": 586538194,
                    "total_stopped_time_in_millis": 0,
                    "total_throttled_time_in_millis": 6844,
                    "total_auto_throttle_in_bytes": 200860207
                },
                "refresh": {
                    "total": 2666,
                    "total_time_in_millis": 108639,
                    "external_total": 2635,
                    "external_total_time_in_millis": 116586,
                    "listeners": 0
                },
                "flush": {
                    "total": 38,
                    "periodic": 0,
                    "total_time_in_millis": 1974
                },
                "warmer": {
                    "current": 0,
                    "total": 2606,
                    "total_time_in_millis": 3484
                },
                "query_cache": {
                    "memory_size_in_bytes": 14160,
                    "total_count": 1008,
                    "hit_count": 330,
                    "miss_count": 678,
                    "cache_size": 77,
                    "cache_count": 193,
                    "evictions": 116
                },
                "fielddata": {
                    "memory_size_in_bytes": 616624,
                    "evictions": 0
                },
                "completion": {
                    "size_in_bytes": 0
                },
                "segments": {
                    "count": 57,
                    "memory_in_bytes": 366428,
                    "terms_memory_in_bytes": 162688,
                    "stored_fields_memory_in_bytes": 28904,
                    "term_vectors_memory_in_bytes": 0,
                    "norms_memory_in_bytes": 2368,
                    "points_memory_in_bytes": 0,
                    "doc_values_memory_in_bytes": 172468,
                    "index_writer_memory_in_bytes": 0,
                    "version_map_memory_in_bytes": 0,
                    "fixed_bit_set_memory_in_bytes": 1176,
                    "max_unsafe_auto_id_timestamp": -1,
                    "file_sizes": {}
                },
                "translog": {
                    "operations": 3348,
                    "size_in_bytes": 1887958,
                    "uncommitted_operations": 3348,
                    "uncommitted_size_in_bytes": 1887958,
                    "earliest_last_modified_age": 0
                },
                "request_cache": {
                    "memory_size_in_bytes": 9591,
                    "evictions": 0,
                    "hit_count": 10,
                    "miss_count": 50
                },
                "recovery": {
                    "current_as_source": 0,
                    "current_as_target": 0,
                    "throttle_time_in_millis": 0
                }
            },
            "os": {
                "timestamp": 1690337153727,
                "cpu": {
                    "percent": 50,
                    "load_average": {
                        "1m": 13.28,
                        "5m": 14.95,
                        "15m": 15.94
                    }
                },
                "mem": {
                    "total_in_bytes": 42021007360,
                    "free_in_bytes": 933277696,
                    "used_in_bytes": 41087729664,
                    "free_percent": 2,
                    "used_percent": 98
                },
                "swap": {
                    "total_in_bytes": 0,
                    "free_in_bytes": 0,
                    "used_in_bytes": 0
                },
                "cgroup": {
                    "cpuacct": {
                        "control_group": "/",
                        "usage_nanos": 2253869159074
                    },
                    "cpu": {
                        "control_group": "/",
                        "cfs_period_micros": 100000,
                        "cfs_quota_micros": -1,
                        "stat": {
                            "number_of_elapsed_periods": 0,
                            "number_of_times_throttled": 0,
                            "time_throttled_nanos": 0
                        }
                    },
                    "memory": {
                        "control_group": "/",
                        "limit_in_bytes": "9223372036854771712",
                        "usage_in_bytes": "5634494464"
                    }
                }
            },
            "process": {
                "timestamp": 1690337153727,
                "open_file_descriptors": 404,
                "max_file_descriptors": 65536,
                "cpu": {
                    "percent": 0,
                    "total_in_millis": 2246510
                },
                "mem": {
                    "total_virtual_in_bytes": 10938613760
                }
            },
            "jvm": {
                "timestamp": 1690337153728,
                "uptime_in_millis": 68880102,
                "mem": {
                    "heap_used_in_bytes": 1476767088,
                    "heap_used_percent": 34,
                    "heap_committed_in_bytes": 4294967296,
                    "heap_max_in_bytes": 4294967296,
                    "non_heap_used_in_bytes": 197427472,
                    "non_heap_committed_in_bytes": 208347136,
                    "pools": {
                        "young": {
                            "used_in_bytes": 924844032,
                            "max_in_bytes": 0,
                            "peak_used_in_bytes": 2573205504,
                            "peak_max_in_bytes": 0
                        },
                        "old": {
                            "used_in_bytes": 546684928,
                            "max_in_bytes": 4294967296,
                            "peak_used_in_bytes": 619681792,
                            "peak_max_in_bytes": 4294967296
                        },
                        "survivor": {
                            "used_in_bytes": 5238128,
                            "max_in_bytes": 0,
                            "peak_used_in_bytes": 153092096,
                            "peak_max_in_bytes": 0
                        }
                    }
                },
                "threads": {
                    "count": 83,
                    "peak_count": 84
                },
                "gc": {
                    "collectors": {
                        "young": {
                            "collection_count": 219,
                            "collection_time_in_millis": 4549
                        },
                        "old": {
                            "collection_count": 0,
                            "collection_time_in_millis": 0
                        }
                    }
                },
                "buffer_pools": {
                    "mapped": {
                        "count": 125,
                        "used_in_bytes": 423368718,
                        "total_capacity_in_bytes": 423368718
                    },
                    "direct": {
                        "count": 78,
                        "used_in_bytes": 9690533,
                        "total_capacity_in_bytes": 9690532
                    },
                    "mapped - 'non-volatile memory'": {
                        "count": 0,
                        "used_in_bytes": 0,
                        "total_capacity_in_bytes": 0
                    }
                },
                "classes": {
                    "current_loaded_count": 23355,
                    "total_loaded_count": 23355,
                    "total_unloaded_count": 0
                }
            },
            "thread_pool": {
                "analyze": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "ccr": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "fetch_shard_started": {
                    "threads": 1,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 8,
                    "completed": 8
                },
                "fetch_shard_store": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "flush": {
                    "threads": 3,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 3,
                    "completed": 68500
                },
                "force_merge": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "generic": {
                    "threads": 6,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 6,
                    "completed": 122024
                },
                "get": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "listener": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "management": {
                    "threads": 5,
                    "queue": 0,
                    "active": 1,
                    "rejected": 0,
                    "largest": 5,
                    "completed": 84810
                },
                "ml_datafeed": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "ml_job_comms": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "ml_utility": {
                    "threads": 2,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 2,
                    "completed": 68550
                },
                "refresh": {
                    "threads": 4,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 4,
                    "completed": 550106
                },
                "rollup_indexing": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "search": {
                    "threads": 13,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 13,
                    "completed": 374
                },
                "search_throttled": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "searchable_snapshots_cache_fetch_async": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "searchable_snapshots_cache_prewarming": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "security-crypto": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "security-token-key": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "snapshot": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "system_read": {
                    "threads": 4,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 4,
                    "completed": 68286
                },
                "system_write": {
                    "threads": 4,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 4,
                    "completed": 3508
                },
                "transform_indexing": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "warmer": {
                    "threads": 4,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 4,
                    "completed": 17556
                },
                "watcher": {
                    "threads": 0,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 0,
                    "completed": 0
                },
                "write": {
                    "threads": 8,
                    "queue": 0,
                    "active": 0,
                    "rejected": 0,
                    "largest": 8,
                    "completed": 970
                }
            },
            "fs": {
                "timestamp": 1690337153729,
                "total": {
                    "total_in_bytes": 41082454016,
                    "free_in_bytes": 7753191424,
                    "available_in_bytes": 5642723328
                },
                "data": [
                    {
                        "path": "/usr/share/elasticsearch/data/nodes/0",
                        "mount": "/ (rootfs)",
                        "type": "rootfs",
                        "total_in_bytes": 41082454016,
                        "free_in_bytes": 7753191424,
                        "available_in_bytes": 5642723328
                    }
                ],
                "io_stats": {}
            },
            "transport": {
                "server_open": 0,
                "total_outbound_connections": 0,
                "rx_count": 0,
                "rx_size_in_bytes": 0,
                "tx_count": 0,
                "tx_size_in_bytes": 0
            },
            "http": {
                "current_open": 57,
                "total_opened": 8506
            },
            "breakers": {
                "request": {
                    "limit_size_in_bytes": 2576980377,
                    "limit_size": "2.3gb",
                    "estimated_size_in_bytes": 0,
                    "estimated_size": "0b",
                    "overhead": 1,
                    "tripped": 0
                },
                "fielddata": {
                    "limit_size_in_bytes": 1717986918,
                    "limit_size": "1.5gb",
                    "estimated_size_in_bytes": 616624,
                    "estimated_size": "602.1kb",
                    "overhead": 1.03,
                    "tripped": 0
                },
                "in_flight_requests": {
                    "limit_size_in_bytes": 4294967296,
                    "limit_size": "4gb",
                    "estimated_size_in_bytes": 0,
                    "estimated_size": "0b",
                    "overhead": 2,
                    "tripped": 0
                },
                "model_inference": {
                    "limit_size_in_bytes": 2147483648,
                    "limit_size": "2gb",
                    "estimated_size_in_bytes": 0,
                    "estimated_size": "0b",
                    "overhead": 1,
                    "tripped": 0
                },
                "accounting": {
                    "limit_size_in_bytes": 4294967296,
                    "limit_size": "4gb",
                    "estimated_size_in_bytes": 363788,
                    "estimated_size": "355.2kb",
                    "overhead": 1,
                    "tripped": 0
                },
                "parent": {
                    "limit_size_in_bytes": 4080218931,
                    "limit_size": "3.7gb",
                    "estimated_size_in_bytes": 1476767088,
                    "estimated_size": "1.3gb",
                    "overhead": 1,
                    "tripped": 0
                }
            },
            "script": {
                "compilations": 21,
                "cache_evictions": 0,
                "compilation_limit_triggered": 0
            },
            "discovery": {
                "cluster_state_queue": {
                    "total": 0,
                    "pending": 0,
                    "committed": 0
                },
                "published_cluster_states": {
                    "full_states": 2,
                    "incompatible_diffs": 0,
                    "compatible_diffs": 11
                }
            },
            "ingest": {
                "total": {
                    "count": 0,
                    "time_in_millis": 0,
                    "current": 0,
                    "failed": 0
                },
                "pipelines": {
                    "xpack_monitoring_6": {
                        "count": 0,
                        "time_in_millis": 0,
                        "current": 0,
                        "failed": 0,
                        "processors": [
                            {
                                "script": {
                                    "type": "script",
                                    "stats": {
                                        "count": 0,
                                        "time_in_millis": 0,
                                        "current": 0,
                                        "failed": 0
                                    }
                                }
                            },
                            {
                                "gsub": {
                                    "type": "gsub",
                                    "stats": {
                                        "count": 0,
                                        "time_in_millis": 0,
                                        "current": 0,
                                        "failed": 0
                                    }
                                }
                            }
                        ]
                    },
                    "xpack_monitoring_7": {
                        "count": 0,
                        "time_in_millis": 0,
                        "current": 0,
                        "failed": 0,
                        "processors": []
                    }
                }
            },
            "adaptive_selection": {
                "w-4PYyVOQ2iTEVbyRbIGZg": {
                    "outgoing_searches": 0,
                    "avg_queue_size": 0,
                    "avg_service_time_ns": 41832363,
                    "avg_response_time_ns": 5049316,
                    "rank": "5.0"
                }
            },
            "script_cache": {
                "sum": {
                    "compilations": 21,
                    "cache_evictions": 0,
                    "compilation_limit_triggered": 0
                },
                "contexts": [
                    {
                        "context": "aggregation_selector",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "aggs",
                        "compilations": 1,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "aggs_combine",
                        "compilations": 1,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "aggs_init",
                        "compilations": 4,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "aggs_map",
                        "compilations": 6,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "aggs_reduce",
                        "compilations": 5,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "analysis",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "bucket_aggregation",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "field",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "filter",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "ingest",
                        "compilations": 1,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "interval",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "moving-function",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "number_sort",
                        "compilations": 1,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "painless_test",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "processor_conditional",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "score",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "script_heuristic",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "similarity",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "similarity_weight",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "string_sort",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "template",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "terms_set",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "update",
                        "compilations": 2,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "watcher_condition",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "watcher_transform",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    },
                    {
                        "context": "xpack_template",
                        "compilations": 0,
                        "cache_evictions": 0,
                        "compilation_limit_triggered": 0
                    }
                ]
            },
            "indexing_pressure": {
                "memory": {
                    "current": {
                        "combined_coordinating_and_primary_in_bytes": 0,
                        "coordinating_in_bytes": 0,
                        "primary_in_bytes": 0,
                        "replica_in_bytes": 0,
                        "all_in_bytes": 0
                    },
                    "total": {
                        "combined_coordinating_and_primary_in_bytes": 2020090463,
                        "coordinating_in_bytes": 2020090463,
                        "primary_in_bytes": 2048863679,
                        "replica_in_bytes": 0,
                        "all_in_bytes": 2020090463,
                        "coordinating_rejections": 0,
                        "primary_rejections": 0,
                        "replica_rejections": 0
                    },
                    "limit_in_bytes": 429496729
                }
            }
        }
    }
}