使用NEST获取ElasticSearch批量队列状态

时间:2015-12-16 16:58:05

标签: c# elasticsearch nest

我有一个程序在ElasticSearch集群上执行多个批量索引操作。在某些时候,我开始得到像这样的错误(剪掉):

RemoteTransportException[...][indices:data/write/bulk[s]]]; nested: EsRejectedExecutionException[rejected execution (queue capacity 100) ...];

有没有办法可以验证批量上传队列的状态,理想情况下使用NEST,这样我就可以减慢客户端应用程序的速度,以防我看到服务器上的队列满了?

NodesInfo方法看起来很有趣,但我不知道如何访问我需要的信息:

using Nest;
using System;

class Program {
    static void Main(string[] args) {
        ElasticClient client = new ElasticClient(new ConnectionSettings(new Uri("http://whatever:9200/")));
        var nodesInfoResponse = client.NodesInfo();
        if (nodesInfoResponse.IsValid) {
            foreach (var n in nodesInfoResponse.Nodes) {
                Console.WriteLine($"Node: {n.Key}");
                var bulk = n.Value.ThreadPool["bulk"];
                // ???
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您需要使用NodesStats()而不是NodesInfo()

var nodesStatsResponse = client.NodesStats();
if (nodesStatsResponse.IsValid)
{
    foreach (var node in nodesStatsResponse.Nodes)
    {
        long bulkThreadPoolQueueSize = node.Value.ThreadPool["bulk"].Queue;
    }
}

<强>更新 上述查询将带来大量不需要的信息。通过_cat/thread_pool API的使用,获得相同信息的高度优化的请求。见下文:

var catThreadPoolResponse = client.CatThreadPool(d => d.H("host", "bulk.queue"));
if (catThreadPoolResponse.IsValid)
{
    foreach (var record in catThreadPoolResponse.Records)
    {
        string nodeName = record.Host;
        long bulkThreadPoolQueueSize = int.Parse(record.Bulk.Queue);
        Console.WriteLine($"Node [{nodeName}] : BulkThreadPoolQueueSize [{bulkThreadPoolQueueSize}]");
    }
}