Skip to content

Commit 6a87a52

Browse files
committed
Add support for /_cluster/state
Add initial monitoring support via /_cluster/state, incomplete on metadata and blocks but the majority of the objects are present/mapped.
1 parent b5db27a commit 6a87a52

File tree

7 files changed

+219
-0
lines changed

7 files changed

+219
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
4+
namespace Nest.Tests.Integration.Cluster
5+
{
6+
[TestFixture]
7+
public class StateTests : IntegrationTests
8+
{
9+
[Test]
10+
public void SimpleState()
11+
{
12+
var r = this._client.ClusterState(ClusterStateInfo.All);
13+
Assert.True(r.IsValid);
14+
Assert.NotNull(r.ClusterName);
15+
Assert.NotNull(r.MasterNode);
16+
Assert.NotNull(r.Metadata);
17+
Assert.NotNull(r.Metadata.Indices);
18+
Assert.True(r.Metadata.Indices.Count > 0);
19+
Assert.NotNull(r.Nodes);
20+
Assert.True(r.Nodes.Count > 0);
21+
Assert.NotNull(r.RoutingNodes);
22+
Assert.True(r.RoutingNodes.Nodes.Count > 0);
23+
Assert.NotNull(r.RoutingTable);
24+
}
25+
[Test]
26+
public void StateWithoutMetadata()
27+
{
28+
var r = this._client.ClusterState(ClusterStateInfo.ExcludeMetadata);
29+
Assert.IsNull(r.Metadata);
30+
}
31+
[Test]
32+
public void StateWithoutNodes()
33+
{
34+
var r = this._client.ClusterState(ClusterStateInfo.ExcludeNodes);
35+
Assert.IsNull(r.Nodes);
36+
}
37+
[Test]
38+
public void StateWithoutRoutingTable()
39+
{
40+
var r = this._client.ClusterState(ClusterStateInfo.ExcludeRoutingTable);
41+
Assert.IsNull(r.RoutingTable);
42+
}
43+
//[Test]
44+
//public void StateWithoutBlocks()
45+
//{
46+
// var r = this._client.ClusterState(ClusterStateInfo.ExcludeRoutingTable);
47+
// Assert.IsNull(r.Blocks);
48+
//}
49+
}
50+
}

src/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Compile Include="Indices\Analysis\Analyzers\AnalyzerTest.cs" />
6565
<Compile Include="Indices\Analysis\Analyzers\AnalyzerTestResult.cs" />
6666
<Compile Include="Indices\Analysis\Analyzers\AnalyzerTests.cs" />
67+
<Compile Include="Cluster\StateTests.cs" />
6768
<Compile Include="IntegrationTests.cs" />
6869
<Compile Include="Cluster\HealthTests.cs" />
6970
<Compile Include="Cluster\NodeTests.cs" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace Nest
5+
{
6+
public interface IClusterStateResponse : IResponse
7+
{
8+
string ClusterName { get; }
9+
string MasterNode { get; }
10+
Dictionary<string, NodeState> Nodes { get; }
11+
MetadataState Metadata { get; }
12+
RoutingTableState RoutingTable { get; }
13+
RoutingNodesState RoutingNodes { get; }
14+
}
15+
16+
[JsonObject]
17+
public class ClusterStateResponse : BaseResponse, IClusterStateResponse
18+
{
19+
public ClusterStateResponse()
20+
{
21+
this.IsValid = true;
22+
}
23+
[JsonProperty("cluster_name")]
24+
public string ClusterName { get; internal set; }
25+
[JsonProperty("master_node")]
26+
public string MasterNode { get; internal set; }
27+
28+
[JsonProperty("nodes")]
29+
public Dictionary<string, NodeState> Nodes { get; internal set; }
30+
31+
[JsonProperty("metadata")]
32+
public MetadataState Metadata { get; internal set; }
33+
34+
[JsonProperty("routing_table")]
35+
public RoutingTableState RoutingTable { get; internal set; }
36+
37+
[JsonProperty("routing_nodes")]
38+
public RoutingNodesState RoutingNodes { get; internal set; }
39+
}
40+
}

src/Nest/Domain/State/ClusterState.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
7+
namespace Nest
8+
{
9+
public class NodeState
10+
{
11+
[JsonProperty("name")]
12+
public string Name { get; internal set; }
13+
14+
[JsonProperty("transport_address")]
15+
public string TransportAddress { get; internal set; }
16+
17+
[JsonProperty("attributes")]
18+
public Dictionary<string, string> Attributes { get; internal set; }
19+
}
20+
21+
public class RoutingTableState
22+
{
23+
[JsonProperty("indices")]
24+
public Dictionary<string, IndexRoutingTable> Indices { get; internal set; }
25+
}
26+
27+
public class IndexRoutingTable
28+
{
29+
[JsonProperty("shards")]
30+
public Dictionary<string, List<RoutingShard>> Shards { get; internal set; }
31+
}
32+
33+
public class RoutingShard
34+
{
35+
[JsonProperty("state")]
36+
public string State { get; internal set; }
37+
38+
[JsonProperty("primary")]
39+
public bool Primary { get; internal set; }
40+
41+
[JsonProperty("node")]
42+
public string Node { get; internal set; }
43+
44+
[JsonProperty("relocating_node")]
45+
public string RelocatingNode { get; internal set; }
46+
47+
[JsonProperty("shard")]
48+
public int Shard { get; internal set; }
49+
50+
[JsonProperty("index")]
51+
public string Index { get; internal set; }
52+
}
53+
54+
public class RoutingNodesState
55+
{
56+
[JsonProperty("unassigned")]
57+
public List<RoutingShard> Unassigned { get; internal set; }
58+
59+
[JsonProperty("nodes")]
60+
public Dictionary<string, List<RoutingShard>> Nodes { get; internal set; }
61+
}
62+
63+
public class MetadataState
64+
{
65+
//[JsonProperty("templates")]
66+
//public ?? Templates { get; internal set; }
67+
68+
[JsonProperty("indices")]
69+
public Dictionary<string, MetadataIndexState> Indices { get; internal set; }
70+
}
71+
72+
public class MetadataIndexState
73+
{
74+
[JsonProperty("state")]
75+
public string State { get; internal set; }
76+
77+
[JsonProperty("settings")]
78+
public Dictionary<string, string> Settings { get; internal set; }
79+
80+
//[JsonProperty("mappings")]
81+
//public Dictionary<string, MetadataIndexStateMapping> Mappings { get; internal set; }
82+
83+
//[JsonProperty("aliases")]
84+
//public ?? Aliases { get; internal set; }
85+
}
86+
}

src/Nest/ElasticClient-State.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
3+
namespace Nest
4+
{
5+
public partial class ElasticClient
6+
{
7+
/// <summary>
8+
/// Gets the health status of the cluster.
9+
/// </summary>
10+
public IClusterStateResponse ClusterState(ClusterStateInfo stateInfo, IEnumerable<string> indices = null)
11+
{
12+
var path = this.PathResolver.CreateClusterPath("state");
13+
14+
var options = new List<string>();
15+
if (indices != null && indices.HasAny() && (!stateInfo.HasFlag(ClusterStateInfo.ExcludeMetadata)))
16+
{
17+
options.Add("filter_indices=" + string.Join(",", indices));
18+
}
19+
20+
21+
if (stateInfo.HasFlag(ClusterStateInfo.ExcludeNodes))
22+
options.Add("filter_nodes=true");
23+
if (stateInfo.HasFlag(ClusterStateInfo.ExcludeRoutingTable))
24+
options.Add("filter_routing_table=true");
25+
if (stateInfo.HasFlag(ClusterStateInfo.ExcludeMetadata))
26+
options.Add("filter_metadata=true");
27+
if (stateInfo.HasFlag(ClusterStateInfo.ExcludeBlocks))
28+
options.Add("filter_blocks=true");
29+
30+
path += "?" + string.Join("&", options);
31+
32+
var status = this.Connection.GetSync(path);
33+
var r = this.ToParsedResponse<ClusterStateResponse>(status);
34+
return r;
35+
}
36+
}
37+
}

src/Nest/IElasticClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ Task<IRegisterPercolateResponse> RegisterPercolatorAsync<T>(
317317
IIndicesShardResponse Snapshot(IEnumerable<string> indices);
318318
IIndicesShardResponse Snapshot(string index);
319319
IIndicesShardResponse Snapshot<T>() where T : class;
320+
IClusterStateResponse ClusterState(ClusterStateInfo stateInfo, IEnumerable<string> indices = null);
320321
IGlobalStatsResponse Stats();
321322
IGlobalStatsResponse Stats(StatsParams parameters);
322323
IStatsResponse Stats(IEnumerable<string> indices);

src/Nest/Nest.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
<ItemGroup>
7070
<Compile Include="Domain\Connection\ConnectionStatusTracer.cs" />
7171
<Compile Include="Domain\PathAndData.cs" />
72+
<Compile Include="Domain\Responses\ClusterStateResponse.cs" />
73+
<Compile Include="Domain\State\ClusterState.cs" />
7274
<Compile Include="DSL\BulkUpdateDescriptor.cs" />
7375
<Compile Include="DSL\Filter\HasParentFilterDescriptor.cs" />
7476
<Compile Include="DSL\Query\ChildScoreType.cs" />
@@ -79,6 +81,8 @@
7981
<Compile Include="DSL\Query\ExternalFieldDeclarationDescriptor.cs" />
8082
<Compile Include="DSL\Query\IExternalFieldDeclarationDescriptor.cs" />
8183
<Compile Include="DSL\Query\MultiMatchQueryDescriptor.cs" />
84+
<Compile Include="ElasticClient-State.cs" />
85+
<Compile Include="Enums\ClusterStateInfo.cs" />
8286
<Compile Include="Extensions\NameValueCollectionExtensions.cs" />
8387
<Compile Include="Extensions\StringExtensions.cs" />
8488
<Compile Include="Extensions\TypeExtensions.cs" />

0 commit comments

Comments
 (0)