|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Graylog, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + */ |
| 17 | +package org.graylog.storage.opensearch3; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import jakarta.inject.Inject; |
| 22 | +import jakarta.json.JsonString; |
| 23 | +import jakarta.json.JsonValue; |
| 24 | +import org.graylog.plugins.datanode.DatanodeUpgradeServiceAdapter; |
| 25 | +import org.graylog.plugins.datanode.dto.ClusterState; |
| 26 | +import org.graylog.plugins.datanode.dto.FlushResponse; |
| 27 | +import org.graylog.plugins.datanode.dto.ManagerNode; |
| 28 | +import org.graylog.plugins.datanode.dto.Node; |
| 29 | +import org.graylog.plugins.datanode.dto.ShardReplication; |
| 30 | +import org.opensearch.client.json.JsonData; |
| 31 | +import org.opensearch.client.opensearch._types.HealthStatus; |
| 32 | +import org.opensearch.client.opensearch.cluster.GetClusterSettingsResponse; |
| 33 | +import org.opensearch.client.opensearch.cluster.HealthResponse; |
| 34 | +import org.opensearch.client.opensearch.cluster.PutClusterSettingsResponse; |
| 35 | +import org.opensearch.client.opensearch.cluster.StateResponse; |
| 36 | +import org.opensearch.client.opensearch.generic.Request; |
| 37 | +import org.opensearch.client.opensearch.generic.Requests; |
| 38 | +import org.opensearch.client.opensearch.generic.Response; |
| 39 | +import org.slf4j.Logger; |
| 40 | +import org.slf4j.LoggerFactory; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.io.InputStream; |
| 44 | +import java.util.Comparator; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Locale; |
| 47 | +import java.util.Optional; |
| 48 | +import java.util.stream.Collectors; |
| 49 | +import java.util.stream.StreamSupport; |
| 50 | + |
| 51 | +public class DatanodeUpgradeServiceAdapterOS implements DatanodeUpgradeServiceAdapter { |
| 52 | + |
| 53 | + private static final Logger LOG = LoggerFactory.getLogger(DatanodeUpgradeServiceAdapterOS.class); |
| 54 | + |
| 55 | + public static final String REPLICATION_PRIMARIES = "primaries"; |
| 56 | + public static final String REPLICATION_ALL = "all"; |
| 57 | + public static final String SETTING_CLUSTER_ROUTING_ALLOCATION_ENABLE = "cluster.routing.allocation.enable"; |
| 58 | + private final OfficialOpensearchClient officialOpensearchClient; |
| 59 | + private final ObjectMapper objectMapper; |
| 60 | + |
| 61 | + @Inject |
| 62 | + public DatanodeUpgradeServiceAdapterOS(OfficialOpensearchClient officialOpensearchClient, ObjectMapper objectMapper) { |
| 63 | + this.officialOpensearchClient = officialOpensearchClient; |
| 64 | + this.objectMapper = objectMapper; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ClusterState getClusterState() { |
| 69 | + final HealthResponse response = getClusterHealthResponse(); |
| 70 | + final String shardReplication = queryShardReplication(); |
| 71 | + final ManagerNode managerNode = managerNode(); |
| 72 | + return new ClusterState( |
| 73 | + org.graylog2.indexer.indices.HealthStatus.fromString(response.status().name()), |
| 74 | + response.clusterName(), |
| 75 | + response.numberOfNodes(), |
| 76 | + response.activeShards(), |
| 77 | + response.relocatingShards(), |
| 78 | + response.initializingShards(), |
| 79 | + response.unassignedShards(), |
| 80 | + response.activePrimaryShards(), |
| 81 | + response.delayedUnassignedShards(), |
| 82 | + Optional.ofNullable(shardReplication).map(v -> v.toUpperCase(Locale.ROOT)).map(ShardReplication::valueOf).orElse(ShardReplication.ALL), |
| 83 | + managerNode, |
| 84 | + nodesResponse()); |
| 85 | + } |
| 86 | + |
| 87 | + private HealthResponse getClusterHealthResponse() { |
| 88 | + return officialOpensearchClient.sync(c -> c.cluster().health(), "Failed to obtain cluster health!"); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public FlushResponse disableShardReplication() { |
| 93 | + LOG.info("Disabling shard replication for opensearch cluster"); |
| 94 | + final HealthStatus clusterHealthStatus = getClusterHealthResponse().status(); |
| 95 | + if (clusterHealthStatus == HealthStatus.Green) { |
| 96 | + return configureShardReplication(REPLICATION_PRIMARIES); |
| 97 | + } else { |
| 98 | + throw new IllegalStateException("Can't disable shard replication, cluster is not in healthy state. Current state: " + clusterHealthStatus); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public FlushResponse enableShardReplication() { |
| 104 | + LOG.info("Enabling shard replication for opensearch cluster"); |
| 105 | + return configureShardReplication(REPLICATION_ALL); |
| 106 | + } |
| 107 | + |
| 108 | + private String queryShardReplication() { |
| 109 | + final GetClusterSettingsResponse response = officialOpensearchClient.sync(c -> c.cluster().getSettings(settings -> settings.includeDefaults(true).flatSettings(true)), "Failed to obtain shard replication settings!"); |
| 110 | + return getSetting(SETTING_CLUSTER_ROUTING_ALLOCATION_ENABLE, response); |
| 111 | + } |
| 112 | + |
| 113 | + private static String getSetting(String setting, GetClusterSettingsResponse settings) { |
| 114 | + JsonData value = settings.transient_().getOrDefault(setting, |
| 115 | + settings.persistent().getOrDefault(setting, |
| 116 | + settings.defaults().get(setting))); |
| 117 | + if (value == null) { |
| 118 | + throw new IllegalStateException("Failed to read setting " + setting + " from cluster state"); |
| 119 | + } |
| 120 | + return value.to(String.class); |
| 121 | + } |
| 122 | + |
| 123 | + private FlushResponse configureShardReplication(String shardReplication) { |
| 124 | + final PutClusterSettingsResponse response = officialOpensearchClient.sync(c -> c.cluster().putSettings(setting -> setting.flatSettings(true).persistent(SETTING_CLUSTER_ROUTING_ALLOCATION_ENABLE, JsonData.of(shardReplication))), "Failed to configure shard replication!"); |
| 125 | + final String value = response.persistent().get(SETTING_CLUSTER_ROUTING_ALLOCATION_ENABLE).to(String.class); |
| 126 | + if (!value.equals(shardReplication)) { |
| 127 | + throw new IllegalStateException("Failed to configure shard replication. Expected cluster.routing.allocation.enable=" + shardReplication + " but was: " + value); |
| 128 | + } |
| 129 | + return flush(); |
| 130 | + } |
| 131 | + |
| 132 | + private FlushResponse flush() { |
| 133 | + LOG.info("Flushing opensearch nodes, storing all in-memory operations to segments on disk"); |
| 134 | + final org.opensearch.client.opensearch.indices.FlushResponse response = officialOpensearchClient.sync(c -> c.indices().flush(f -> f.force(true).waitIfOngoing(true)), "Failed to flush opensearch nodes!"); |
| 135 | + return new FlushResponse(response.shards().total(), response.shards().successful(), response.shards().failed()); |
| 136 | + } |
| 137 | + |
| 138 | + private List<Node> nodesResponse() { |
| 139 | + //https://github.com/opensearch-project/opensearch-java/issues/894 |
| 140 | + //final NodesInfoResponse nodes = officialOpensearchClient.sync(c -> c.nodes().info(), "Failed to obtain opensearch nodes"); |
| 141 | + final Request req = Requests.builder() |
| 142 | + .method("GET") |
| 143 | + .endpoint("/_nodes") |
| 144 | + .build(); |
| 145 | + return officialOpensearchClient.sync(c -> { |
| 146 | + try (final Response response = c.generic().execute(req)) { |
| 147 | + return parseNodesResponse(response); |
| 148 | + } |
| 149 | + }, "Failed to obtain node infos"); |
| 150 | + } |
| 151 | + |
| 152 | + private List<Node> parseNodesResponse(Response response) { |
| 153 | + return response.getBody().map(body -> { |
| 154 | + try (final InputStream is = body.body()) { |
| 155 | + final JsonNode parsed = objectMapper.readValue(is, JsonNode.class); |
| 156 | + return parseNodes(parsed.path("nodes")); |
| 157 | + } catch (IOException e) { |
| 158 | + throw new RuntimeException("Failed to parse node response from /_nodes", e); |
| 159 | + } |
| 160 | + }).orElseThrow(() -> new IllegalStateException("Failed to obtain node response")); |
| 161 | + } |
| 162 | + |
| 163 | + private ManagerNode managerNode() { |
| 164 | + // https://github.com/opensearch-project/opensearch-java/issues/1791 |
| 165 | + final StateResponse response = officialOpensearchClient.sync(c -> c.cluster().state(), "Failed to obtain manager node!"); |
| 166 | + final JsonValue json = response.valueBody().toJson(); |
| 167 | + final String managerNodeID = parseString(json.asJsonObject().get("cluster_manager_node")); |
| 168 | + final String managerNodeName = parseString(json.asJsonObject().get("nodes").asJsonObject().get(managerNodeID).asJsonObject().get("name")); |
| 169 | + return new ManagerNode(managerNodeID, managerNodeName); |
| 170 | + } |
| 171 | + |
| 172 | + private static String parseString(JsonValue clusterManagerNode) { |
| 173 | + if (clusterManagerNode instanceof JsonString jsonString) { |
| 174 | + return jsonString.getString(); |
| 175 | + } else { |
| 176 | + throw new IllegalStateException("Failed to obtain String value from json object!"); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private List<Node> parseNodes(JsonNode nodes) { |
| 181 | + return StreamSupport.stream(nodes.spliterator(), false) |
| 182 | + .map(node -> new Node( |
| 183 | + node.path("host").asText(), |
| 184 | + node.path("ip").asText(), |
| 185 | + node.path("name").asText(), |
| 186 | + node.path("version").asText(), |
| 187 | + parseRoles(node.path("roles")))) |
| 188 | + .sorted(Comparator.comparing(Node::name)) |
| 189 | + .collect(Collectors.toList()); |
| 190 | + } |
| 191 | + |
| 192 | + private List<String> parseRoles(JsonNode roles) { |
| 193 | + return StreamSupport.stream(roles.spliterator(), false).map(JsonNode::asText) |
| 194 | + .sorted(Comparator.naturalOrder()) |
| 195 | + .collect(Collectors.toList()); |
| 196 | + } |
| 197 | +} |
0 commit comments