Skip to content

Commit 107c379

Browse files
committed
Fix Cluster Stats response structure
This commit fixes a change in the cluster stats response structure. Close #1494
1 parent fb742c1 commit 107c379

File tree

3 files changed

+200
-20
lines changed

3 files changed

+200
-20
lines changed

cluster_stats_test.go

+189
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package elastic
66

77
import (
8+
"encoding/json"
89
"net/url"
910
"testing"
1011
)
@@ -67,3 +68,191 @@ func TestClusterStatsURLs(t *testing.T) {
6768
}
6869
}
6970
}
71+
72+
func TestClusterStatsErrorResponse(t *testing.T) {
73+
body := `{
74+
"_nodes": {
75+
"total": 2,
76+
"successful": 1,
77+
"failed": 1,
78+
"failures": [
79+
{
80+
"type": "failed_node_exception",
81+
"reason": "Failed node [mhUZF1sPTcu2b-pIJfqQRg]",
82+
"node_id": "mhUZF1sPTcu2b-pIJfqQRg",
83+
"caused_by": {
84+
"type": "node_not_connected_exception",
85+
"reason": "[es02][172.27.0.2:9300] Node not connected"
86+
}
87+
}
88+
]
89+
},
90+
"cluster_name": "es-docker-cluster",
91+
"cluster_uuid": "r-OkEGlJTFOE8wP36G-VSg",
92+
"timestamp": 1621834319499,
93+
"indices": {
94+
"count": 0,
95+
"shards": {},
96+
"docs": {
97+
"count": 0,
98+
"deleted": 0
99+
},
100+
"store": {
101+
"size_in_bytes": 0,
102+
"reserved_in_bytes": 0
103+
},
104+
"fielddata": {
105+
"memory_size_in_bytes": 0,
106+
"evictions": 0
107+
},
108+
"query_cache": {
109+
"memory_size_in_bytes": 0,
110+
"total_count": 0,
111+
"hit_count": 0,
112+
"miss_count": 0,
113+
"cache_size": 0,
114+
"cache_count": 0,
115+
"evictions": 0
116+
},
117+
"completion": {
118+
"size_in_bytes": 0
119+
},
120+
"segments": {
121+
"count": 0,
122+
"memory_in_bytes": 0,
123+
"terms_memory_in_bytes": 0,
124+
"stored_fields_memory_in_bytes": 0,
125+
"term_vectors_memory_in_bytes": 0,
126+
"norms_memory_in_bytes": 0,
127+
"points_memory_in_bytes": 0,
128+
"doc_values_memory_in_bytes": 0,
129+
"index_writer_memory_in_bytes": 0,
130+
"version_map_memory_in_bytes": 0,
131+
"fixed_bit_set_memory_in_bytes": 0,
132+
"max_unsafe_auto_id_timestamp": -9223372036854775808,
133+
"file_sizes": {}
134+
},
135+
"mappings": {
136+
"field_types": []
137+
},
138+
"analysis": {
139+
"char_filter_types": [],
140+
"tokenizer_types": [],
141+
"filter_types": [],
142+
"analyzer_types": [],
143+
"built_in_char_filters": [],
144+
"built_in_tokenizers": [],
145+
"built_in_filters": [],
146+
"built_in_analyzers": []
147+
}
148+
},
149+
"nodes": {
150+
"count": {
151+
"total": 1,
152+
"coordinating_only": 0,
153+
"data": 1,
154+
"data_cold": 1,
155+
"data_content": 1,
156+
"data_hot": 1,
157+
"data_warm": 1,
158+
"ingest": 1,
159+
"master": 1,
160+
"ml": 1,
161+
"remote_cluster_client": 1,
162+
"transform": 1,
163+
"voting_only": 0
164+
},
165+
"versions": [
166+
"7.10.0"
167+
],
168+
"os": {
169+
"available_processors": 6,
170+
"allocated_processors": 6,
171+
"names": [
172+
{
173+
"name": "Linux",
174+
"count": 1
175+
}
176+
],
177+
"pretty_names": [
178+
{
179+
"pretty_name": "CentOS Linux 8 (Core)",
180+
"count": 1
181+
}
182+
],
183+
"mem": {
184+
"total_in_bytes": 2084679680,
185+
"free_in_bytes": 590282752,
186+
"used_in_bytes": 1494396928,
187+
"free_percent": 28,
188+
"used_percent": 72
189+
}
190+
},
191+
"process": {
192+
"cpu": {
193+
"percent": 0
194+
},
195+
"open_file_descriptors": {
196+
"min": 260,
197+
"max": 260,
198+
"avg": 260
199+
}
200+
},
201+
"jvm": {
202+
"max_uptime_in_millis": 1042623,
203+
"versions": [
204+
{
205+
"version": "15.0.1",
206+
"vm_name": "OpenJDK 64-Bit Server VM",
207+
"vm_version": "15.0.1+9",
208+
"vm_vendor": "AdoptOpenJDK",
209+
"bundled_jdk": true,
210+
"using_bundled_jdk": true,
211+
"count": 1
212+
}
213+
],
214+
"mem": {
215+
"heap_used_in_bytes": 208299344,
216+
"heap_max_in_bytes": 314572800
217+
},
218+
"threads": 32
219+
},
220+
"fs": {
221+
"total_in_bytes": 62725623808,
222+
"free_in_bytes": 26955173888,
223+
"available_in_bytes": 23738458112
224+
},
225+
"plugins": [],
226+
"network_types": {
227+
"transport_types": {
228+
"security4": 1
229+
},
230+
"http_types": {
231+
"security4": 1
232+
}
233+
},
234+
"discovery_types": {
235+
"zen": 1
236+
},
237+
"packaging_types": [
238+
{
239+
"flavor": "default",
240+
"type": "docker",
241+
"count": 1
242+
}
243+
],
244+
"ingest": {
245+
"number_of_pipelines": 0,
246+
"processor_stats": {}
247+
}
248+
}
249+
}`
250+
251+
var resp ClusterStatsResponse
252+
if err := json.Unmarshal([]byte(body), &resp); err != nil {
253+
t.Fatal(err)
254+
}
255+
if want, have := 1, len(resp.NodesStats.Failures); want != have {
256+
t.Fatalf("expected %d errors, got %d", want, have)
257+
}
258+
}

errors.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,15 @@ func IsStatusCode(err interface{}, code int) bool {
204204

205205
// ShardsInfo represents information from a shard.
206206
type ShardsInfo struct {
207-
Total int `json:"total"`
208-
Successful int `json:"successful"`
209-
Failed int `json:"failed"`
210-
Failures []*ShardFailure `json:"failures,omitempty"`
211-
Skipped int `json:"skipped,omitempty"`
212-
}
213-
214-
// ShardFailure represents details about a failure.
215-
type ShardFailure struct {
216-
Index string `json:"_index,omitempty"`
217-
Shard int `json:"_shard,omitempty"`
218-
Node string `json:"_node,omitempty"`
219-
Reason map[string]interface{} `json:"reason,omitempty"`
220-
Status string `json:"status,omitempty"`
221-
Primary bool `json:"primary,omitempty"`
207+
Total int `json:"total"`
208+
Successful int `json:"successful"`
209+
Failed int `json:"failed"`
210+
Failures []*FailedNodeException `json:"failures,omitempty"`
211+
Skipped int `json:"skipped,omitempty"`
212+
}
213+
214+
// FailedNodeException returns an error on the node level.
215+
type FailedNodeException struct {
216+
*ErrorDetails
217+
NodeId string `json:"node_id"`
222218
}

tasks_list.go

-5
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,6 @@ type TaskOperationFailure struct {
239239
Reason *ErrorDetails `json:"reason"`
240240
}
241241

242-
type FailedNodeException struct {
243-
*ErrorDetails
244-
NodeId string `json:"node_id"`
245-
}
246-
247242
type DiscoveryNode struct {
248243
Name string `json:"name"`
249244
TransportAddress string `json:"transport_address"`

0 commit comments

Comments
 (0)