-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterAdapterES7.java
300 lines (265 loc) · 13.7 KB
/
ClusterAdapterES7.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog.storage.elasticsearch7;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.github.joschi.jadconfig.util.Duration;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsResponse;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.IndicesOptions;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexRequest;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue;
import org.graylog.storage.elasticsearch7.cat.CatApi;
import org.graylog.storage.elasticsearch7.cat.NodeResponse;
import org.graylog2.indexer.ElasticsearchException;
import org.graylog2.indexer.cluster.ClusterAdapter;
import org.graylog2.indexer.cluster.PendingTasksStats;
import org.graylog2.indexer.cluster.health.ClusterAllocationDiskSettings;
import org.graylog2.indexer.cluster.health.ClusterAllocationDiskSettingsFactory;
import org.graylog2.indexer.cluster.health.NodeDiskUsageStats;
import org.graylog2.indexer.cluster.health.NodeFileDescriptorStats;
import org.graylog2.indexer.indices.HealthStatus;
import org.graylog2.rest.models.system.indexer.responses.ClusterHealth;
import org.graylog2.system.stats.elasticsearch.ClusterStats;
import org.graylog2.system.stats.elasticsearch.IndicesStats;
import org.graylog2.system.stats.elasticsearch.NodesStats;
import org.graylog2.system.stats.elasticsearch.ShardStats;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class ClusterAdapterES7 implements ClusterAdapter {
private static final Logger LOG = LoggerFactory.getLogger(ClusterAdapterES7.class);
private final ElasticsearchClient client;
private final Duration requestTimeout;
private final CatApi catApi;
private final PlainJsonApi jsonApi;
@Inject
public ClusterAdapterES7(ElasticsearchClient client,
@Named("elasticsearch_socket_timeout") Duration requestTimeout,
CatApi catApi,
PlainJsonApi jsonApi) {
this.client = client;
this.requestTimeout = requestTimeout;
this.catApi = catApi;
this.jsonApi = jsonApi;
}
@Override
public Optional<HealthStatus> health(Collection<String> indices) {
return clusterHealth(indices).map(response -> healthStatusFrom(response.getStatus()));
}
private HealthStatus healthStatusFrom(ClusterHealthStatus status) {
switch (status) {
case RED:
return HealthStatus.Red;
case YELLOW:
return HealthStatus.Yellow;
case GREEN:
return HealthStatus.Green;
}
throw new IllegalStateException("Invalid health status received: " + status);
}
@Override
public Set<NodeFileDescriptorStats> fileDescriptorStats() {
final List<NodeResponse> result = nodes();
return result.stream()
.map(node -> NodeFileDescriptorStats.create(node.name(), node.ip(), node.host(), node.fileDescriptorMax()))
.collect(Collectors.toSet());
}
private List<NodeResponse> nodes() {
final List<NodeResponse> allNodes = catApi.nodes();
final List<NodeResponse> nodesWithDiskStatistics = allNodes.stream().filter(NodeResponse::hasDiskStatistics).collect(Collectors.toList());
if (allNodes.size() != nodesWithDiskStatistics.size()) {
final List<NodeResponse> nodesWithMissingDiskStatistics = allNodes.stream().filter(nr -> !nr.hasDiskStatistics()).collect(Collectors.toList());
LOG.info("_cat/nodes API has returned " + nodesWithMissingDiskStatistics.size() + " nodes without disk statistics:");
nodesWithMissingDiskStatistics.forEach(node -> LOG.info(node.toString()));
}
return nodesWithDiskStatistics;
}
@Override
public Set<NodeDiskUsageStats> diskUsageStats() {
final List<NodeResponse> result = nodes();
return result.stream()
.map(node -> NodeDiskUsageStats.create(node.name(), node.role(), node.ip(), node.host(), node.diskUsed(), node.diskTotal(), node.diskUsedPercent()))
.collect(Collectors.toSet());
}
@Override
public ClusterAllocationDiskSettings clusterAllocationDiskSettings() {
final ClusterGetSettingsRequest request = new ClusterGetSettingsRequest();
request.includeDefaults(true);
final ClusterGetSettingsResponse response = client.execute((c, requestOptions) -> c.cluster().getSettings(request, requestOptions));
return ClusterAllocationDiskSettingsFactory.create(
Boolean.parseBoolean(response.getSetting("cluster.routing.allocation.disk.threshold_enabled")),
response.getSetting("cluster.routing.allocation.disk.watermark.low"),
response.getSetting("cluster.routing.allocation.disk.watermark.high"),
response.getSetting("cluster.routing.allocation.disk.watermark.flood_stage")
);
}
@Override
public Optional<String> nodeIdToName(String nodeId) {
return nodeById(nodeId)
.map(jsonNode -> jsonNode.get("name").asText());
}
@Override
public Optional<String> nodeIdToHostName(String nodeId) {
return nodeById(nodeId)
.map(jsonNode -> jsonNode.path("host"))
.filter(host -> !host.isMissingNode())
.map(JsonNode::asText);
}
private Optional<JsonNode> nodeById(String nodeId) {
if (Strings.isNullOrEmpty(nodeId)) {
return Optional.empty();
}
final Request request = new Request("GET", "/_nodes/" + nodeId);
return Optional.of(jsonApi.perform(request, "Unable to retrieve node information for node id " + nodeId))
.map(jsonNode -> jsonNode.path("nodes").path(nodeId))
.filter(node -> !node.isMissingNode());
}
@Override
public boolean isConnected() {
final ClusterHealthRequest request = new ClusterHealthRequest()
.timeout(new TimeValue(requestTimeout.getQuantity(), requestTimeout.getUnit()))
.local(true);
try {
final ClusterHealthResponse result = client.execute((c, requestOptions) -> c.cluster().health(request, requestOptions));
return result.getNumberOfDataNodes() > 0;
} catch (org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException e) {
if (LOG.isDebugEnabled()) {
LOG.error(e.getMessage(), e);
}
return false;
}
}
@Override
public Optional<String> clusterName(Collection<String> indices) {
return clusterHealth(indices).map(ClusterHealthResponse::getClusterName);
}
@Override
public Optional<ClusterHealth> clusterHealthStats(Collection<String> indices) {
return clusterHealth(indices)
.map(this::clusterHealthFrom);
}
private ClusterHealth clusterHealthFrom(ClusterHealthResponse response) {
return ClusterHealth.create(response.getStatus().toString().toLowerCase(Locale.ENGLISH),
ClusterHealth.ShardStatus.create(
response.getActiveShards(),
response.getInitializingShards(),
response.getRelocatingShards(),
response.getUnassignedShards()
)
);
}
@Override
public PendingTasksStats pendingTasks() {
final Request request = new Request("GET", "/_cluster/pending_tasks");
final JsonNode response = jsonApi.perform(request, "Couldn't read Elasticsearch pending cluster tasks");
final JsonNode pendingClusterTasks = response.path("tasks");
final int pendingTasksSize = pendingClusterTasks.size();
final List<Long> pendingTasksTimeInQueue = Lists.newArrayListWithCapacity(pendingTasksSize);
for (JsonNode jsonElement : pendingClusterTasks) {
if (jsonElement.has("time_in_queue_millis")) {
pendingTasksTimeInQueue.add(jsonElement.get("time_in_queue_millis").asLong());
}
}
return PendingTasksStats.create(pendingTasksSize, pendingTasksTimeInQueue);
}
@Override
public ClusterStats clusterStats() {
final Request request = new Request("GET", "/_cluster/stats/nodes/*");
final JsonNode clusterStatsResponseJson = jsonApi.perform(request,
"Couldn't read Elasticsearch cluster stats");
final String clusterName = clusterStatsResponseJson.path("cluster_name").asText();
String clusterVersion = null;
if (clusterStatsResponseJson.path("nodes").path("versions").isArray()) {
final ArrayNode versions = (ArrayNode) clusterStatsResponseJson.path("nodes").path("versions");
// We just use the first version in the "versions" array. This is not correct if there are different
// versions running in the cluster, but that is not recommended anyway.
final JsonNode versionNode = versions.path(0);
if (versionNode.getNodeType() != JsonNodeType.MISSING) {
clusterVersion = versionNode.asText();
}
}
final JsonNode countStats = clusterStatsResponseJson.path("nodes").path("count");
final NodesStats nodesStats = NodesStats.create(
countStats.path("total").asInt(-1),
countStats.path("master_only").asInt(-1),
countStats.path("data_only").asInt(-1),
countStats.path("master_data").asInt(-1),
countStats.path("client").asInt(-1)
);
final JsonNode clusterIndicesStats = clusterStatsResponseJson.path("indices");
final IndicesStats indicesStats = IndicesStats.create(
clusterIndicesStats.path("count").asInt(-1),
clusterIndicesStats.path("store").path("size_in_bytes").asLong(-1L),
clusterIndicesStats.path("fielddata").path("memory_size_in_bytes").asLong(-1L)
);
return ClusterStats.create(clusterName, clusterVersion, nodesStats, indicesStats);
}
@Override
public ShardStats shardStats(Collection<String> indices) {
return clusterHealth(indices)
.map(response -> ShardStats.create(
response.getNumberOfNodes(),
response.getNumberOfDataNodes(),
response.getActiveShards(),
response.getRelocatingShards(),
response.getActivePrimaryShards(),
response.getInitializingShards(),
response.getUnassignedShards(),
response.isTimedOut()
))
.orElseThrow(() -> new ElasticsearchException("Unable to retrieve shard stats."));
}
private Optional<ClusterHealthResponse> clusterHealth(Collection<String> indices) {
final String[] indicesAry = indices.toArray(new String[0]);
try {
if (!indices.isEmpty() && !indicesExist(indicesAry)) {
return Optional.empty();
}
final ClusterHealthRequest request = new ClusterHealthRequest(indicesAry)
.timeout(TimeValue.timeValueSeconds(Ints.saturatedCast(requestTimeout.toSeconds())))
.indicesOptions(IndicesOptions.lenientExpand());
return Optional.of(client.execute((c, requestOptions) -> c.cluster().health(request, requestOptions)));
} catch (org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException e) {
if (LOG.isDebugEnabled()) {
LOG.error("{} ({})", e.getMessage(), Optional.ofNullable(e.getCause()).map(Throwable::getMessage).orElse("n/a"), e);
} else {
LOG.error("{} ({})", e.getMessage(), Optional.ofNullable(e.getCause()).map(Throwable::getMessage).orElse("n/a"));
}
return Optional.empty();
}
}
private boolean indicesExist(String... indices) {
final GetIndexRequest getIndexRequest = new GetIndexRequest(indices);
return client.execute((c, requestOptions) -> c.indices().exists(getIndexRequest, requestOptions));
}
}