From d984f5028e77e281ed08386d43de4e64e06f2bf8 Mon Sep 17 00:00:00 2001 From: Nick Knize Date: Wed, 26 Apr 2023 18:40:38 -0500 Subject: [PATCH] [Refactor] Refactors remaining ImmutableOpenMap usage to j.u.Map (#7309) Refactors all remaining usage of HPPC backed ImmutableOpenMap to j.u.Map. This is the last usage of the class so ImmutableOpenMap is completely removed in favor of java maps. Signed-off-by: Nicholas Walter Knize --- .../client/AbstractResponseTestCase.java | 18 - .../client/indices/GetIndexResponseTests.java | 17 +- .../admin/indices/create/ShrinkIndexIT.java | 61 +-- .../action/admin/indices/get/GetIndexIT.java | 5 +- .../shards/IndicesShardStoreRequestIT.java | 4 +- .../cluster/ClusterStateDiffIT.java | 12 +- .../AwarenessAttributeDecommissionIT.java | 4 +- .../opensearch/index/IndexingPressureIT.java | 11 +- .../index/ShardIndexingPressureIT.java | 11 +- .../ShardIndexingPressureSettingsIT.java | 11 +- .../RemoveCorruptedShardDataCommandIT.java | 17 +- .../recovery/IndexPrimaryRelocationIT.java | 2 +- .../indices/recovery/IndexRecoveryIT.java | 5 +- .../indices/state/CloseIndexIT.java | 2 +- .../routing/PartitionedRoutingIT.java | 2 +- .../search/SearchWeightedRoutingIT.java | 9 +- .../opensearch/search/pit/PitMultiNodeIT.java | 10 +- .../snapshots/SearchableSnapshotIT.java | 2 +- .../TransportClusterRerouteAction.java | 4 +- .../admin/indices/get/GetIndexResponse.java | 60 ++- .../indices/get/TransportGetIndexAction.java | 24 +- .../settings/get/GetSettingsResponse.java | 40 +- .../get/TransportGetSettingsAction.java | 9 +- .../shards/IndicesShardStoresResponse.java | 24 +- .../TransportIndicesShardStoresAction.java | 11 +- .../action/ingest/IngestActionForwarder.java | 2 +- .../opensearch/action/search/PitService.java | 4 +- .../support/tasks/TransportTasksAction.java | 4 +- .../ClusterAwarenessAttributesHealth.java | 5 +- .../ClusterFormationFailureHelper.java | 2 +- .../decommission/DecommissionController.java | 2 +- .../decommission/DecommissionHelper.java | 4 +- .../cluster/metadata/AutoExpandReplicas.java | 5 +- .../cluster/node/DiscoveryNodes.java | 129 +++--- .../cluster/routing/RoutingNodes.java | 7 +- .../common/collect/ImmutableOpenMap.java | 429 ------------------ .../common/io/stream/StreamInput.java | 20 - .../common/io/stream/StreamOutput.java | 24 - .../org/opensearch/discovery/PeerFinder.java | 5 +- .../opensearch/gateway/AsyncShardFetch.java | 4 +- .../java/org/opensearch/gateway/Gateway.java | 2 +- .../opensearch/gateway/GatewayAllocator.java | 10 +- .../gateway/ReplicaShardAllocator.java | 5 +- .../opensearch/index/mapper/FieldMapper.java | 48 +- .../VerifyNodeRepositoryAction.java | 8 +- .../rest/action/cat/RestIndicesAction.java | 7 +- .../transport/SniffConnectionStrategy.java | 2 +- .../indices/get/GetIndexResponseTests.java | 9 +- .../get/GetSettingsResponseTests.java | 11 +- .../IndicesShardStoreResponseTests.java | 7 +- .../bulk/TransportBulkActionIngestTests.java | 6 +- .../action/search/PitTestsUtil.java | 7 +- .../nodes/TransportNodesActionTests.java | 4 +- ...rdFailedClusterStateTaskExecutorTests.java | 5 +- .../action/shard/ShardStateActionTests.java | 2 +- .../cluster/node/DiscoveryNodesTests.java | 25 +- .../allocation/FailedNodeRoutingTests.java | 7 +- .../allocation/FailedShardsRoutingTests.java | 19 +- .../common/io/stream/BytesStreamsTests.java | 33 -- .../indices/ShardLimitValidatorTests.java | 11 +- ...ClusterStateServiceRandomUpdatesTests.java | 2 +- .../PersistentTasksClusterServiceTests.java | 5 +- .../test/hamcrest/CollectionAssertions.java | 50 -- .../test/hamcrest/CollectionMatchers.java | 115 ----- 64 files changed, 327 insertions(+), 1094 deletions(-) delete mode 100644 server/src/main/java/org/opensearch/common/collect/ImmutableOpenMap.java delete mode 100644 test/framework/src/main/java/org/opensearch/test/hamcrest/CollectionAssertions.java delete mode 100644 test/framework/src/main/java/org/opensearch/test/hamcrest/CollectionMatchers.java diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/AbstractResponseTestCase.java b/client/rest-high-level/src/test/java/org/opensearch/client/AbstractResponseTestCase.java index 3f3d8178dc9b2..3908126df23ac 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/AbstractResponseTestCase.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/AbstractResponseTestCase.java @@ -32,7 +32,6 @@ package org.opensearch.client; import org.opensearch.common.bytes.BytesReference; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.xcontent.LoggingDeprecationHandler; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.core.xcontent.ToXContent; @@ -43,10 +42,6 @@ import org.opensearch.test.OpenSearchTestCase; import java.io.IOException; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; /** * Base class for HLRC response parsing tests. @@ -103,17 +98,4 @@ public final void testFromXContent() throws IOException { protected ToXContent.Params getParams() { return ToXContent.EMPTY_PARAMS; } - - protected static void assertMapEquals(ImmutableOpenMap expected, Map actual) { - Set expectedKeys = new HashSet<>(); - Iterator keysIt = expected.keysIt(); - while (keysIt.hasNext()) { - expectedKeys.add(keysIt.next()); - } - - assertEquals(expectedKeys, actual.keySet()); - for (String key : expectedKeys) { - assertEquals(expected.get(key), actual.get(key)); - } - } } diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/indices/GetIndexResponseTests.java b/client/rest-high-level/src/test/java/org/opensearch/client/indices/GetIndexResponseTests.java index 39bfd52b4dcfc..374c33389dd3a 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/indices/GetIndexResponseTests.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/indices/GetIndexResponseTests.java @@ -37,7 +37,6 @@ import org.opensearch.client.GetAliasesResponseTests; import org.opensearch.cluster.metadata.AliasMetadata; import org.opensearch.cluster.metadata.MappingMetadata; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.IndexScopedSettings; import org.opensearch.common.settings.Settings; import org.opensearch.core.xcontent.XContentParser; @@ -64,9 +63,9 @@ protected org.opensearch.action.admin.indices.get.GetIndexResponse createServerT String[] indices = generateRandomStringArray(5, 5, false, false); final Map mappings = new HashMap<>(); final Map> aliases = new HashMap<>(); - ImmutableOpenMap.Builder settings = ImmutableOpenMap.builder(); - ImmutableOpenMap.Builder defaultSettings = ImmutableOpenMap.builder(); - ImmutableOpenMap.Builder dataStreams = ImmutableOpenMap.builder(); + final Map settings = new HashMap<>(); + final Map defaultSettings = new HashMap<>(); + final Map dataStreams = new HashMap<>(); IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS; boolean includeDefaults = randomBoolean(); for (String index : indices) { @@ -96,9 +95,9 @@ protected org.opensearch.action.admin.indices.get.GetIndexResponse createServerT indices, mappings, aliases, - settings.build(), - defaultSettings.build(), - dataStreams.build() + settings, + defaultSettings, + dataStreams ); } @@ -114,8 +113,8 @@ protected void assertInstances( ) { assertArrayEquals(serverTestInstance.getIndices(), clientInstance.getIndices()); assertEquals(serverTestInstance.getMappings(), clientInstance.getMappings()); - assertMapEquals(serverTestInstance.getSettings(), clientInstance.getSettings()); - assertMapEquals(serverTestInstance.defaultSettings(), clientInstance.getDefaultSettings()); + assertEquals(serverTestInstance.getSettings(), clientInstance.getSettings()); + assertEquals(serverTestInstance.defaultSettings(), clientInstance.getDefaultSettings()); assertEquals(serverTestInstance.getAliases(), clientInstance.getAliases()); } diff --git a/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/create/ShrinkIndexIT.java b/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/create/ShrinkIndexIT.java index ef74e7a28fec3..3579539f3998a 100644 --- a/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/create/ShrinkIndexIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/create/ShrinkIndexIT.java @@ -64,7 +64,6 @@ import org.opensearch.cluster.routing.UnassignedInfo; import org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider; import org.opensearch.common.Priority; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.Settings; import org.opensearch.common.unit.ByteSizeValue; import org.opensearch.common.unit.TimeValue; @@ -113,15 +112,9 @@ public void testCreateShrinkIndexToN() { .setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON) .get(); } - ImmutableOpenMap dataNodes = client().admin() - .cluster() - .prepareState() - .get() - .getState() - .nodes() - .getDataNodes(); + final Map dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes(); assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2); - DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class); + DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(new DiscoveryNode[0]); String mergeNode = discoveryNodes[0].getName(); // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due @@ -212,15 +205,9 @@ public void testShrinkIndexPrimaryTerm() throws Exception { internalCluster().ensureAtLeastNumDataNodes(2); prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("number_of_shards", numberOfShards)).get(); - final ImmutableOpenMap dataNodes = client().admin() - .cluster() - .prepareState() - .get() - .getState() - .nodes() - .getDataNodes(); + final Map dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes(); assertThat(dataNodes.size(), greaterThanOrEqualTo(2)); - final DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class); + final DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(new DiscoveryNode[0]); final String mergeNode = discoveryNodes[0].getName(); // This needs more than the default timeout if a large number of shards were created. ensureGreen(TimeValue.timeValueSeconds(120)); @@ -298,15 +285,9 @@ public void testCreateShrinkIndex() { for (int i = 0; i < docs; i++) { client().prepareIndex("source").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get(); } - ImmutableOpenMap dataNodes = client().admin() - .cluster() - .prepareState() - .get() - .getState() - .nodes() - .getDataNodes(); + final Map dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes(); assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2); - DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class); + DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(new DiscoveryNode[0]); // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due // to the require._name below. @@ -426,15 +407,9 @@ public void testCreateShrinkIndexFails() throws Exception { for (int i = 0; i < 20; i++) { client().prepareIndex("source").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get(); } - ImmutableOpenMap dataNodes = client().admin() - .cluster() - .prepareState() - .get() - .getState() - .nodes() - .getDataNodes(); + final Map dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes(); assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2); - DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class); + DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(new DiscoveryNode[0]); String spareNode = discoveryNodes[0].getName(); String mergeNode = discoveryNodes[1].getName(); // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node @@ -534,15 +509,9 @@ public void testCreateShrinkWithIndexSort() throws Exception { .setSource("{\"foo\" : \"bar\", \"id\" : " + i + "}", XContentType.JSON) .get(); } - ImmutableOpenMap dataNodes = client().admin() - .cluster() - .prepareState() - .get() - .getState() - .nodes() - .getDataNodes(); + final Map dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes(); assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2); - DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class); + DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(new DiscoveryNode[0]); String mergeNode = discoveryNodes[0].getName(); // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due @@ -614,14 +583,8 @@ public void testShrinkCommitsMergeOnIdle() throws Exception { client().prepareIndex("source").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get(); } client().admin().indices().prepareFlush("source").get(); - ImmutableOpenMap dataNodes = client().admin() - .cluster() - .prepareState() - .get() - .getState() - .nodes() - .getDataNodes(); - DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class); + final Map dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes(); + DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(new DiscoveryNode[0]); // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due // to the require._name below. diff --git a/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/get/GetIndexIT.java b/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/get/GetIndexIT.java index 8237f7e785395..ac92c389e9a71 100644 --- a/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/get/GetIndexIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/get/GetIndexIT.java @@ -37,7 +37,6 @@ import org.opensearch.action.support.IndicesOptions; import org.opensearch.cluster.metadata.AliasMetadata; import org.opensearch.cluster.metadata.MappingMetadata; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.Settings; import org.opensearch.index.IndexNotFoundException; import org.opensearch.test.OpenSearchIntegTestCase; @@ -254,7 +253,7 @@ private GetIndexResponse runWithRandomFeatureMethod(GetIndexRequestBuilder reque } private void assertSettings(GetIndexResponse response, String indexName) { - ImmutableOpenMap settings = response.settings(); + final Map settings = response.settings(); assertThat(settings, notNullValue()); assertThat(settings.size(), equalTo(1)); Settings indexSettings = settings.get(indexName); @@ -263,7 +262,7 @@ private void assertSettings(GetIndexResponse response, String indexName) { } private void assertNonEmptySettings(GetIndexResponse response, String indexName) { - ImmutableOpenMap settings = response.settings(); + final Map settings = response.settings(); assertThat(settings, notNullValue()); assertThat(settings.size(), equalTo(1)); Settings indexSettings = settings.get(indexName); diff --git a/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/shards/IndicesShardStoreRequestIT.java b/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/shards/IndicesShardStoreRequestIT.java index ea9f7e0a7232d..c8501171eb5da 100644 --- a/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/shards/IndicesShardStoreRequestIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/shards/IndicesShardStoreRequestIT.java @@ -43,7 +43,6 @@ import org.opensearch.cluster.routing.ShardRouting; import org.opensearch.cluster.routing.ShardRoutingState; import org.opensearch.common.collect.ImmutableOpenIntMap; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.Settings; import org.opensearch.index.Index; import org.opensearch.index.IndexService; @@ -151,8 +150,7 @@ public void testIndices() throws Exception { .indices() .shardStores(Requests.indicesShardStoresRequest().shardStatuses("all")) .get(); - ImmutableOpenMap>> shardStatuses = response - .getStoreStatuses(); + Map>> shardStatuses = response.getStoreStatuses(); assertThat(shardStatuses.containsKey(index1), equalTo(true)); assertThat(shardStatuses.containsKey(index2), equalTo(true)); assertThat(shardStatuses.get(index1).size(), equalTo(2)); diff --git a/server/src/internalClusterTest/java/org/opensearch/cluster/ClusterStateDiffIT.java b/server/src/internalClusterTest/java/org/opensearch/cluster/ClusterStateDiffIT.java index 71f441b4a117d..ead6bc40a953e 100644 --- a/server/src/internalClusterTest/java/org/opensearch/cluster/ClusterStateDiffIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/cluster/ClusterStateDiffIT.java @@ -172,9 +172,9 @@ public void testClusterStateDiffSerialization() throws Exception { assertThat(clusterStateFromDiffs.nodes().getNodes(), equalTo(clusterState.nodes().getNodes())); assertThat(clusterStateFromDiffs.nodes().getLocalNodeId(), equalTo(previousClusterStateFromDiffs.nodes().getLocalNodeId())); assertThat(clusterStateFromDiffs.nodes().getNodes(), equalTo(clusterState.nodes().getNodes())); - for (ObjectCursor node : clusterStateFromDiffs.nodes().getNodes().keys()) { - DiscoveryNode node1 = clusterState.nodes().get(node.value); - DiscoveryNode node2 = clusterStateFromDiffs.nodes().get(node.value); + for (final String node : clusterStateFromDiffs.nodes().getNodes().keySet()) { + DiscoveryNode node1 = clusterState.nodes().get(node); + DiscoveryNode node2 = clusterStateFromDiffs.nodes().get(node); assertThat(node1.getVersion(), equalTo(node2.getVersion())); assertThat(node1.getAddress(), equalTo(node2.getAddress())); assertThat(node1.getAttributes(), equalTo(node2.getAttributes())); @@ -256,7 +256,7 @@ private ClusterState.Builder randomNodes(ClusterState clusterState) { DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(clusterState.nodes()); List nodeIds = randomSubsetOf( randomInt(clusterState.nodes().getNodes().size() - 1), - clusterState.nodes().getNodes().keys().toArray(String.class) + clusterState.nodes().getNodes().keySet().toArray(new String[0]) ); for (String nodeId : nodeIds) { if (nodeId.startsWith("node-")) { @@ -291,7 +291,7 @@ private ClusterState.Builder randomRoutingTable(ClusterState clusterState) { builder.add( randomChangeToIndexRoutingTable( clusterState.routingTable().indicesRouting().get(index), - clusterState.nodes().getNodes().keys().toArray(String.class) + clusterState.nodes().getNodes().keySet().toArray(new String[0]) ) ); } @@ -299,7 +299,7 @@ private ClusterState.Builder randomRoutingTable(ClusterState clusterState) { } int additionalIndexCount = randomIntBetween(1, 20); for (int i = 0; i < additionalIndexCount; i++) { - builder.add(randomIndexRoutingTable("index-" + randomInt(), clusterState.nodes().getNodes().keys().toArray(String.class))); + builder.add(randomIndexRoutingTable("index-" + randomInt(), clusterState.nodes().getNodes().keySet().toArray(new String[0]))); } return ClusterState.builder(clusterState).routingTable(builder.build()); } diff --git a/server/src/internalClusterTest/java/org/opensearch/cluster/coordination/AwarenessAttributeDecommissionIT.java b/server/src/internalClusterTest/java/org/opensearch/cluster/coordination/AwarenessAttributeDecommissionIT.java index ed076dc2ed424..6f2d858e349f0 100644 --- a/server/src/internalClusterTest/java/org/opensearch/cluster/coordination/AwarenessAttributeDecommissionIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/cluster/coordination/AwarenessAttributeDecommissionIT.java @@ -507,7 +507,7 @@ private void assertNodesRemovedAfterZoneDecommission(boolean originalClusterMana assertEquals(clusterState.nodes().getDataNodes().size(), 8); assertEquals(clusterState.nodes().getClusterManagerNodes().size(), 2); - Iterator discoveryNodeIterator = clusterState.nodes().getNodes().valuesIt(); + Iterator discoveryNodeIterator = clusterState.nodes().getNodes().values().iterator(); while (discoveryNodeIterator.hasNext()) { // assert no node has decommissioned attribute DiscoveryNode node = discoveryNodeIterator.next(); @@ -717,7 +717,7 @@ public void testDecommissionStatusUpdatePublishedToAllNodes() throws ExecutionEx logger.info("--> Got cluster state with 4 nodes."); // assert status on nodes that are part of cluster currently - Iterator discoveryNodeIterator = clusterState.nodes().getNodes().valuesIt(); + Iterator discoveryNodeIterator = clusterState.nodes().getNodes().values().iterator(); DiscoveryNode clusterManagerNodeAfterDecommission = null; while (discoveryNodeIterator.hasNext()) { // assert no node has decommissioned attribute diff --git a/server/src/internalClusterTest/java/org/opensearch/index/IndexingPressureIT.java b/server/src/internalClusterTest/java/org/opensearch/index/IndexingPressureIT.java index b63ffb2653741..17bd8bbe0ae80 100644 --- a/server/src/internalClusterTest/java/org/opensearch/index/IndexingPressureIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/index/IndexingPressureIT.java @@ -437,7 +437,16 @@ public Settings onNodeStopped(String nodeName) { } private String getCoordinatingOnlyNode() { - return client().admin().cluster().prepareState().get().getState().nodes().getCoordinatingOnlyNodes().iterator().next().value + return client().admin() + .cluster() + .prepareState() + .get() + .getState() + .nodes() + .getCoordinatingOnlyNodes() + .values() + .iterator() + .next() .getName(); } diff --git a/server/src/internalClusterTest/java/org/opensearch/index/ShardIndexingPressureIT.java b/server/src/internalClusterTest/java/org/opensearch/index/ShardIndexingPressureIT.java index 0c95b71be8b91..4bb3b53b9a59f 100644 --- a/server/src/internalClusterTest/java/org/opensearch/index/ShardIndexingPressureIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/index/ShardIndexingPressureIT.java @@ -778,7 +778,16 @@ private Tuple getPrimaryReplicaNodeNames(String indexName) { } private String getCoordinatingOnlyNode() { - return client().admin().cluster().prepareState().get().getState().nodes().getCoordinatingOnlyNodes().iterator().next().value + return client().admin() + .cluster() + .prepareState() + .get() + .getState() + .nodes() + .getCoordinatingOnlyNodes() + .values() + .iterator() + .next() .getName(); } diff --git a/server/src/internalClusterTest/java/org/opensearch/index/ShardIndexingPressureSettingsIT.java b/server/src/internalClusterTest/java/org/opensearch/index/ShardIndexingPressureSettingsIT.java index dafc8fd16e892..393e867f2e578 100644 --- a/server/src/internalClusterTest/java/org/opensearch/index/ShardIndexingPressureSettingsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/index/ShardIndexingPressureSettingsIT.java @@ -920,7 +920,16 @@ private Tuple getPrimaryReplicaNodeNames(String indexName) { } private String getCoordinatingOnlyNode() { - return client().admin().cluster().prepareState().get().getState().nodes().getCoordinatingOnlyNodes().iterator().next().value + return client().admin() + .cluster() + .prepareState() + .get() + .getState() + .nodes() + .getCoordinatingOnlyNodes() + .values() + .iterator() + .next() .getName(); } diff --git a/server/src/internalClusterTest/java/org/opensearch/index/shard/RemoveCorruptedShardDataCommandIT.java b/server/src/internalClusterTest/java/org/opensearch/index/shard/RemoveCorruptedShardDataCommandIT.java index 7f7914b1ead87..28f7db77738ba 100644 --- a/server/src/internalClusterTest/java/org/opensearch/index/shard/RemoveCorruptedShardDataCommandIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/index/shard/RemoveCorruptedShardDataCommandIT.java @@ -31,7 +31,6 @@ package org.opensearch.index.shard; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.carrotsearch.randomizedtesting.generators.RandomPicks; import joptsimple.OptionParser; import joptsimple.OptionSet; @@ -235,10 +234,10 @@ public Settings onNodeStopped(String nodeName) throws Exception { String nodeId = null; final ClusterState state = client().admin().cluster().prepareState().get().getState(); final DiscoveryNodes nodes = state.nodes(); - for (ObjectObjectCursor cursor : nodes.getNodes()) { - final String name = cursor.value.getName(); + for (final Map.Entry cursor : nodes.getNodes().entrySet()) { + final String name = cursor.getValue().getName(); if (name.equals(node)) { - nodeId = cursor.key; + nodeId = cursor.getKey(); break; } } @@ -423,10 +422,10 @@ public Settings onNodeStopped(String nodeName) throws Exception { String primaryNodeId = null; final ClusterState state = client().admin().cluster().prepareState().get().getState(); final DiscoveryNodes nodes = state.nodes(); - for (ObjectObjectCursor cursor : nodes.getNodes()) { - final String name = cursor.value.getName(); + for (final Map.Entry cursor : nodes.getNodes().entrySet()) { + final String name = cursor.getValue().getName(); if (name.equals(node1)) { - primaryNodeId = cursor.key; + primaryNodeId = cursor.getKey(); break; } } @@ -630,8 +629,8 @@ public void testResolvePath() throws Exception { final Map nodeNameToNodeId = new HashMap<>(); final ClusterState state = client().admin().cluster().prepareState().get().getState(); final DiscoveryNodes nodes = state.nodes(); - for (ObjectObjectCursor cursor : nodes.getNodes()) { - nodeNameToNodeId.put(cursor.value.getName(), cursor.key); + for (final Map.Entry cursor : nodes.getNodes().entrySet()) { + nodeNameToNodeId.put(cursor.getValue().getName(), cursor.getKey()); } final GroupShardsIterator shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(new String[] { indexName }, false); diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexPrimaryRelocationIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexPrimaryRelocationIT.java index 08cf33a342c65..32a10451a0dd3 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexPrimaryRelocationIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexPrimaryRelocationIT.java @@ -83,7 +83,7 @@ public void run() { indexingThread.start(); ClusterState initialState = client().admin().cluster().prepareState().get().getState(); - DiscoveryNode[] dataNodes = initialState.getNodes().getDataNodes().values().toArray(DiscoveryNode.class); + DiscoveryNode[] dataNodes = initialState.getNodes().getDataNodes().values().toArray(new DiscoveryNode[0]); DiscoveryNode relocationSource = initialState.getNodes() .getDataNodes() .get(initialState.getRoutingTable().shardRoutingTable("test", 0).primaryShard().currentNodeId()); diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexRecoveryIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexRecoveryIT.java index e448b018dd061..d04c31c0d6e24 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexRecoveryIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexRecoveryIT.java @@ -139,6 +139,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Spliterators; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Semaphore; @@ -1536,8 +1537,8 @@ public void testRecoverLocallyUpToGlobalCheckpoint() throws Exception { internalCluster().ensureAtLeastNumDataNodes(2); List nodes = randomSubsetOf( 2, - StreamSupport.stream(clusterService().state().nodes().getDataNodes().spliterator(), false) - .map(node -> node.value.getName()) + StreamSupport.stream(Spliterators.spliterator(clusterService().state().nodes().getDataNodes().values(), 0), false) + .map(node -> node.getName()) .collect(Collectors.toSet()) ); String indexName = "test-index"; diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/state/CloseIndexIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/state/CloseIndexIT.java index 41749a9bfd0f4..28bd5a6ae252d 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/state/CloseIndexIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/state/CloseIndexIT.java @@ -462,7 +462,7 @@ public void testRecoverExistingReplica() throws Exception { internalCluster().ensureAtLeastNumDataNodes(2); List dataNodes = randomSubsetOf( 2, - Sets.newHashSet(clusterService().state().nodes().getDataNodes().valuesIt()) + Sets.newHashSet(clusterService().state().nodes().getDataNodes().values().iterator()) .stream() .map(DiscoveryNode::getName) .collect(Collectors.toSet()) diff --git a/server/src/internalClusterTest/java/org/opensearch/routing/PartitionedRoutingIT.java b/server/src/internalClusterTest/java/org/opensearch/routing/PartitionedRoutingIT.java index a64e857f089f0..3fd0c954398e8 100644 --- a/server/src/internalClusterTest/java/org/opensearch/routing/PartitionedRoutingIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/routing/PartitionedRoutingIT.java @@ -133,7 +133,7 @@ public void testShrinking() throws Exception { .nodes() .getDataNodes() .values() - .toArray(DiscoveryNode.class)[0].getName() + .toArray(new DiscoveryNode[0])[0].getName() ) .put("index.blocks.write", true) ) diff --git a/server/src/internalClusterTest/java/org/opensearch/search/SearchWeightedRoutingIT.java b/server/src/internalClusterTest/java/org/opensearch/search/SearchWeightedRoutingIT.java index 422527506ed6c..a0bb2989b8328 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/SearchWeightedRoutingIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/SearchWeightedRoutingIT.java @@ -26,7 +26,6 @@ import org.opensearch.cluster.routing.WeightedRouting; import org.opensearch.cluster.routing.WeightedRoutingStats; import org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.Settings; import org.opensearch.index.query.QueryBuilders; import org.opensearch.index.search.stats.SearchStats; @@ -800,10 +799,10 @@ public void testStrictWeightedRoutingWithShardPref() throws Exception { } private void assertNoSearchInAZ(String az) { - ImmutableOpenMap dataNodes = internalCluster().clusterService().state().nodes().getDataNodes(); + final Map dataNodes = internalCluster().clusterService().state().nodes().getDataNodes(); String dataNodeId = null; - for (Iterator it = dataNodes.valuesIt(); it.hasNext();) { + for (Iterator it = dataNodes.values().iterator(); it.hasNext();) { DiscoveryNode node = it.next(); if (node.getAttributes().get("zone").equals(az)) { dataNodeId = node.getId(); @@ -824,10 +823,10 @@ private void assertNoSearchInAZ(String az) { } private void assertSearchInAZ(String az) { - ImmutableOpenMap dataNodes = internalCluster().clusterService().state().nodes().getDataNodes(); + final Map dataNodes = internalCluster().clusterService().state().nodes().getDataNodes(); String dataNodeId = null; - for (Iterator it = dataNodes.valuesIt(); it.hasNext();) { + for (Iterator it = dataNodes.values().iterator(); it.hasNext();) { DiscoveryNode node = it.next(); if (node.getAttributes().get("zone").equals(az)) { dataNodeId = node.getId(); diff --git a/server/src/internalClusterTest/java/org/opensearch/search/pit/PitMultiNodeIT.java b/server/src/internalClusterTest/java/org/opensearch/search/pit/PitMultiNodeIT.java index 3df540870267b..117165bf1e4d6 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/pit/PitMultiNodeIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/pit/PitMultiNodeIT.java @@ -8,7 +8,6 @@ package org.opensearch.search.pit; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -366,8 +365,7 @@ public void testGetAllPits() throws Exception { clusterStateRequest.clear().nodes(true).routingTable(true).indices("*"); ClusterStateResponse clusterStateResponse = client().admin().cluster().state(clusterStateRequest).get(); final List nodes = new LinkedList<>(); - for (ObjectCursor cursor : clusterStateResponse.getState().nodes().getDataNodes().values()) { - DiscoveryNode node = cursor.value; + for (final DiscoveryNode node : clusterStateResponse.getState().nodes().getDataNodes().values()) { nodes.add(node); } DiscoveryNode[] disNodesArr = new DiscoveryNode[nodes.size()]; @@ -410,11 +408,7 @@ private DiscoveryNode[] getDiscoveryNodes() throws ExecutionException, Interrupt clusterStateRequest.local(false); clusterStateRequest.clear().nodes(true).routingTable(true).indices("*"); ClusterStateResponse clusterStateResponse = client().admin().cluster().state(clusterStateRequest).get(); - final List nodes = new LinkedList<>(); - for (ObjectCursor cursor : clusterStateResponse.getState().nodes().getDataNodes().values()) { - DiscoveryNode node = cursor.value; - nodes.add(node); - } + final List nodes = new LinkedList<>(clusterStateResponse.getState().nodes().getDataNodes().values()); DiscoveryNode[] disNodesArr = new DiscoveryNode[nodes.size()]; nodes.toArray(disNodesArr); return disNodesArr; diff --git a/server/src/internalClusterTest/java/org/opensearch/snapshots/SearchableSnapshotIT.java b/server/src/internalClusterTest/java/org/opensearch/snapshots/SearchableSnapshotIT.java index 9948c5edc49b0..e9021dd8ed9ef 100644 --- a/server/src/internalClusterTest/java/org/opensearch/snapshots/SearchableSnapshotIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/snapshots/SearchableSnapshotIT.java @@ -400,7 +400,7 @@ private void assertRemoteSnapshotIndexSettings(Client client, String... snapshot .indices() .getSettings(new GetSettingsRequest().indices(snapshotIndexNames)) .actionGet(); - assertEquals(snapshotIndexNames.length, settingsResponse.getIndexToSettings().keys().size()); + assertEquals(snapshotIndexNames.length, settingsResponse.getIndexToSettings().keySet().size()); for (String snapshotIndexName : snapshotIndexNames) { assertEquals( IndexModule.Type.REMOTE_SNAPSHOT.getSettingsKey(), diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/reroute/TransportClusterRerouteAction.java b/server/src/main/java/org/opensearch/action/admin/cluster/reroute/TransportClusterRerouteAction.java index e9ae23f6b9e34..218dfcbc816d9 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/reroute/TransportClusterRerouteAction.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/reroute/TransportClusterRerouteAction.java @@ -60,7 +60,6 @@ import org.opensearch.common.Priority; import org.opensearch.common.Strings; import org.opensearch.common.collect.ImmutableOpenIntMap; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.inject.Inject; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.threadpool.ThreadPool; @@ -153,8 +152,7 @@ private void verifyThenSubmitUpdate( IndicesShardStoresAction.NAME, new IndicesShardStoresRequest().indices(stalePrimaryAllocations.keySet().toArray(Strings.EMPTY_ARRAY)), new ActionListenerResponseHandler<>(ActionListener.wrap(response -> { - ImmutableOpenMap>> status = response - .getStoreStatuses(); + final Map>> status = response.getStoreStatuses(); Exception e = null; for (Map.Entry> entry : stalePrimaryAllocations.entrySet()) { final String index = entry.getKey(); diff --git a/server/src/main/java/org/opensearch/action/admin/indices/get/GetIndexResponse.java b/server/src/main/java/org/opensearch/action/admin/indices/get/GetIndexResponse.java index a745af84f4bfc..a44fcb93966bb 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/get/GetIndexResponse.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/get/GetIndexResponse.java @@ -32,13 +32,11 @@ package org.opensearch.action.admin.indices.get; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.opensearch.Version; import org.opensearch.action.ActionResponse; import org.opensearch.cluster.metadata.AliasMetadata; import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.common.Strings; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.settings.Settings; @@ -65,18 +63,18 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject private Map mappings = Map.of(); private Map> aliases = Map.of(); - private ImmutableOpenMap settings = ImmutableOpenMap.of(); - private ImmutableOpenMap defaultSettings = ImmutableOpenMap.of(); - private ImmutableOpenMap dataStreams = ImmutableOpenMap.of(); + private Map settings = Map.of(); + private Map defaultSettings = Map.of(); + private Map dataStreams = Map.of(); private final String[] indices; public GetIndexResponse( String[] indices, Map mappings, final Map> aliases, - ImmutableOpenMap settings, - ImmutableOpenMap defaultSettings, - ImmutableOpenMap dataStreams + final Map settings, + final Map defaultSettings, + final Map dataStreams ) { this.indices = indices; // to have deterministic order @@ -88,13 +86,13 @@ public GetIndexResponse( this.aliases = Collections.unmodifiableMap(aliases); } if (settings != null) { - this.settings = settings; + this.settings = Collections.unmodifiableMap(settings); } if (defaultSettings != null) { - this.defaultSettings = defaultSettings; + this.defaultSettings = Collections.unmodifiableMap(defaultSettings); } if (dataStreams != null) { - this.dataStreams = dataStreams; + this.dataStreams = Collections.unmodifiableMap(dataStreams); } } @@ -140,26 +138,26 @@ public GetIndexResponse( aliases = Collections.unmodifiableMap(aliasesMapBuilder); int settingsSize = in.readVInt(); - ImmutableOpenMap.Builder settingsMapBuilder = ImmutableOpenMap.builder(); + final Map settingsMapBuilder = new HashMap<>(); for (int i = 0; i < settingsSize; i++) { String key = in.readString(); settingsMapBuilder.put(key, Settings.readSettingsFromStream(in)); } - settings = settingsMapBuilder.build(); + settings = Collections.unmodifiableMap(settingsMapBuilder); - ImmutableOpenMap.Builder defaultSettingsMapBuilder = ImmutableOpenMap.builder(); + final Map defaultSettingsMapBuilder = new HashMap<>(); int defaultSettingsSize = in.readVInt(); for (int i = 0; i < defaultSettingsSize; i++) { defaultSettingsMapBuilder.put(in.readString(), Settings.readSettingsFromStream(in)); } - defaultSettings = defaultSettingsMapBuilder.build(); + defaultSettings = Collections.unmodifiableMap(defaultSettingsMapBuilder); - ImmutableOpenMap.Builder dataStreamsMapBuilder = ImmutableOpenMap.builder(); + final Map dataStreamsMapBuilder = new HashMap<>(); int dataStreamsSize = in.readVInt(); for (int i = 0; i < dataStreamsSize; i++) { dataStreamsMapBuilder.put(in.readString(), in.readOptionalString()); } - dataStreams = dataStreamsMapBuilder.build(); + dataStreams = Collections.unmodifiableMap(dataStreamsMapBuilder); } public String[] indices() { @@ -186,15 +184,15 @@ public Map> getAliases() { return aliases(); } - public ImmutableOpenMap settings() { + public Map settings() { return settings; } - public ImmutableOpenMap dataStreams() { + public Map dataStreams() { return dataStreams; } - public ImmutableOpenMap getDataStreams() { + public Map getDataStreams() { return dataStreams(); } @@ -206,11 +204,11 @@ public ImmutableOpenMap getDataStreams() { * via {@link #settings()}. * See also {@link GetIndexRequest#includeDefaults(boolean)} */ - public ImmutableOpenMap defaultSettings() { + public Map defaultSettings() { return defaultSettings; } - public ImmutableOpenMap getSettings() { + public Map getSettings() { return settings(); } @@ -263,19 +261,19 @@ public void writeTo(StreamOutput out) throws IOException { } } out.writeVInt(settings.size()); - for (ObjectObjectCursor indexEntry : settings) { - out.writeString(indexEntry.key); - Settings.writeSettingsToStream(indexEntry.value, out); + for (final Map.Entry indexEntry : settings.entrySet()) { + out.writeString(indexEntry.getKey()); + Settings.writeSettingsToStream(indexEntry.getValue(), out); } out.writeVInt(defaultSettings.size()); - for (ObjectObjectCursor indexEntry : defaultSettings) { - out.writeString(indexEntry.key); - Settings.writeSettingsToStream(indexEntry.value, out); + for (final Map.Entry indexEntry : defaultSettings.entrySet()) { + out.writeString(indexEntry.getKey()); + Settings.writeSettingsToStream(indexEntry.getValue(), out); } out.writeVInt(dataStreams.size()); - for (ObjectObjectCursor indexEntry : dataStreams) { - out.writeString(indexEntry.key); - out.writeOptionalString(indexEntry.value); + for (final Map.Entry indexEntry : dataStreams.entrySet()) { + out.writeString(indexEntry.getKey()); + out.writeOptionalString(indexEntry.getValue()); } } diff --git a/server/src/main/java/org/opensearch/action/admin/indices/get/TransportGetIndexAction.java b/server/src/main/java/org/opensearch/action/admin/indices/get/TransportGetIndexAction.java index 1a32560c8d341..558f4f513966b 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/get/TransportGetIndexAction.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/get/TransportGetIndexAction.java @@ -41,7 +41,6 @@ import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.inject.Inject; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.settings.IndexScopedSettings; @@ -52,6 +51,7 @@ import org.opensearch.transport.TransportService; import java.io.IOException; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Spliterators; @@ -108,14 +108,12 @@ protected void doClusterManagerOperation( ) { Map mappingsResult = Map.of(); Map> aliasesResult = Map.of(); - ImmutableOpenMap settings = ImmutableOpenMap.of(); - ImmutableOpenMap defaultSettings = ImmutableOpenMap.of(); - ImmutableOpenMap dataStreams = ImmutableOpenMap.builder() - .putAll( - StreamSupport.stream(Spliterators.spliterator(state.metadata().findDataStreams(concreteIndices).entrySet(), 0), false) - .collect(Collectors.toMap(k -> k.getKey(), v -> v.getValue().getName())) - ) - .build(); + Map settings = Map.of(); + Map defaultSettings = Map.of(); + final Map dataStreams = new HashMap<>( + StreamSupport.stream(Spliterators.spliterator(state.metadata().findDataStreams(concreteIndices).entrySet(), 0), false) + .collect(Collectors.toMap(k -> k.getKey(), v -> v.getValue().getName())) + ); GetIndexRequest.Feature[] features = request.features(); boolean doneAliases = false; boolean doneMappings = false; @@ -141,8 +139,8 @@ protected void doClusterManagerOperation( break; case SETTINGS: if (!doneSettings) { - ImmutableOpenMap.Builder settingsMapBuilder = ImmutableOpenMap.builder(); - ImmutableOpenMap.Builder defaultSettingsMapBuilder = ImmutableOpenMap.builder(); + final Map settingsMapBuilder = new HashMap<>(); + final Map defaultSettingsMapBuilder = new HashMap<>(); for (String index : concreteIndices) { Settings indexSettings = state.metadata().index(index).getSettings(); if (request.humanReadable()) { @@ -156,8 +154,8 @@ protected void doClusterManagerOperation( defaultSettingsMapBuilder.put(index, defaultIndexSettings); } } - settings = settingsMapBuilder.build(); - defaultSettings = defaultSettingsMapBuilder.build(); + settings = settingsMapBuilder; + defaultSettings = defaultSettingsMapBuilder; doneSettings = true; } break; diff --git a/server/src/main/java/org/opensearch/action/admin/indices/settings/get/GetSettingsResponse.java b/server/src/main/java/org/opensearch/action/admin/indices/settings/get/GetSettingsResponse.java index 5ddd9e9c910c3..86008630077ec 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/settings/get/GetSettingsResponse.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/settings/get/GetSettingsResponse.java @@ -32,10 +32,8 @@ package org.opensearch.action.admin.indices.settings.get; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.opensearch.action.ActionResponse; import org.opensearch.common.Strings; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.settings.Settings; @@ -48,6 +46,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -59,21 +58,18 @@ */ public class GetSettingsResponse extends ActionResponse implements ToXContentObject { - private final ImmutableOpenMap indexToSettings; - private final ImmutableOpenMap indexToDefaultSettings; + private final Map indexToSettings; + private final Map indexToDefaultSettings; - public GetSettingsResponse( - ImmutableOpenMap indexToSettings, - ImmutableOpenMap indexToDefaultSettings - ) { - this.indexToSettings = indexToSettings; - this.indexToDefaultSettings = indexToDefaultSettings; + public GetSettingsResponse(Map indexToSettings, Map indexToDefaultSettings) { + this.indexToSettings = Collections.unmodifiableMap(indexToSettings); + this.indexToDefaultSettings = Collections.unmodifiableMap(indexToDefaultSettings); } public GetSettingsResponse(StreamInput in) throws IOException { super(in); - indexToSettings = in.readImmutableMap(StreamInput::readString, Settings::readSettingsFromStream); - indexToDefaultSettings = in.readImmutableMap(StreamInput::readString, Settings::readSettingsFromStream); + indexToSettings = in.readMap(StreamInput::readString, Settings::readSettingsFromStream); + indexToDefaultSettings = in.readMap(StreamInput::readString, Settings::readSettingsFromStream); } /** @@ -81,7 +77,7 @@ public GetSettingsResponse(StreamInput in) throws IOException { * objects contain only those settings explicitly set on a given index. Any settings * taking effect as defaults must be accessed via {@link #getIndexToDefaultSettings()}. */ - public ImmutableOpenMap getIndexToSettings() { + public Map getIndexToSettings() { return indexToSettings; } @@ -93,7 +89,7 @@ public ImmutableOpenMap getIndexToSettings() { * via {@link #getIndexToSettings()}. * See also {@link GetSettingsRequest#includeDefaults(boolean)} */ - public ImmutableOpenMap getIndexToDefaultSettings() { + public Map getIndexToDefaultSettings() { return indexToDefaultSettings; } @@ -185,10 +181,8 @@ public static GetSettingsResponse fromXContent(XContentParser parser) throws IOE } } - ImmutableOpenMap settingsMap = ImmutableOpenMap.builder().putAll(indexToSettings).build(); - ImmutableOpenMap defaultSettingsMap = ImmutableOpenMap.builder() - .putAll(indexToDefaultSettings) - .build(); + final Map settingsMap = Collections.unmodifiableMap(indexToSettings); + final Map defaultSettingsMap = Collections.unmodifiableMap(indexToDefaultSettings); return new GetSettingsResponse(settingsMap, defaultSettingsMap); } @@ -212,18 +206,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws private XContentBuilder toXContent(XContentBuilder builder, Params params, boolean omitEmptySettings) throws IOException { builder.startObject(); - for (ObjectObjectCursor cursor : getIndexToSettings()) { + for (final Map.Entry cursor : getIndexToSettings().entrySet()) { // no settings, jump over it to shorten the response data - if (omitEmptySettings && cursor.value.isEmpty()) { + if (omitEmptySettings && cursor.getValue().isEmpty()) { continue; } - builder.startObject(cursor.key); + builder.startObject(cursor.getKey()); builder.startObject("settings"); - cursor.value.toXContent(builder, params); + cursor.getValue().toXContent(builder, params); builder.endObject(); if (indexToDefaultSettings.isEmpty() == false) { builder.startObject("defaults"); - indexToDefaultSettings.get(cursor.key).toXContent(builder, params); + indexToDefaultSettings.get(cursor.getKey()).toXContent(builder, params); builder.endObject(); } builder.endObject(); diff --git a/server/src/main/java/org/opensearch/action/admin/indices/settings/get/TransportGetSettingsAction.java b/server/src/main/java/org/opensearch/action/admin/indices/settings/get/TransportGetSettingsAction.java index cfa75167afa09..671d04fde5bcb 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/settings/get/TransportGetSettingsAction.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/settings/get/TransportGetSettingsAction.java @@ -41,7 +41,6 @@ import org.opensearch.cluster.metadata.IndexMetadata; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.inject.Inject; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.regex.Regex; @@ -54,6 +53,8 @@ import org.opensearch.transport.TransportService; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; /** * Transport action for getting index settings @@ -112,8 +113,8 @@ private static boolean isFilteredRequest(GetSettingsRequest request) { @Override protected void clusterManagerOperation(GetSettingsRequest request, ClusterState state, ActionListener listener) { Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request); - ImmutableOpenMap.Builder indexToSettingsBuilder = ImmutableOpenMap.builder(); - ImmutableOpenMap.Builder indexToDefaultSettingsBuilder = ImmutableOpenMap.builder(); + final Map indexToSettingsBuilder = new HashMap<>(); + final Map indexToDefaultSettingsBuilder = new HashMap<>(); for (Index concreteIndex : concreteIndices) { IndexMetadata indexMetadata = state.getMetadata().index(concreteIndex); if (indexMetadata == null) { @@ -138,6 +139,6 @@ protected void clusterManagerOperation(GetSettingsRequest request, ClusterState indexToDefaultSettingsBuilder.put(concreteIndex.getName(), defaultSettings); } } - listener.onResponse(new GetSettingsResponse(indexToSettingsBuilder.build(), indexToDefaultSettingsBuilder.build())); + listener.onResponse(new GetSettingsResponse(indexToSettingsBuilder, indexToDefaultSettingsBuilder)); } } diff --git a/server/src/main/java/org/opensearch/action/admin/indices/shards/IndicesShardStoresResponse.java b/server/src/main/java/org/opensearch/action/admin/indices/shards/IndicesShardStoresResponse.java index ff0a3ae638525..01321bb09323d 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/shards/IndicesShardStoresResponse.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/shards/IndicesShardStoresResponse.java @@ -33,13 +33,11 @@ package org.opensearch.action.admin.indices.shards; import com.carrotsearch.hppc.cursors.IntObjectCursor; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.opensearch.OpenSearchException; import org.opensearch.action.ActionResponse; import org.opensearch.action.support.DefaultShardOperationFailedException; import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.common.collect.ImmutableOpenIntMap; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.io.stream.Writeable; @@ -49,6 +47,7 @@ import java.io.IOException; import java.util.Collections; import java.util.List; +import java.util.Map; /** * Response for {@link IndicesShardStoresAction} @@ -271,24 +270,21 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t } } - private final ImmutableOpenMap>> storeStatuses; + private final Map>> storeStatuses; private final List failures; - public IndicesShardStoresResponse( - ImmutableOpenMap>> storeStatuses, - List failures - ) { - this.storeStatuses = storeStatuses; + public IndicesShardStoresResponse(final Map>> storeStatuses, List failures) { + this.storeStatuses = Collections.unmodifiableMap(storeStatuses); this.failures = failures; } IndicesShardStoresResponse() { - this(ImmutableOpenMap.of(), Collections.emptyList()); + this(Map.of(), Collections.emptyList()); } public IndicesShardStoresResponse(StreamInput in) throws IOException { super(in); - storeStatuses = in.readImmutableMap(StreamInput::readString, i -> { + storeStatuses = in.readMap(StreamInput::readString, i -> { int indexEntries = i.readVInt(); ImmutableOpenIntMap.Builder> shardEntries = ImmutableOpenIntMap.builder(); for (int shardCount = 0; shardCount < indexEntries; shardCount++) { @@ -303,7 +299,7 @@ public IndicesShardStoresResponse(StreamInput in) throws IOException { * Returns {@link StoreStatus}s * grouped by their index names and shard ids. */ - public ImmutableOpenMap>> getStoreStatuses() { + public Map>> getStoreStatuses() { return storeStatuses; } @@ -338,11 +334,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } builder.startObject(Fields.INDICES); - for (ObjectObjectCursor>> indexShards : storeStatuses) { - builder.startObject(indexShards.key); + for (final Map.Entry>> indexShards : storeStatuses.entrySet()) { + builder.startObject(indexShards.getKey()); builder.startObject(Fields.SHARDS); - for (IntObjectCursor> shardStatusesEntry : indexShards.value) { + for (IntObjectCursor> shardStatusesEntry : indexShards.getValue()) { builder.startObject(String.valueOf(shardStatusesEntry.key)); builder.startArray(Fields.STORES); for (StoreStatus storeStatus : shardStatusesEntry.value) { diff --git a/server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java b/server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java index cfe682e47f688..e0fb4fd922ef6 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java @@ -54,7 +54,6 @@ import org.opensearch.cluster.routing.ShardRouting; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.collect.ImmutableOpenIntMap; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.collect.Tuple; import org.opensearch.common.inject.Inject; import org.opensearch.common.io.stream.StreamInput; @@ -69,8 +68,10 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Queue; import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; @@ -230,10 +231,8 @@ protected synchronized void processAsyncFetch( } void finish() { - ImmutableOpenMap.Builder< - String, - ImmutableOpenIntMap>> indicesStoreStatusesBuilder = - ImmutableOpenMap.builder(); + final Map>> indicesStoreStatusesBuilder = + new HashMap<>(); java.util.List failureBuilder = new ArrayList<>(); for (Response fetchResponse : fetchResponses) { @@ -283,7 +282,7 @@ void finish() { } } listener.onResponse( - new IndicesShardStoresResponse(indicesStoreStatusesBuilder.build(), Collections.unmodifiableList(failureBuilder)) + new IndicesShardStoresResponse(indicesStoreStatusesBuilder, Collections.unmodifiableList(failureBuilder)) ); } diff --git a/server/src/main/java/org/opensearch/action/ingest/IngestActionForwarder.java b/server/src/main/java/org/opensearch/action/ingest/IngestActionForwarder.java index 9df82c474c498..830cb8d6fac42 100644 --- a/server/src/main/java/org/opensearch/action/ingest/IngestActionForwarder.java +++ b/server/src/main/java/org/opensearch/action/ingest/IngestActionForwarder.java @@ -82,6 +82,6 @@ private DiscoveryNode randomIngestNode() { @Override public void applyClusterState(ClusterChangedEvent event) { - ingestNodes = event.state().getNodes().getIngestNodes().values().toArray(DiscoveryNode.class); + ingestNodes = event.state().getNodes().getIngestNodes().values().toArray(new DiscoveryNode[0]); } } diff --git a/server/src/main/java/org/opensearch/action/search/PitService.java b/server/src/main/java/org/opensearch/action/search/PitService.java index 079f91db383d1..5a756d2b5d4aa 100644 --- a/server/src/main/java/org/opensearch/action/search/PitService.java +++ b/server/src/main/java/org/opensearch/action/search/PitService.java @@ -8,7 +8,6 @@ package org.opensearch.action.search; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -172,8 +171,7 @@ public Map getIndicesForPits(List pitIds) { */ public void getAllPits(ActionListener getAllPitsListener) { final List nodes = new ArrayList<>(); - for (ObjectCursor cursor : clusterService.state().nodes().getDataNodes().values()) { - DiscoveryNode node = cursor.value; + for (final DiscoveryNode node : clusterService.state().nodes().getDataNodes().values()) { nodes.add(node); } DiscoveryNode[] disNodesArr = nodes.toArray(new DiscoveryNode[0]); diff --git a/server/src/main/java/org/opensearch/action/support/tasks/TransportTasksAction.java b/server/src/main/java/org/opensearch/action/support/tasks/TransportTasksAction.java index 460a0f7875558..e2e872eea5dc0 100644 --- a/server/src/main/java/org/opensearch/action/support/tasks/TransportTasksAction.java +++ b/server/src/main/java/org/opensearch/action/support/tasks/TransportTasksAction.java @@ -44,7 +44,6 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.node.DiscoveryNodes; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.collect.Tuple; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; @@ -65,6 +64,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.function.Consumer; @@ -259,7 +259,7 @@ private AsyncAction(Task task, TasksRequest request, ActionListener nodes = clusterState.nodes().getNodes(); + final Map nodes = clusterState.nodes().getNodes(); this.nodes = new DiscoveryNode[nodesIds.length]; for (int i = 0; i < this.nodesIds.length; i++) { this.nodes[i] = nodes.get(this.nodesIds[i]); diff --git a/server/src/main/java/org/opensearch/cluster/awarenesshealth/ClusterAwarenessAttributesHealth.java b/server/src/main/java/org/opensearch/cluster/awarenesshealth/ClusterAwarenessAttributesHealth.java index cef4d1df4633a..26c4f75d446e6 100644 --- a/server/src/main/java/org/opensearch/cluster/awarenesshealth/ClusterAwarenessAttributesHealth.java +++ b/server/src/main/java/org/opensearch/cluster/awarenesshealth/ClusterAwarenessAttributesHealth.java @@ -12,7 +12,6 @@ import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.common.Strings; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.io.stream.Writeable; @@ -59,7 +58,7 @@ public ClusterAwarenessAttributesHealth( Map> attributesNodeList = new HashMap<>(); // Getting the node map for cluster - ImmutableOpenMap nodeMap = clusterState.nodes().getDataNodes(); + final Map nodeMap = clusterState.nodes().getDataNodes(); // This is the map that would store all the stats per attribute ie // health stats for rack-1, rack-2 etc. @@ -67,7 +66,7 @@ public ClusterAwarenessAttributesHealth( String attributeValue; if (!nodeMap.isEmpty()) { - Iterator iter = nodeMap.keysIt(); + Iterator iter = nodeMap.keySet().iterator(); while (iter.hasNext()) { List clusterAwarenessAttributeNodeList; String node = iter.next(); diff --git a/server/src/main/java/org/opensearch/cluster/coordination/ClusterFormationFailureHelper.java b/server/src/main/java/org/opensearch/cluster/coordination/ClusterFormationFailureHelper.java index 434bd5e562c4b..9b51e56dce966 100644 --- a/server/src/main/java/org/opensearch/cluster/coordination/ClusterFormationFailureHelper.java +++ b/server/src/main/java/org/opensearch/cluster/coordination/ClusterFormationFailureHelper.java @@ -187,7 +187,7 @@ String getDescription() { final List clusterStateNodes = StreamSupport.stream( clusterState.nodes().getClusterManagerNodes().values().spliterator(), false - ).map(n -> n.value.toString()).collect(Collectors.toList()); + ).map(n -> n.toString()).collect(Collectors.toList()); final String discoveryWillContinueDescription = String.format( Locale.ROOT, diff --git a/server/src/main/java/org/opensearch/cluster/decommission/DecommissionController.java b/server/src/main/java/org/opensearch/cluster/decommission/DecommissionController.java index 79a91ee85f016..1b605643d85fe 100644 --- a/server/src/main/java/org/opensearch/cluster/decommission/DecommissionController.java +++ b/server/src/main/java/org/opensearch/cluster/decommission/DecommissionController.java @@ -107,7 +107,7 @@ public synchronized void removeDecommissionedNodes( ); Predicate allDecommissionedNodesRemovedPredicate = clusterState -> { - Set intersection = Arrays.stream(clusterState.nodes().getNodes().values().toArray(DiscoveryNode.class)) + Set intersection = Arrays.stream(clusterState.nodes().getNodes().values().toArray(new DiscoveryNode[0])) .collect(Collectors.toSet()); intersection.retainAll(nodesToBeDecommissioned); return intersection.size() == 0; diff --git a/server/src/main/java/org/opensearch/cluster/decommission/DecommissionHelper.java b/server/src/main/java/org/opensearch/cluster/decommission/DecommissionHelper.java index 83589a2f55685..2feee082ed8c8 100644 --- a/server/src/main/java/org/opensearch/cluster/decommission/DecommissionHelper.java +++ b/server/src/main/java/org/opensearch/cluster/decommission/DecommissionHelper.java @@ -75,8 +75,8 @@ static Set filterNodesWithDecommissionAttribute( ) { Set nodesWithDecommissionAttribute = new HashSet<>(); Iterator nodesIter = onlyClusterManagerNodes - ? clusterState.nodes().getClusterManagerNodes().valuesIt() - : clusterState.nodes().getNodes().valuesIt(); + ? clusterState.nodes().getClusterManagerNodes().values().iterator() + : clusterState.nodes().getNodes().values().iterator(); while (nodesIter.hasNext()) { final DiscoveryNode node = nodesIter.next(); diff --git a/server/src/main/java/org/opensearch/cluster/metadata/AutoExpandReplicas.java b/server/src/main/java/org/opensearch/cluster/metadata/AutoExpandReplicas.java index 117b8d059b0b7..bd31350780d72 100644 --- a/server/src/main/java/org/opensearch/cluster/metadata/AutoExpandReplicas.java +++ b/server/src/main/java/org/opensearch/cluster/metadata/AutoExpandReplicas.java @@ -31,7 +31,6 @@ package org.opensearch.cluster.metadata; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.routing.allocation.RoutingAllocation; import org.opensearch.cluster.routing.allocation.decider.Decision; @@ -144,8 +143,8 @@ public boolean isEnabled() { private OptionalInt getDesiredNumberOfReplicas(IndexMetadata indexMetadata, RoutingAllocation allocation) { if (enabled) { int numMatchingDataNodes = 0; - for (ObjectCursor cursor : allocation.nodes().getDataNodes().values()) { - Decision decision = allocation.deciders().shouldAutoExpandToNode(indexMetadata, cursor.value, allocation); + for (final DiscoveryNode cursor : allocation.nodes().getDataNodes().values()) { + Decision decision = allocation.deciders().shouldAutoExpandToNode(indexMetadata, cursor, allocation); if (decision.type() != Decision.Type.NO) { numMatchingDataNodes++; } diff --git a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodes.java b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodes.java index 22669c74c2223..e214b5d6cd3b1 100644 --- a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodes.java +++ b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodes.java @@ -32,16 +32,12 @@ package org.opensearch.cluster.node; -import com.carrotsearch.hppc.ObjectHashSet; -import com.carrotsearch.hppc.cursors.ObjectCursor; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.opensearch.Version; import org.opensearch.cluster.AbstractDiffable; import org.opensearch.cluster.Diff; import org.opensearch.common.Booleans; import org.opensearch.common.Nullable; import org.opensearch.common.Strings; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.regex.Regex; @@ -51,10 +47,13 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Spliterators; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -69,10 +68,10 @@ public class DiscoveryNodes extends AbstractDiffable implements public static final DiscoveryNodes EMPTY_NODES = builder().build(); - private final ImmutableOpenMap nodes; - private final ImmutableOpenMap dataNodes; - private final ImmutableOpenMap clusterManagerNodes; - private final ImmutableOpenMap ingestNodes; + private final Map nodes; + private final Map dataNodes; + private final Map clusterManagerNodes; + private final Map ingestNodes; private final String clusterManagerNodeId; private final String localNodeId; @@ -82,10 +81,10 @@ public class DiscoveryNodes extends AbstractDiffable implements private final Version minNodeVersion; private DiscoveryNodes( - ImmutableOpenMap nodes, - ImmutableOpenMap dataNodes, - ImmutableOpenMap clusterManagerNodes, - ImmutableOpenMap ingestNodes, + final Map nodes, + final Map dataNodes, + final Map clusterManagerNodes, + final Map ingestNodes, String clusterManagerNodeId, String localNodeId, Version minNonClientNodeVersion, @@ -93,10 +92,10 @@ private DiscoveryNodes( Version maxNodeVersion, Version minNodeVersion ) { - this.nodes = nodes; - this.dataNodes = dataNodes; - this.clusterManagerNodes = clusterManagerNodes; - this.ingestNodes = ingestNodes; + this.nodes = Collections.unmodifiableMap(nodes); + this.dataNodes = Collections.unmodifiableMap(dataNodes); + this.clusterManagerNodes = Collections.unmodifiableMap(clusterManagerNodes); + this.ingestNodes = Collections.unmodifiableMap(ingestNodes); this.clusterManagerNodeId = clusterManagerNodeId; this.localNodeId = localNodeId; this.minNonClientNodeVersion = minNonClientNodeVersion; @@ -107,7 +106,7 @@ private DiscoveryNodes( @Override public Iterator iterator() { - return nodes.valuesIt(); + return nodes.values().iterator(); } /** @@ -145,7 +144,7 @@ public int getSize() { * * @return {@link Map} of the discovered nodes arranged by their ids */ - public ImmutableOpenMap getNodes() { + public Map getNodes() { return this.nodes; } @@ -154,7 +153,7 @@ public ImmutableOpenMap getNodes() { * * @return {@link Map} of the discovered data nodes arranged by their ids */ - public ImmutableOpenMap getDataNodes() { + public Map getDataNodes() { return this.dataNodes; } @@ -163,7 +162,7 @@ public ImmutableOpenMap getDataNodes() { * * @return {@link Map} of the discovered cluster-manager nodes arranged by their ids */ - public ImmutableOpenMap getClusterManagerNodes() { + public Map getClusterManagerNodes() { return this.clusterManagerNodes; } @@ -174,14 +173,14 @@ public ImmutableOpenMap getClusterManagerNodes() { * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link #getClusterManagerNodes()} */ @Deprecated - public ImmutableOpenMap getMasterNodes() { + public Map getMasterNodes() { return getClusterManagerNodes(); } /** * @return All the ingest nodes arranged by their ids */ - public ImmutableOpenMap getIngestNodes() { + public Map getIngestNodes() { return ingestNodes; } @@ -190,10 +189,10 @@ public ImmutableOpenMap getIngestNodes() { * * @return {@link Map} of the discovered cluster-manager and data nodes arranged by their ids */ - public ImmutableOpenMap getClusterManagerAndDataNodes() { - ImmutableOpenMap.Builder nodes = ImmutableOpenMap.builder(dataNodes); + public Map getClusterManagerAndDataNodes() { + final Map nodes = new HashMap<>(dataNodes); nodes.putAll(clusterManagerNodes); - return nodes.build(); + return Collections.unmodifiableMap(nodes); } /** @@ -203,7 +202,7 @@ public ImmutableOpenMap getClusterManagerAndDataNodes() { * @deprecated As of 2.2, because supporting inclusive language, replaced by {@link #getClusterManagerAndDataNodes()} */ @Deprecated - public ImmutableOpenMap getMasterAndDataNodes() { + public Map getMasterAndDataNodes() { return getClusterManagerAndDataNodes(); } @@ -212,12 +211,12 @@ public ImmutableOpenMap getMasterAndDataNodes() { * * @return {@link Map} of the coordinating only nodes arranged by their ids */ - public ImmutableOpenMap getCoordinatingOnlyNodes() { - ImmutableOpenMap.Builder nodes = ImmutableOpenMap.builder(this.nodes); - nodes.removeAll(clusterManagerNodes.keys()); - nodes.removeAll(dataNodes.keys()); - nodes.removeAll(ingestNodes.keys()); - return nodes.build(); + public Map getCoordinatingOnlyNodes() { + final HashMap nodes = new HashMap<>(this.nodes); + nodes.keySet().removeAll(clusterManagerNodes.keySet()); + nodes.keySet().removeAll(dataNodes.keySet()); + nodes.keySet().removeAll(ingestNodes.keySet()); + return Collections.unmodifiableMap(nodes); } /** @@ -225,7 +224,7 @@ public ImmutableOpenMap getCoordinatingOnlyNodes() { */ public Stream clusterManagersFirstStream() { return Stream.concat( - StreamSupport.stream(clusterManagerNodes.spliterator(), false).map(cur -> cur.value), + StreamSupport.stream(Spliterators.spliterator(clusterManagerNodes.entrySet(), 0), false).map(cur -> cur.getValue()), StreamSupport.stream(this.spliterator(), false).filter(n -> n.isClusterManagerNode() == false) ); } @@ -347,8 +346,7 @@ public DiscoveryNode getMasterNode() { * @return node identified by the given address or null if no such node exists */ public DiscoveryNode findByAddress(TransportAddress address) { - for (ObjectCursor cursor : nodes.values()) { - DiscoveryNode node = cursor.value; + for (final DiscoveryNode node : nodes.values()) { if (node.getAddress().equals(address)) { return node; } @@ -435,7 +433,7 @@ public String[] resolveNodes(String... nodes) { if (nodes == null || nodes.length == 0) { return StreamSupport.stream(this.spliterator(), false).map(DiscoveryNode::getId).toArray(String[]::new); } else { - ObjectHashSet resolvedNodesIds = new ObjectHashSet<>(nodes.length); + final HashSet resolvedNodesIds = new HashSet<>(nodes.length); for (String nodeId : nodes) { if (nodeId == null) { // don't silence the underlying issue, it is a bug, so lets fail if assertions are enabled @@ -468,27 +466,27 @@ public String[] resolveNodes(String... nodes) { String matchAttrValue = nodeId.substring(index + 1); if (DiscoveryNodeRole.DATA_ROLE.roleName().equals(matchAttrName)) { if (Booleans.parseBoolean(matchAttrValue, true)) { - resolvedNodesIds.addAll(dataNodes.keys()); + resolvedNodesIds.addAll(dataNodes.keySet()); } else { - resolvedNodesIds.removeAll(dataNodes.keys()); + resolvedNodesIds.removeAll(dataNodes.keySet()); } } else if (roleNameIsClusterManager(matchAttrName)) { if (Booleans.parseBoolean(matchAttrValue, true)) { - resolvedNodesIds.addAll(clusterManagerNodes.keys()); + resolvedNodesIds.addAll(clusterManagerNodes.keySet()); } else { - resolvedNodesIds.removeAll(clusterManagerNodes.keys()); + resolvedNodesIds.removeAll(clusterManagerNodes.keySet()); } } else if (DiscoveryNodeRole.INGEST_ROLE.roleName().equals(matchAttrName)) { if (Booleans.parseBoolean(matchAttrValue, true)) { - resolvedNodesIds.addAll(ingestNodes.keys()); + resolvedNodesIds.addAll(ingestNodes.keySet()); } else { - resolvedNodesIds.removeAll(ingestNodes.keys()); + resolvedNodesIds.removeAll(ingestNodes.keySet()); } } else if (DiscoveryNode.COORDINATING_ONLY.equals(matchAttrName)) { if (Booleans.parseBoolean(matchAttrValue, true)) { - resolvedNodesIds.addAll(getCoordinatingOnlyNodes().keys()); + resolvedNodesIds.addAll(getCoordinatingOnlyNodes().keySet()); } else { - resolvedNodesIds.removeAll(getCoordinatingOnlyNodes().keys()); + resolvedNodesIds.removeAll(getCoordinatingOnlyNodes().keySet()); } } else { for (DiscoveryNode node : this) { @@ -515,7 +513,7 @@ public String[] resolveNodes(String... nodes) { } } } - return resolvedNodesIds.toArray(String.class); + return resolvedNodesIds.toArray(new String[0]); } } @@ -741,18 +739,18 @@ public static Builder builder(DiscoveryNodes nodes) { */ public static class Builder { - private final ImmutableOpenMap.Builder nodes; + private final Map nodes; private String clusterManagerNodeId; private String localNodeId; public Builder() { - nodes = ImmutableOpenMap.builder(); + nodes = new HashMap<>(); } public Builder(DiscoveryNodes nodes) { this.clusterManagerNodeId = nodes.getClusterManagerNodeId(); this.localNodeId = nodes.getLocalNodeId(); - this.nodes = ImmutableOpenMap.builder(nodes.getNodes()); + this.nodes = new HashMap<>(nodes.getNodes()); } /** @@ -820,8 +818,7 @@ public Builder localNodeId(String localNodeId) { * exception */ private String validateAdd(DiscoveryNode node) { - for (ObjectCursor cursor : nodes.values()) { - final DiscoveryNode existingNode = cursor.value; + for (final DiscoveryNode existingNode : nodes.values()) { if (node.getAddress().equals(existingNode.getAddress()) && node.getId().equals(existingNode.getId()) == false) { return "can't add node " + node + ", found existing node " + existingNode + " with same address"; } @@ -837,22 +834,22 @@ private String validateAdd(DiscoveryNode node) { } public DiscoveryNodes build() { - ImmutableOpenMap.Builder dataNodesBuilder = ImmutableOpenMap.builder(); - ImmutableOpenMap.Builder clusterManagerNodesBuilder = ImmutableOpenMap.builder(); - ImmutableOpenMap.Builder ingestNodesBuilder = ImmutableOpenMap.builder(); + final Map dataNodesBuilder = new HashMap<>(); + final Map clusterManagerNodesBuilder = new HashMap<>(); + final Map ingestNodesBuilder = new HashMap<>(); Version minNodeVersion = null; Version maxNodeVersion = null; Version minNonClientNodeVersion = null; Version maxNonClientNodeVersion = null; - for (ObjectObjectCursor nodeEntry : nodes) { - if (nodeEntry.value.isDataNode()) { - dataNodesBuilder.put(nodeEntry.key, nodeEntry.value); + for (final Map.Entry nodeEntry : nodes.entrySet()) { + if (nodeEntry.getValue().isDataNode()) { + dataNodesBuilder.put(nodeEntry.getKey(), nodeEntry.getValue()); } - if (nodeEntry.value.isClusterManagerNode()) { - clusterManagerNodesBuilder.put(nodeEntry.key, nodeEntry.value); + if (nodeEntry.getValue().isClusterManagerNode()) { + clusterManagerNodesBuilder.put(nodeEntry.getKey(), nodeEntry.getValue()); } - final Version version = nodeEntry.value.getVersion(); - if (nodeEntry.value.isDataNode() || nodeEntry.value.isClusterManagerNode()) { + final Version version = nodeEntry.getValue().getVersion(); + if (nodeEntry.getValue().isDataNode() || nodeEntry.getValue().isClusterManagerNode()) { if (minNonClientNodeVersion == null) { minNonClientNodeVersion = version; maxNonClientNodeVersion = version; @@ -861,18 +858,18 @@ public DiscoveryNodes build() { maxNonClientNodeVersion = Version.max(maxNonClientNodeVersion, version); } } - if (nodeEntry.value.isIngestNode()) { - ingestNodesBuilder.put(nodeEntry.key, nodeEntry.value); + if (nodeEntry.getValue().isIngestNode()) { + ingestNodesBuilder.put(nodeEntry.getKey(), nodeEntry.getValue()); } minNodeVersion = minNodeVersion == null ? version : Version.min(minNodeVersion, version); maxNodeVersion = maxNodeVersion == null ? version : Version.max(maxNodeVersion, version); } return new DiscoveryNodes( - nodes.build(), - dataNodesBuilder.build(), - clusterManagerNodesBuilder.build(), - ingestNodesBuilder.build(), + nodes, + dataNodesBuilder, + clusterManagerNodesBuilder, + ingestNodesBuilder, clusterManagerNodeId, localNodeId, minNonClientNodeVersion == null ? Version.CURRENT : minNonClientNodeVersion, diff --git a/server/src/main/java/org/opensearch/cluster/routing/RoutingNodes.java b/server/src/main/java/org/opensearch/cluster/routing/RoutingNodes.java index 1f071d0e4bf08..e67c37d15d18e 100644 --- a/server/src/main/java/org/opensearch/cluster/routing/RoutingNodes.java +++ b/server/src/main/java/org/opensearch/cluster/routing/RoutingNodes.java @@ -33,7 +33,6 @@ package org.opensearch.cluster.routing; import com.carrotsearch.hppc.ObjectIntHashMap; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.logging.log4j.Logger; import org.apache.lucene.util.CollectionUtil; import org.opensearch.core.Assertions; @@ -110,9 +109,9 @@ public RoutingNodes(ClusterState clusterState, boolean readOnly) { final RoutingTable routingTable = clusterState.routingTable(); // fill in the nodeToShards with the "live" nodes - for (ObjectCursor cursor : clusterState.nodes().getDataNodes().values()) { - String nodeId = cursor.value.getId(); - this.nodesToShards.put(cursor.value.getId(), new RoutingNode(nodeId, clusterState.nodes().get(nodeId))); + for (final DiscoveryNode cursor : clusterState.nodes().getDataNodes().values()) { + String nodeId = cursor.getId(); + this.nodesToShards.put(cursor.getId(), new RoutingNode(nodeId, clusterState.nodes().get(nodeId))); } // fill in the inverse of node -> shards allocated diff --git a/server/src/main/java/org/opensearch/common/collect/ImmutableOpenMap.java b/server/src/main/java/org/opensearch/common/collect/ImmutableOpenMap.java deleted file mode 100644 index 77716c951c524..0000000000000 --- a/server/src/main/java/org/opensearch/common/collect/ImmutableOpenMap.java +++ /dev/null @@ -1,429 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.common.collect; - -import com.carrotsearch.hppc.ObjectCollection; -import com.carrotsearch.hppc.ObjectContainer; -import com.carrotsearch.hppc.ObjectLookupContainer; -import com.carrotsearch.hppc.ObjectObjectAssociativeContainer; -import com.carrotsearch.hppc.ObjectObjectHashMap; -import com.carrotsearch.hppc.ObjectObjectMap; -import com.carrotsearch.hppc.cursors.ObjectCursor; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; -import com.carrotsearch.hppc.predicates.ObjectObjectPredicate; -import com.carrotsearch.hppc.predicates.ObjectPredicate; -import com.carrotsearch.hppc.procedures.ObjectObjectProcedure; - -import java.util.Iterator; -import java.util.Map; - -/** - * An immutable map implementation based on open hash map. - *

- * Can be constructed using a {@link #builder()}, or using {@link #builder(ImmutableOpenMap)} (which is an optimized - * option to copy over existing content and modify it). - * - * @opensearch.internal - */ -public final class ImmutableOpenMap implements Iterable> { - - private final ObjectObjectHashMap map; - - private ImmutableOpenMap(ObjectObjectHashMap map) { - this.map = map; - } - - /** - * @return Returns the value associated with the given key or the default value - * for the key type, if the key is not associated with any value. - *

- * Important note: For primitive type values, the value returned for a non-existing - * key may not be the default value of the primitive type (it may be any value previously - * assigned to that slot). - */ - public VType get(KType key) { - return map.get(key); - } - - /** - * @return Returns the value associated with the given key or the provided default value if the - * key is not associated with any value. - */ - public VType getOrDefault(KType key, VType defaultValue) { - return map.getOrDefault(key, defaultValue); - } - - /** - * Returns true if this container has an association to a value for - * the given key. - */ - public boolean containsKey(KType key) { - return map.containsKey(key); - } - - /** - * @return Returns the current size (number of assigned keys) in the container. - */ - public int size() { - return map.size(); - } - - /** - * @return Return true if this hash map contains no assigned keys. - */ - public boolean isEmpty() { - return map.isEmpty(); - } - - /** - * Returns a cursor over the entries (key-value pairs) in this map. The iterator is - * implemented as a cursor and it returns the same cursor instance on every - * call to {@link Iterator#next()}. To read the current key and value use the cursor's - * public fields. An example is shown below. - *

-     * for (IntShortCursor c : intShortMap)
-     * {
-     *     System.out.println("index=" + c.index
-     *       + " key=" + c.key
-     *       + " value=" + c.value);
-     * }
-     * 
- *

- * The index field inside the cursor gives the internal index inside - * the container's implementation. The interpretation of this index depends on - * to the container. - */ - @Override - public Iterator> iterator() { - return map.iterator(); - } - - /** - * Returns a specialized view of the keys of this associated container. - * The view additionally implements {@link ObjectLookupContainer}. - */ - public ObjectLookupContainer keys() { - return map.keys(); - } - - /** - * Returns a direct iterator over the keys. - */ - public Iterator keysIt() { - final Iterator> iterator = map.keys().iterator(); - return new Iterator() { - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public KType next() { - return iterator.next().value; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - - /** - * @return Returns a container with all values stored in this map. - */ - public ObjectContainer values() { - return map.values(); - } - - /** - * Returns a direct iterator over the keys. - */ - public Iterator valuesIt() { - final Iterator> iterator = map.values().iterator(); - return new Iterator() { - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public VType next() { - return iterator.next().value; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - - @Override - public String toString() { - return map.toString(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ImmutableOpenMap that = (ImmutableOpenMap) o; - - if (!map.equals(that.map)) return false; - - return true; - } - - @Override - public int hashCode() { - return map.hashCode(); - } - - @SuppressWarnings("unchecked") - private static final ImmutableOpenMap EMPTY = new ImmutableOpenMap(new ObjectObjectHashMap()); - - @SuppressWarnings("unchecked") - public static ImmutableOpenMap of() { - return EMPTY; - } - - /** - * @return An immutable copy of the given map - */ - public static ImmutableOpenMap copyOf(ObjectObjectMap map) { - Builder builder = builder(); - builder.putAll(map); - return builder.build(); - } - - public static Builder builder() { - return new Builder<>(); - } - - public static Builder builder(int size) { - return new Builder<>(size); - } - - public static Builder builder(ImmutableOpenMap map) { - return new Builder<>(map); - } - - /** - * Builder for an immuatable map - * - * @opensearch.internal - */ - public static class Builder implements ObjectObjectMap { - private ObjectObjectHashMap map; - - public Builder() { - // noinspection unchecked - this(EMPTY); - } - - public Builder(int size) { - this.map = new ObjectObjectHashMap<>(size); - } - - public Builder(ImmutableOpenMap map) { - this.map = map.map.clone(); - } - - /** - * Builds a new instance of the - */ - public ImmutableOpenMap build() { - ObjectObjectHashMap map = this.map; - this.map = null; // nullify the map, so any operation post build will fail! (hackish, but safest) - return new ImmutableOpenMap<>(map); - } - - /** - * Puts all the entries in the map to the builder. - */ - public Builder putAll(Map map) { - for (Map.Entry entry : map.entrySet()) { - this.map.put(entry.getKey(), entry.getValue()); - } - return this; - } - - /** - * A put operation that can be used in the fluent pattern. - */ - public Builder fPut(KType key, VType value) { - map.put(key, value); - return this; - } - - @Override - public VType put(KType key, VType value) { - return map.put(key, value); - } - - @Override - public VType get(KType key) { - return map.get(key); - } - - @Override - public VType getOrDefault(KType kType, VType vType) { - return map.getOrDefault(kType, vType); - } - - @Override - public int putAll(ObjectObjectAssociativeContainer container) { - return map.putAll(container); - } - - @Override - public int putAll(Iterable> iterable) { - return map.putAll(iterable); - } - - /** - * Remove that can be used in the fluent pattern. - */ - public Builder fRemove(KType key) { - map.remove(key); - return this; - } - - @Override - public VType remove(KType key) { - return map.remove(key); - } - - @Override - public Iterator> iterator() { - return map.iterator(); - } - - @Override - public boolean containsKey(KType key) { - return map.containsKey(key); - } - - @Override - public int size() { - return map.size(); - } - - @Override - public boolean isEmpty() { - return map.isEmpty(); - } - - @Override - public int removeAll(ObjectContainer container) { - return map.removeAll(container); - } - - @Override - public int removeAll(ObjectPredicate predicate) { - return map.removeAll(predicate); - } - - @Override - public > T forEach(T procedure) { - return map.forEach(procedure); - } - - @Override - public void clear() { - map.clear(); - } - - @Override - public ObjectCollection keys() { - return map.keys(); - } - - @Override - public ObjectContainer values() { - return map.values(); - } - - @SuppressWarnings("unchecked") - public Builder cast() { - return (Builder) this; - } - - @Override - public int removeAll(ObjectObjectPredicate predicate) { - return map.removeAll(predicate); - } - - @Override - public > T forEach(T predicate) { - return map.forEach(predicate); - } - - @Override - public int indexOf(KType key) { - return map.indexOf(key); - } - - @Override - public boolean indexExists(int index) { - return map.indexExists(index); - } - - @Override - public VType indexGet(int index) { - return map.indexGet(index); - } - - @Override - public VType indexReplace(int index, VType newValue) { - return map.indexReplace(index, newValue); - } - - @Override - public void indexInsert(int index, KType key, VType value) { - map.indexInsert(index, key, value); - } - - @Override - public void release() { - map.release(); - } - - @Override - public String visualizeKeyDistribution(int characters) { - return map.visualizeKeyDistribution(characters); - } - } -} diff --git a/server/src/main/java/org/opensearch/common/io/stream/StreamInput.java b/server/src/main/java/org/opensearch/common/io/stream/StreamInput.java index 419308880f04e..62844929f12e4 100644 --- a/server/src/main/java/org/opensearch/common/io/stream/StreamInput.java +++ b/server/src/main/java/org/opensearch/common/io/stream/StreamInput.java @@ -49,7 +49,6 @@ import org.opensearch.common.Strings; import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesReference; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.geo.GeoPoint; import org.opensearch.common.settings.SecureString; import org.opensearch.common.text.Text; @@ -679,25 +678,6 @@ public Map readMap() throws IOException { return (Map) readGenericValue(); } - /** - * Read {@link ImmutableOpenMap} using given key and value readers. - * - * @param keyReader key reader - * @param valueReader value reader - */ - public ImmutableOpenMap readImmutableMap(Writeable.Reader keyReader, Writeable.Reader valueReader) - throws IOException { - final int size = readVInt(); - if (size == 0) { - return ImmutableOpenMap.of(); - } - final ImmutableOpenMap.Builder builder = ImmutableOpenMap.builder(size); - for (int i = 0; i < size; i++) { - builder.put(keyReader.read(this), valueReader.read(this)); - } - return builder.build(); - } - /** * Reads a value of unspecified type. If a collection is read then the collection will be mutable if it contains any entry but might * be immutable if it is empty. diff --git a/server/src/main/java/org/opensearch/common/io/stream/StreamOutput.java b/server/src/main/java/org/opensearch/common/io/stream/StreamOutput.java index f3c184ea288bd..18c9953a9aa9c 100644 --- a/server/src/main/java/org/opensearch/common/io/stream/StreamOutput.java +++ b/server/src/main/java/org/opensearch/common/io/stream/StreamOutput.java @@ -32,7 +32,6 @@ package org.opensearch.common.io.stream; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooOldException; @@ -51,7 +50,6 @@ import org.opensearch.common.Nullable; import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesReference; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.geo.GeoPoint; import org.opensearch.common.io.stream.Writeable.Writer; import org.opensearch.common.settings.SecureString; @@ -640,28 +638,6 @@ public final void writeMap(final Map map, final Writer keyWriter } } - /** - * Write a {@link ImmutableOpenMap} of {@code K}-type keys to {@code V}-type. - * - * @param keyWriter The key writer - * @param valueWriter The value writer - */ - public final void writeMap(final ImmutableOpenMap map, final Writer keyWriter, final Writer valueWriter) - throws IOException { - writeVInt(map.size()); - for (final ObjectObjectCursor entry : map) { - keyWriter.write(this, entry.key); - valueWriter.write(this, entry.value); - } - } - - /** - * Write a {@link ImmutableOpenMap} of {@code K}-type keys to {@code V}-type. - */ - public final void writeMap(final ImmutableOpenMap map) throws IOException { - writeMap(map, (o, k) -> k.writeTo(o), (o, v) -> v.writeTo(o)); - } - /** * Writes an {@link Instant} to the stream with nanosecond resolution */ diff --git a/server/src/main/java/org/opensearch/discovery/PeerFinder.java b/server/src/main/java/org/opensearch/discovery/PeerFinder.java index 467ed7ed80ea7..6bd87a4e6cdd0 100644 --- a/server/src/main/java/org/opensearch/discovery/PeerFinder.java +++ b/server/src/main/java/org/opensearch/discovery/PeerFinder.java @@ -32,7 +32,6 @@ package org.opensearch.discovery; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -318,8 +317,8 @@ private boolean handleWakeUp() { } logger.trace("probing cluster-manager nodes from cluster state: {}", lastAcceptedNodes); - for (ObjectCursor discoveryNodeObjectCursor : lastAcceptedNodes.getClusterManagerNodes().values()) { - startProbe(discoveryNodeObjectCursor.value.getAddress()); + for (final DiscoveryNode discoveryNodeObjectCursor : lastAcceptedNodes.getClusterManagerNodes().values()) { + startProbe(discoveryNodeObjectCursor.getAddress()); } configuredHostsResolver.resolveConfiguredHosts(providedAddresses -> { diff --git a/server/src/main/java/org/opensearch/gateway/AsyncShardFetch.java b/server/src/main/java/org/opensearch/gateway/AsyncShardFetch.java index b5c4d00228caa..dcc25faa18bc1 100644 --- a/server/src/main/java/org/opensearch/gateway/AsyncShardFetch.java +++ b/server/src/main/java/org/opensearch/gateway/AsyncShardFetch.java @@ -31,7 +31,6 @@ package org.opensearch.gateway; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.opensearch.OpenSearchTimeoutException; @@ -295,8 +294,7 @@ synchronized void clearCacheForNode(String nodeId) { */ private void fillShardCacheWithDataNodes(Map> shardCache, DiscoveryNodes nodes) { // verify that all current data nodes are there - for (ObjectObjectCursor cursor : nodes.getDataNodes()) { - DiscoveryNode node = cursor.value; + for (final DiscoveryNode node : nodes.getDataNodes().values()) { if (shardCache.containsKey(node.getId()) == false) { shardCache.put(node.getId(), new NodeEntry(node.getId())); } diff --git a/server/src/main/java/org/opensearch/gateway/Gateway.java b/server/src/main/java/org/opensearch/gateway/Gateway.java index 2648af4b73be3..27e028ced0c54 100644 --- a/server/src/main/java/org/opensearch/gateway/Gateway.java +++ b/server/src/main/java/org/opensearch/gateway/Gateway.java @@ -69,7 +69,7 @@ public Gateway( } public void performStateRecovery(final GatewayStateRecoveredListener listener) throws GatewayException { - final String[] nodesIds = clusterService.state().nodes().getClusterManagerNodes().keys().toArray(String.class); + final String[] nodesIds = clusterService.state().nodes().getClusterManagerNodes().keySet().toArray(new String[0]); logger.trace("performing state recovery from {}", Arrays.toString(nodesIds)); final TransportNodesListGatewayMetaState.NodesGatewayMetaState nodesState = listGatewayMetaState.list(nodesIds, null).actionGet(); diff --git a/server/src/main/java/org/opensearch/gateway/GatewayAllocator.java b/server/src/main/java/org/opensearch/gateway/GatewayAllocator.java index cdcf813d9ede0..0fbb48b6ca73f 100644 --- a/server/src/main/java/org/opensearch/gateway/GatewayAllocator.java +++ b/server/src/main/java/org/opensearch/gateway/GatewayAllocator.java @@ -32,7 +32,6 @@ package org.opensearch.gateway; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -59,6 +58,7 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.Spliterators; import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -203,8 +203,8 @@ public AllocateUnassignedDecision explainUnassignedShardAllocation(ShardRouting private void ensureAsyncFetchStorePrimaryRecency(RoutingAllocation allocation) { DiscoveryNodes nodes = allocation.nodes(); if (hasNewNodes(nodes)) { - final Set newEphemeralIds = StreamSupport.stream(nodes.getDataNodes().spliterator(), false) - .map(node -> node.value.getEphemeralId()) + final Set newEphemeralIds = StreamSupport.stream(Spliterators.spliterator(nodes.getDataNodes().entrySet(), 0), false) + .map(node -> node.getValue().getEphemeralId()) .collect(Collectors.toSet()); // Invalidate the cache if a data node has been added to the cluster. This ensures that we do not cancel a recovery if a node // drops out, we fetch the shard data, then some indexing happens and then the node rejoins the cluster again. There are other @@ -233,8 +233,8 @@ private static void clearCacheForPrimary( } private boolean hasNewNodes(DiscoveryNodes nodes) { - for (ObjectObjectCursor node : nodes.getDataNodes()) { - if (lastSeenEphemeralIds.contains(node.value.getEphemeralId()) == false) { + for (final DiscoveryNode node : nodes.getDataNodes().values()) { + if (lastSeenEphemeralIds.contains(node.getEphemeralId()) == false) { return true; } } diff --git a/server/src/main/java/org/opensearch/gateway/ReplicaShardAllocator.java b/server/src/main/java/org/opensearch/gateway/ReplicaShardAllocator.java index c0b831b6fe4d0..5216dd2fcb4b5 100644 --- a/server/src/main/java/org/opensearch/gateway/ReplicaShardAllocator.java +++ b/server/src/main/java/org/opensearch/gateway/ReplicaShardAllocator.java @@ -32,7 +32,6 @@ package org.opensearch.gateway; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.logging.log4j.Logger; import org.opensearch.cluster.metadata.IndexMetadata; import org.opensearch.cluster.metadata.Metadata; @@ -309,8 +308,8 @@ private static Tuple> canBeAllocated Decision madeDecision = Decision.NO; final boolean explain = allocation.debugDecision(); Map nodeDecisions = explain ? new HashMap<>() : null; - for (ObjectCursor cursor : allocation.nodes().getDataNodes().values()) { - RoutingNode node = allocation.routingNodes().node(cursor.value.getId()); + for (final DiscoveryNode cursor : allocation.nodes().getDataNodes().values()) { + RoutingNode node = allocation.routingNodes().node(cursor.getId()); if (node == null) { continue; } diff --git a/server/src/main/java/org/opensearch/index/mapper/FieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/FieldMapper.java index a2b0702edb9a3..e2f0697a65f8e 100644 --- a/server/src/main/java/org/opensearch/index/mapper/FieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/FieldMapper.java @@ -32,13 +32,9 @@ package org.opensearch.index.mapper; -import com.carrotsearch.hppc.cursors.ObjectCursor; -import com.carrotsearch.hppc.cursors.ObjectObjectCursor; - import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Setting.Property; import org.opensearch.common.settings.Settings; @@ -58,6 +54,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Spliterators; import java.util.TreeMap; import java.util.stream.StreamSupport; @@ -580,7 +577,7 @@ protected static String indexOptionToString(IndexOptions indexOption) { public static class MultiFields implements Iterable { public static MultiFields empty() { - return new MultiFields(ImmutableOpenMap.of()); + return new MultiFields(Map.of()); } /** @@ -590,7 +587,7 @@ public static MultiFields empty() { */ public static class Builder { - private final ImmutableOpenMap.Builder mapperBuilders = ImmutableOpenMap.builder(); + private final Map mapperBuilders = new HashMap<>(); public Builder add(Mapper.Builder builder) { mapperBuilders.put(builder.name(), builder); @@ -624,30 +621,25 @@ public MultiFields build(Mapper.Builder mainFieldBuilder, BuilderContext context return empty(); } else { context.path().add(mainFieldBuilder.name()); - ImmutableOpenMap.Builder mapperBuilders = this.mapperBuilders; - for (ObjectObjectCursor cursor : this.mapperBuilders) { - String key = cursor.key; - Mapper.Builder value = cursor.value; + Map mapperBuilders = this.mapperBuilders; + for (final Map.Entry cursor : this.mapperBuilders.entrySet()) { + String key = cursor.getKey(); + Mapper.Builder value = cursor.getValue(); Mapper mapper = value.build(context); assert mapper instanceof FieldMapper; mapperBuilders.put(key, mapper); } context.path().remove(); - ImmutableOpenMap.Builder mappers = mapperBuilders.cast(); - return new MultiFields(mappers.build()); + final Map mappers = (Map) mapperBuilders; + return new MultiFields(mappers); } } } - private final ImmutableOpenMap mappers; + private final Map mappers; - private MultiFields(ImmutableOpenMap mappers) { - ImmutableOpenMap.Builder builder = new ImmutableOpenMap.Builder<>(); - // we disable the all in multi-field mappers - for (ObjectObjectCursor cursor : mappers) { - builder.put(cursor.key, cursor.value); - } - this.mappers = builder.build(); + private MultiFields(final Map mappers) { + this.mappers = Collections.unmodifiableMap(mappers); } public void parse(FieldMapper mainField, ParseContext context) throws IOException { @@ -658,19 +650,17 @@ public void parse(FieldMapper mainField, ParseContext context) throws IOExceptio } context = context.createMultiFieldContext(); - context.path().add(mainField.simpleName()); - for (ObjectCursor cursor : mappers.values()) { - cursor.value.parse(context); + for (final FieldMapper cursor : mappers.values()) { + cursor.parse(context); } context.path().remove(); } public MultiFields merge(MultiFields mergeWith) { - ImmutableOpenMap.Builder newMappersBuilder = ImmutableOpenMap.builder(mappers); + final Map newMappersBuilder = new HashMap<>(mappers); - for (ObjectCursor cursor : mergeWith.mappers.values()) { - FieldMapper mergeWithMapper = cursor.value; + for (final FieldMapper mergeWithMapper : mergeWith.mappers.values()) { FieldMapper mergeIntoMapper = mappers.get(mergeWithMapper.simpleName()); if (mergeIntoMapper == null) { newMappersBuilder.put(mergeWithMapper.simpleName(), mergeWithMapper); @@ -680,19 +670,19 @@ public MultiFields merge(MultiFields mergeWith) { } } - ImmutableOpenMap mappers = newMappersBuilder.build(); + final Map mappers = Collections.unmodifiableMap(newMappersBuilder); return new MultiFields(mappers); } @Override public Iterator iterator() { - return StreamSupport.stream(mappers.values().spliterator(), false).map((p) -> (Mapper) p.value).iterator(); + return StreamSupport.stream(Spliterators.spliterator(mappers.values(), 0), false).map((p) -> (Mapper) p).iterator(); } public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (!mappers.isEmpty()) { // sort the mappers so we get consistent serialization format - Mapper[] sortedMappers = mappers.values().toArray(Mapper.class); + Mapper[] sortedMappers = mappers.values().toArray(new FieldMapper[0]); Arrays.sort(sortedMappers, new Comparator() { @Override public int compare(Mapper o1, Mapper o2) { diff --git a/server/src/main/java/org/opensearch/repositories/VerifyNodeRepositoryAction.java b/server/src/main/java/org/opensearch/repositories/VerifyNodeRepositoryAction.java index 3c6d948aee453..7a6e4291e3f72 100644 --- a/server/src/main/java/org/opensearch/repositories/VerifyNodeRepositoryAction.java +++ b/server/src/main/java/org/opensearch/repositories/VerifyNodeRepositoryAction.java @@ -32,8 +32,6 @@ package org.opensearch.repositories; -import com.carrotsearch.hppc.ObjectContainer; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -55,6 +53,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicInteger; @@ -96,10 +95,9 @@ public void verify(String repository, String verificationToken, final ActionList final DiscoveryNodes discoNodes = clusterService.state().nodes(); final DiscoveryNode localNode = discoNodes.getLocalNode(); - final ObjectContainer masterAndDataNodes = discoNodes.getClusterManagerAndDataNodes().values(); + final Collection masterAndDataNodes = discoNodes.getClusterManagerAndDataNodes().values(); final List nodes = new ArrayList<>(); - for (ObjectCursor cursor : masterAndDataNodes) { - DiscoveryNode node = cursor.value; + for (final DiscoveryNode node : masterAndDataNodes) { if (RepositoriesService.isDedicatedVotingOnlyNode(node.getRoles()) == false) { nodes.add(node); } diff --git a/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java b/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java index f04d0ab712b39..6df47bbcd8dd2 100644 --- a/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java +++ b/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java @@ -72,6 +72,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.Spliterators; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -293,8 +294,10 @@ private GroupedActionListener createGroupedListener( public void onResponse(final Collection responses) { try { GetSettingsResponse settingsResponse = extractResponse(responses, GetSettingsResponse.class); - Map indicesSettings = StreamSupport.stream(settingsResponse.getIndexToSettings().spliterator(), false) - .collect(Collectors.toMap(cursor -> cursor.key, cursor -> cursor.value)); + Map indicesSettings = StreamSupport.stream( + Spliterators.spliterator(settingsResponse.getIndexToSettings().entrySet(), 0), + false + ).collect(Collectors.toMap(cursor -> cursor.getKey(), cursor -> cursor.getValue())); ClusterStateResponse stateResponse = extractResponse(responses, ClusterStateResponse.class); Map indicesStates = StreamSupport.stream( diff --git a/server/src/main/java/org/opensearch/transport/SniffConnectionStrategy.java b/server/src/main/java/org/opensearch/transport/SniffConnectionStrategy.java index cbac3250d898e..a9bba4fe44874 100644 --- a/server/src/main/java/org/opensearch/transport/SniffConnectionStrategy.java +++ b/server/src/main/java/org/opensearch/transport/SniffConnectionStrategy.java @@ -397,7 +397,7 @@ public ClusterStateResponse read(StreamInput in) throws IOException { @Override public void handleResponse(ClusterStateResponse response) { - handleNodes(response.getState().nodes().getNodes().valuesIt()); + handleNodes(response.getState().nodes().getNodes().values().iterator()); } private void handleNodes(Iterator nodesIter) { diff --git a/server/src/test/java/org/opensearch/action/admin/indices/get/GetIndexResponseTests.java b/server/src/test/java/org/opensearch/action/admin/indices/get/GetIndexResponseTests.java index 94e20af76103e..401d2bbdf9e89 100644 --- a/server/src/test/java/org/opensearch/action/admin/indices/get/GetIndexResponseTests.java +++ b/server/src/test/java/org/opensearch/action/admin/indices/get/GetIndexResponseTests.java @@ -37,7 +37,6 @@ import org.opensearch.action.admin.indices.mapping.get.GetMappingsResponseTests; import org.opensearch.cluster.metadata.AliasMetadata; import org.opensearch.cluster.metadata.MappingMetadata; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.io.stream.Writeable; import org.opensearch.common.settings.IndexScopedSettings; import org.opensearch.common.settings.Settings; @@ -64,9 +63,9 @@ protected GetIndexResponse createTestInstance() { String[] indices = generateRandomStringArray(5, 5, false, false); final Map mappings = new HashMap<>(); final Map> aliases = new HashMap<>(); - ImmutableOpenMap.Builder settings = ImmutableOpenMap.builder(); - ImmutableOpenMap.Builder defaultSettings = ImmutableOpenMap.builder(); - ImmutableOpenMap.Builder dataStreams = ImmutableOpenMap.builder(); + Map settings = new HashMap<>(); + Map defaultSettings = new HashMap<>(); + Map dataStreams = new HashMap<>(); IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS; boolean includeDefaults = randomBoolean(); for (String index : indices) { @@ -92,6 +91,6 @@ protected GetIndexResponse createTestInstance() { dataStreams.put(index, randomAlphaOfLength(5).toLowerCase(Locale.ROOT)); } } - return new GetIndexResponse(indices, mappings, aliases, settings.build(), defaultSettings.build(), dataStreams.build()); + return new GetIndexResponse(indices, mappings, aliases, settings, defaultSettings, dataStreams); } } diff --git a/server/src/test/java/org/opensearch/action/admin/indices/settings/get/GetSettingsResponseTests.java b/server/src/test/java/org/opensearch/action/admin/indices/settings/get/GetSettingsResponseTests.java index 6de6fe6a4ddb0..9eb2f28405b69 100644 --- a/server/src/test/java/org/opensearch/action/admin/indices/settings/get/GetSettingsResponseTests.java +++ b/server/src/test/java/org/opensearch/action/admin/indices/settings/get/GetSettingsResponseTests.java @@ -32,7 +32,6 @@ package org.opensearch.action.admin.indices.settings.get; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.io.stream.Writeable; import org.opensearch.common.settings.IndexScopedSettings; import org.opensearch.common.settings.Settings; @@ -41,8 +40,10 @@ import org.opensearch.test.AbstractSerializingTestCase; import java.io.IOException; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import java.util.function.Predicate; @@ -72,9 +73,7 @@ protected GetSettingsResponse createTestInstance() { builder.put("index.refresh_interval", "1s"); indexToSettings.put(indexName, builder.build()); } - ImmutableOpenMap immutableIndexToSettings = ImmutableOpenMap.builder() - .putAll(indexToSettings) - .build(); + final Map immutableIndexToSettings = Collections.unmodifiableMap(indexToSettings); if (randomBoolean()) { for (String indexName : indexToSettings.keySet()) { @@ -83,9 +82,7 @@ protected GetSettingsResponse createTestInstance() { } } - ImmutableOpenMap immutableIndexToDefaultSettings = ImmutableOpenMap.builder() - .putAll(indexToDefaultSettings) - .build(); + final Map immutableIndexToDefaultSettings = Collections.unmodifiableMap(indexToDefaultSettings); return new GetSettingsResponse(immutableIndexToSettings, immutableIndexToDefaultSettings); } diff --git a/server/src/test/java/org/opensearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java b/server/src/test/java/org/opensearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java index 4c215b89dd96f..2d353340aa5b0 100644 --- a/server/src/test/java/org/opensearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java +++ b/server/src/test/java/org/opensearch/action/admin/indices/shards/IndicesShardStoreResponseTests.java @@ -38,7 +38,6 @@ import org.opensearch.common.UUIDs; import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.collect.ImmutableOpenIntMap; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; @@ -50,6 +49,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -59,8 +59,7 @@ public class IndicesShardStoreResponseTests extends OpenSearchTestCase { public void testBasicSerialization() throws Exception { - ImmutableOpenMap.Builder>> indexStoreStatuses = - ImmutableOpenMap.builder(); + final Map>> indexStoreStatuses = new HashMap<>(); List failures = new ArrayList<>(); ImmutableOpenIntMap.Builder> storeStatuses = ImmutableOpenIntMap.builder(); @@ -96,7 +95,7 @@ public void testBasicSerialization() throws Exception { failures.add(new IndicesShardStoresResponse.Failure("node1", "test", 3, new NodeDisconnectedException(node1, ""))); IndicesShardStoresResponse storesResponse = new IndicesShardStoresResponse( - indexStoreStatuses.build(), + indexStoreStatuses, Collections.unmodifiableList(failures) ); XContentBuilder contentBuilder = XContentFactory.jsonBuilder(); diff --git a/server/src/test/java/org/opensearch/action/bulk/TransportBulkActionIngestTests.java b/server/src/test/java/org/opensearch/action/bulk/TransportBulkActionIngestTests.java index 5881c72587756..6a514b47e55a4 100644 --- a/server/src/test/java/org/opensearch/action/bulk/TransportBulkActionIngestTests.java +++ b/server/src/test/java/org/opensearch/action/bulk/TransportBulkActionIngestTests.java @@ -57,7 +57,6 @@ import org.opensearch.cluster.node.DiscoveryNodes; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.Nullable; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.collect.MapBuilder; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; @@ -236,10 +235,7 @@ public void setupAction() { remoteNode1 = mock(DiscoveryNode.class); remoteNode2 = mock(DiscoveryNode.class); nodes = mock(DiscoveryNodes.class); - ImmutableOpenMap ingestNodes = ImmutableOpenMap.builder(2) - .fPut("node1", remoteNode1) - .fPut("node2", remoteNode2) - .build(); + final Map ingestNodes = Map.of("node1", remoteNode1, "node2", remoteNode2); when(nodes.getIngestNodes()).thenReturn(ingestNodes); when(nodes.getMinNodeVersion()).thenReturn(VersionUtils.randomCompatibleVersion(random(), Version.CURRENT)); ClusterState state = mock(ClusterState.class); diff --git a/server/src/test/java/org/opensearch/action/search/PitTestsUtil.java b/server/src/test/java/org/opensearch/action/search/PitTestsUtil.java index 3962a4a11fc90..83bcdef312b39 100644 --- a/server/src/test/java/org/opensearch/action/search/PitTestsUtil.java +++ b/server/src/test/java/org/opensearch/action/search/PitTestsUtil.java @@ -8,7 +8,6 @@ package org.opensearch.action.search; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.junit.Assert; import org.opensearch.Version; import org.opensearch.action.ActionFuture; @@ -104,8 +103,7 @@ public static void assertUsingGetAllPits(Client client, String id, long creation clusterStateRequest.clear().nodes(true).routingTable(true).indices("*"); ClusterStateResponse clusterStateResponse = client.admin().cluster().state(clusterStateRequest).get(); final List nodes = new LinkedList<>(); - for (ObjectCursor cursor : clusterStateResponse.getState().nodes().getDataNodes().values()) { - DiscoveryNode node = cursor.value; + for (final DiscoveryNode node : clusterStateResponse.getState().nodes().getDataNodes().values()) { nodes.add(node); } DiscoveryNode[] disNodesArr = new DiscoveryNode[nodes.size()]; @@ -123,8 +121,7 @@ public static void assertGetAllPitsEmpty(Client client) throws ExecutionExceptio clusterStateRequest.clear().nodes(true).routingTable(true).indices("*"); ClusterStateResponse clusterStateResponse = client.admin().cluster().state(clusterStateRequest).get(); final List nodes = new LinkedList<>(); - for (ObjectCursor cursor : clusterStateResponse.getState().nodes().getDataNodes().values()) { - DiscoveryNode node = cursor.value; + for (final DiscoveryNode node : clusterStateResponse.getState().nodes().getDataNodes().values()) { nodes.add(node); } DiscoveryNode[] disNodesArr = new DiscoveryNode[nodes.size()]; diff --git a/server/src/test/java/org/opensearch/action/support/nodes/TransportNodesActionTests.java b/server/src/test/java/org/opensearch/action/support/nodes/TransportNodesActionTests.java index 0827e4fc20255..66502efd054f0 100644 --- a/server/src/test/java/org/opensearch/action/support/nodes/TransportNodesActionTests.java +++ b/server/src/test/java/org/opensearch/action/support/nodes/TransportNodesActionTests.java @@ -100,7 +100,7 @@ public void testNodesSelectors() { nodeSelectors.add(randomFrom(NodeSelector.values()).selector); } int numNodeIds = randomIntBetween(0, 3); - String[] nodeIds = clusterService.state().nodes().getNodes().keys().toArray(String.class); + String[] nodeIds = clusterService.state().nodes().getNodes().keySet().toArray(new String[0]); for (int i = 0; i < numNodeIds; i++) { String nodeId = randomFrom(nodeIds); nodeSelectors.add(nodeId); @@ -340,7 +340,7 @@ private static class DataNodesOnlyTransportNodesAction extends TestTransportNode @Override protected void resolveRequest(TestNodesRequest request, ClusterState clusterState) { - request.setConcreteNodes(clusterState.nodes().getDataNodes().values().toArray(DiscoveryNode.class)); + request.setConcreteNodes(clusterState.nodes().getDataNodes().values().toArray(new DiscoveryNode[0])); } } diff --git a/server/src/test/java/org/opensearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java b/server/src/test/java/org/opensearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java index 3632079b97b63..2ee50156cc7cf 100644 --- a/server/src/test/java/org/opensearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java +++ b/server/src/test/java/org/opensearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java @@ -32,7 +32,6 @@ package org.opensearch.cluster.action.shard; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.lucene.index.CorruptIndexException; import org.opensearch.Version; import org.opensearch.cluster.ClusterName; @@ -275,8 +274,8 @@ private List createNonExistentShards(ClusterS String nonExistentIndexUUID = "non-existent"; Index index = new Index("non-existent", nonExistentIndexUUID); List nodeIds = new ArrayList<>(); - for (ObjectCursor nodeId : currentState.nodes().getNodes().keys()) { - nodeIds.add(nodeId.toString()); + for (final String nodeId : currentState.nodes().getNodes().keySet()) { + nodeIds.add(nodeId); } List nonExistentShards = new ArrayList<>(); nonExistentShards.add(nonExistentShardRouting(index, nodeIds, true)); diff --git a/server/src/test/java/org/opensearch/cluster/action/shard/ShardStateActionTests.java b/server/src/test/java/org/opensearch/cluster/action/shard/ShardStateActionTests.java index 95ef12f0224fd..8ae01e405b392 100644 --- a/server/src/test/java/org/opensearch/cluster/action/shard/ShardStateActionTests.java +++ b/server/src/test/java/org/opensearch/cluster/action/shard/ShardStateActionTests.java @@ -505,7 +505,7 @@ private void setUpClusterManagerRetryVerification( shardStateAction.setOnBeforeWaitForNewClusterManagerAndRetry(() -> { DiscoveryNodes.Builder clusterManagerBuilder = DiscoveryNodes.builder(clusterService.state().nodes()); clusterManagerBuilder.clusterManagerNodeId( - clusterService.state().nodes().getClusterManagerNodes().iterator().next().value.getId() + clusterService.state().nodes().getClusterManagerNodes().values().iterator().next().getId() ); setState(clusterService, ClusterState.builder(clusterService.state()).nodes(clusterManagerBuilder)); }); diff --git a/server/src/test/java/org/opensearch/cluster/node/DiscoveryNodesTests.java b/server/src/test/java/org/opensearch/cluster/node/DiscoveryNodesTests.java index dba0afc9f3641..47676ecc13f5e 100644 --- a/server/src/test/java/org/opensearch/cluster/node/DiscoveryNodesTests.java +++ b/server/src/test/java/org/opensearch/cluster/node/DiscoveryNodesTests.java @@ -64,7 +64,7 @@ public class DiscoveryNodesTests extends OpenSearchTestCase { public void testResolveNodeByIdOrName() { DiscoveryNodes discoveryNodes = buildDiscoveryNodes(); - DiscoveryNode[] nodes = discoveryNodes.getNodes().values().toArray(DiscoveryNode.class); + DiscoveryNode[] nodes = discoveryNodes.getNodes().values().toArray(new DiscoveryNode[0]); DiscoveryNode node = randomFrom(nodes); DiscoveryNode resolvedNode = discoveryNodes.resolveNode(randomBoolean() ? node.getId() : node.getName()); assertThat(resolvedNode.getId(), equalTo(node.getId())); @@ -109,7 +109,6 @@ public void testAll() { assertThat(discoveryNodes.resolveNodes("_all"), arrayContainingInAnyOrder(allNodes)); final String[] nonClusterManagerNodes = StreamSupport.stream(discoveryNodes.getNodes().values().spliterator(), false) - .map(n -> n.value) .filter(n -> n.isClusterManagerNode() == false) .map(DiscoveryNode::getId) .toArray(String[]::new); @@ -122,13 +121,11 @@ public void testCoordinatorOnlyNodes() { final DiscoveryNodes discoveryNodes = buildDiscoveryNodes(); final String[] coordinatorOnlyNodes = StreamSupport.stream(discoveryNodes.getNodes().values().spliterator(), false) - .map(n -> n.value) .filter(n -> n.isDataNode() == false && n.isIngestNode() == false && n.isClusterManagerNode() == false) .map(DiscoveryNode::getId) .toArray(String[]::new); final String[] nonCoordinatorOnlyNodes = StreamSupport.stream(discoveryNodes.getNodes().values().spliterator(), false) - .map(n -> n.value) .filter(n -> n.isClusterManagerNode() || n.isDataNode() || n.isIngestNode()) .map(DiscoveryNode::getId) .toArray(String[]::new); @@ -154,14 +151,14 @@ public void testResolveNodesIds() { } } int numNodeIds = randomIntBetween(0, 3); - String[] nodeIds = discoveryNodes.getNodes().keys().toArray(String.class); + String[] nodeIds = discoveryNodes.getNodes().keySet().toArray(new String[0]); for (int i = 0; i < numNodeIds; i++) { String nodeId = randomFrom(nodeIds); nodeSelectors.add(nodeId); expectedNodeIdsSet.add(nodeId); } int numNodeNames = randomIntBetween(0, 3); - DiscoveryNode[] nodes = discoveryNodes.getNodes().values().toArray(DiscoveryNode.class); + DiscoveryNode[] nodes = discoveryNodes.getNodes().values().toArray(new DiscoveryNode[0]); for (int i = 0; i < numNodeNames; i++) { DiscoveryNode discoveryNode = randomFrom(nodes); nodeSelectors.add(discoveryNode.getName()); @@ -315,13 +312,11 @@ public void testDeprecatedMasterNodeFilter() { .toArray(String[]::new); final String[] clusterManagerNodes = StreamSupport.stream(discoveryNodes.getNodes().values().spliterator(), false) - .map(n -> n.value) .filter(n -> n.isClusterManagerNode() == true) .map(DiscoveryNode::getId) .toArray(String[]::new); final String[] nonClusterManagerNodes = StreamSupport.stream(discoveryNodes.getNodes().values().spliterator(), false) - .map(n -> n.value) .filter(n -> n.isClusterManagerNode() == false) .map(DiscoveryNode::getId) .toArray(String[]::new); @@ -392,7 +387,7 @@ Set matchingNodeIds(DiscoveryNodes nodes) { @Override Set matchingNodeIds(DiscoveryNodes nodes) { Set ids = new HashSet<>(); - nodes.getClusterManagerNodes().keysIt().forEachRemaining(ids::add); + nodes.getClusterManagerNodes().keySet().iterator().forEachRemaining(ids::add); return ids; } }, @@ -400,7 +395,7 @@ Set matchingNodeIds(DiscoveryNodes nodes) { @Override Set matchingNodeIds(DiscoveryNodes nodes) { Set ids = new HashSet<>(); - nodes.getClusterManagerNodes().keysIt().forEachRemaining(ids::add); + nodes.getClusterManagerNodes().keySet().iterator().forEachRemaining(ids::add); return ids; } }, @@ -408,7 +403,7 @@ Set matchingNodeIds(DiscoveryNodes nodes) { @Override Set matchingNodeIds(DiscoveryNodes nodes) { Set ids = new HashSet<>(); - nodes.getDataNodes().keysIt().forEachRemaining(ids::add); + nodes.getDataNodes().keySet().iterator().forEachRemaining(ids::add); return ids; } }, @@ -416,7 +411,7 @@ Set matchingNodeIds(DiscoveryNodes nodes) { @Override Set matchingNodeIds(DiscoveryNodes nodes) { Set ids = new HashSet<>(); - nodes.getIngestNodes().keysIt().forEachRemaining(ids::add); + nodes.getIngestNodes().keySet().iterator().forEachRemaining(ids::add); return ids; } }, @@ -424,7 +419,7 @@ Set matchingNodeIds(DiscoveryNodes nodes) { @Override Set matchingNodeIds(DiscoveryNodes nodes) { Set ids = new HashSet<>(); - nodes.getCoordinatingOnlyNodes().keysIt().forEachRemaining(ids::add); + nodes.getCoordinatingOnlyNodes().keySet().iterator().forEachRemaining(ids::add); return ids; } }, @@ -432,7 +427,7 @@ Set matchingNodeIds(DiscoveryNodes nodes) { @Override Set matchingNodeIds(DiscoveryNodes nodes) { Set ids = new HashSet<>(); - nodes.getNodes().valuesIt().forEachRemaining(node -> { + nodes.getNodes().values().iterator().forEachRemaining(node -> { if ("value".equals(node.getAttributes().get("attr"))) { ids.add(node.getId()); } @@ -444,7 +439,7 @@ Set matchingNodeIds(DiscoveryNodes nodes) { @Override Set matchingNodeIds(DiscoveryNodes nodes) { Set ids = new HashSet<>(); - nodes.getNodes().valuesIt().forEachRemaining(node -> { + nodes.getNodes().values().iterator().forEachRemaining(node -> { if (node.getRoles().stream().anyMatch(role -> role.roleName().equals("custom_role"))) { ids.add(node.getId()); } diff --git a/server/src/test/java/org/opensearch/cluster/routing/allocation/FailedNodeRoutingTests.java b/server/src/test/java/org/opensearch/cluster/routing/allocation/FailedNodeRoutingTests.java index cddc5cc900b9c..80afc1d9b0b0f 100644 --- a/server/src/test/java/org/opensearch/cluster/routing/allocation/FailedNodeRoutingTests.java +++ b/server/src/test/java/org/opensearch/cluster/routing/allocation/FailedNodeRoutingTests.java @@ -32,7 +32,6 @@ package org.opensearch.cluster.routing.allocation; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.Version; @@ -153,9 +152,9 @@ public void testRandomClusterPromotesNewestReplica() throws InterruptedException } // Log the node versions (for debugging if necessary) - for (ObjectCursor cursor : state.nodes().getDataNodes().values()) { - Version nodeVer = cursor.value.getVersion(); - logger.info("--> node [{}] has version [{}]", cursor.value.getId(), nodeVer); + for (final DiscoveryNode cursor : state.nodes().getDataNodes().values()) { + Version nodeVer = cursor.getVersion(); + logger.info("--> node [{}] has version [{}]", cursor.getId(), nodeVer); } // randomly create some indices diff --git a/server/src/test/java/org/opensearch/cluster/routing/allocation/FailedShardsRoutingTests.java b/server/src/test/java/org/opensearch/cluster/routing/allocation/FailedShardsRoutingTests.java index 227d4c38e1838..49f7e097c08c2 100644 --- a/server/src/test/java/org/opensearch/cluster/routing/allocation/FailedShardsRoutingTests.java +++ b/server/src/test/java/org/opensearch/cluster/routing/allocation/FailedShardsRoutingTests.java @@ -32,7 +32,6 @@ package org.opensearch.cluster.routing.allocation; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.Version; @@ -734,14 +733,14 @@ public void testReplicaOnNewestVersionIsPromoted() { assertNotNull(replicaNodeVersion); logger.info("--> shard {} got assigned to node with version {}", startedReplica, replicaNodeVersion); - for (ObjectCursor cursor : clusterState.nodes().getDataNodes().values()) { - if ("node1".equals(cursor.value.getId())) { + for (final DiscoveryNode cursor : clusterState.nodes().getDataNodes().values()) { + if ("node1".equals(cursor.getId())) { // Skip the node that the primary was on, it doesn't have a replica so doesn't need a version check continue; } - Version nodeVer = cursor.value.getVersion(); + Version nodeVer = cursor.getVersion(); assertTrue( - "expected node [" + cursor.value.getId() + "] with version " + nodeVer + " to be before " + replicaNodeVersion, + "expected node [" + cursor.getId() + "] with version " + nodeVer + " to be before " + replicaNodeVersion, replicaNodeVersion.onOrAfter(nodeVer) ); } @@ -765,15 +764,15 @@ public void testReplicaOnNewestVersionIsPromoted() { assertNotNull(replicaNodeVersion); logger.info("--> shard {} got assigned to node with version {}", startedReplica, replicaNodeVersion); - for (ObjectCursor cursor : clusterState.nodes().getDataNodes().values()) { - if (primaryShardToFail.currentNodeId().equals(cursor.value.getId()) - || secondPrimaryShardToFail.currentNodeId().equals(cursor.value.getId())) { + for (final DiscoveryNode cursor : clusterState.nodes().getDataNodes().values()) { + if (primaryShardToFail.currentNodeId().equals(cursor.getId()) + || secondPrimaryShardToFail.currentNodeId().equals(cursor.getId())) { // Skip the node that the primary was on, it doesn't have a replica so doesn't need a version check continue; } - Version nodeVer = cursor.value.getVersion(); + Version nodeVer = cursor.getVersion(); assertTrue( - "expected node [" + cursor.value.getId() + "] with version " + nodeVer + " to be before " + replicaNodeVersion, + "expected node [" + cursor.getId() + "] with version " + nodeVer + " to be before " + replicaNodeVersion, replicaNodeVersion.onOrAfter(nodeVer) ); } diff --git a/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java b/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java index 0ff3511f26f30..bff35ad9fc975 100644 --- a/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java +++ b/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java @@ -37,7 +37,6 @@ import org.apache.lucene.util.Constants; import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesReference; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.geo.GeoPoint; import org.opensearch.common.lucene.BytesRefs; import org.opensearch.common.unit.TimeValue; @@ -534,38 +533,6 @@ public void testWriteMap() throws IOException { assertThat(expected, equalTo(loaded)); } - public void testWriteImmutableMap() throws IOException { - final int size = randomIntBetween(0, 100); - final ImmutableOpenMap.Builder expectedBuilder = ImmutableOpenMap.builder(randomIntBetween(0, 100)); - for (int i = 0; i < size; ++i) { - expectedBuilder.put(randomAlphaOfLength(2), randomAlphaOfLength(5)); - } - - final ImmutableOpenMap expected = expectedBuilder.build(); - final BytesStreamOutput out = new BytesStreamOutput(); - out.writeMap(expected, StreamOutput::writeString, StreamOutput::writeString); - final StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes())); - final ImmutableOpenMap loaded = in.readImmutableMap(StreamInput::readString, StreamInput::readString); - - assertThat(expected, equalTo(loaded)); - } - - public void testWriteImmutableMapOfWritable() throws IOException { - final int size = randomIntBetween(0, 100); - final ImmutableOpenMap.Builder expectedBuilder = ImmutableOpenMap.builder(randomIntBetween(0, 100)); - for (int i = 0; i < size; ++i) { - expectedBuilder.put(new TestWriteable(randomBoolean()), new TestWriteable(randomBoolean())); - } - - final ImmutableOpenMap expected = expectedBuilder.build(); - final BytesStreamOutput out = new BytesStreamOutput(); - out.writeMap(expected); - final StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes())); - final ImmutableOpenMap loaded = in.readImmutableMap(TestWriteable::new, TestWriteable::new); - - assertThat(expected, equalTo(loaded)); - } - public void testWriteMapOfLists() throws IOException { final int size = randomIntBetween(0, 5); final Map> expected = new HashMap<>(size); diff --git a/server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java b/server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java index ef3e85b0da775..8d0c8ed4690c4 100644 --- a/server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java +++ b/server/src/test/java/org/opensearch/indices/ShardLimitValidatorTests.java @@ -42,13 +42,14 @@ import org.opensearch.cluster.service.ClusterService; import org.opensearch.cluster.shards.ShardCounts; import org.opensearch.common.ValidationException; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; import org.opensearch.index.Index; import org.opensearch.test.OpenSearchTestCase; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; @@ -498,12 +499,12 @@ public void testDataStreamIndexOpeningFails() { } public static ClusterState createClusterForShardLimitTest(int nodesInCluster, int shardsInIndex, int replicas) { - ImmutableOpenMap.Builder dataNodes = ImmutableOpenMap.builder(); + final Map dataNodes = new HashMap<>(); for (int i = 0; i < nodesInCluster; i++) { dataNodes.put(randomAlphaOfLengthBetween(5, 15), mock(DiscoveryNode.class)); } DiscoveryNodes nodes = mock(DiscoveryNodes.class); - when(nodes.getDataNodes()).thenReturn(dataNodes.build()); + when(nodes.getDataNodes()).thenReturn(dataNodes); IndexMetadata.Builder indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 15)) .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) @@ -529,12 +530,12 @@ public static ClusterState createClusterForShardLimitTest( int closedIndexShards, int closedIndexReplicas ) { - ImmutableOpenMap.Builder dataNodes = ImmutableOpenMap.builder(); + final Map dataNodes = new HashMap<>(); for (int i = 0; i < nodesInCluster; i++) { dataNodes.put(randomAlphaOfLengthBetween(5, 15), mock(DiscoveryNode.class)); } DiscoveryNodes nodes = mock(DiscoveryNodes.class); - when(nodes.getDataNodes()).thenReturn(dataNodes.build()); + when(nodes.getDataNodes()).thenReturn(dataNodes); ClusterState state = ClusterState.builder(ClusterName.DEFAULT).build(); state = addOpenedIndex(openIndexName, openIndexShards, openIndexReplicas, state); diff --git a/server/src/test/java/org/opensearch/indices/cluster/IndicesClusterStateServiceRandomUpdatesTests.java b/server/src/test/java/org/opensearch/indices/cluster/IndicesClusterStateServiceRandomUpdatesTests.java index 353bf404b2b65..22a5194b50f6d 100644 --- a/server/src/test/java/org/opensearch/indices/cluster/IndicesClusterStateServiceRandomUpdatesTests.java +++ b/server/src/test/java/org/opensearch/indices/cluster/IndicesClusterStateServiceRandomUpdatesTests.java @@ -498,7 +498,7 @@ public ClusterState randomlyUpdateClusterState( } else { // remove node if (state.nodes().getDataNodes().size() > 3) { - DiscoveryNode discoveryNode = randomFrom(state.nodes().getNodes().values().toArray(DiscoveryNode.class)); + DiscoveryNode discoveryNode = randomFrom(state.nodes().getNodes().values().toArray(new DiscoveryNode[0])); if (discoveryNode.equals(state.nodes().getClusterManagerNode()) == false) { state = cluster.removeNodes(state, Collections.singletonList(discoveryNode)); updateNodes(state, clusterStateServiceMap, indicesServiceSupplier); diff --git a/server/src/test/java/org/opensearch/persistent/PersistentTasksClusterServiceTests.java b/server/src/test/java/org/opensearch/persistent/PersistentTasksClusterServiceTests.java index 7659bce456381..bfff3c77e1d89 100644 --- a/server/src/test/java/org/opensearch/persistent/PersistentTasksClusterServiceTests.java +++ b/server/src/test/java/org/opensearch/persistent/PersistentTasksClusterServiceTests.java @@ -32,7 +32,6 @@ package org.opensearch.persistent; -import com.carrotsearch.hppc.cursors.ObjectCursor; import org.opensearch.ResourceNotFoundException; import org.opensearch.Version; import org.opensearch.action.ActionListener; @@ -677,8 +676,8 @@ private Assignment randomNodeAssignment(DiscoveryNodes nodes) { return NO_NODE_FOUND; } List nodeList = new ArrayList<>(); - for (ObjectCursor node : nodes.getNodes().keys()) { - nodeList.add(node.value); + for (final String node : nodes.getNodes().keySet()) { + nodeList.add(node); } String node = randomFrom(nodeList); if (node != null) { diff --git a/test/framework/src/main/java/org/opensearch/test/hamcrest/CollectionAssertions.java b/test/framework/src/main/java/org/opensearch/test/hamcrest/CollectionAssertions.java deleted file mode 100644 index ca000ef5b460c..0000000000000 --- a/test/framework/src/main/java/org/opensearch/test/hamcrest/CollectionAssertions.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.test.hamcrest; - -import org.opensearch.common.collect.ImmutableOpenMap; -import org.hamcrest.Matcher; - -/** - * Assertions for easier handling of our custom collections, - * for example ImmutableOpenMap - */ -public class CollectionAssertions { - - public static Matcher hasKey(final String key) { - return new CollectionMatchers.ImmutableOpenMapHasKeyMatcher(key); - } - - public static Matcher hasAllKeys(final String... keys) { - return new CollectionMatchers.ImmutableOpenMapHasAllKeysMatcher(keys); - } -} diff --git a/test/framework/src/main/java/org/opensearch/test/hamcrest/CollectionMatchers.java b/test/framework/src/main/java/org/opensearch/test/hamcrest/CollectionMatchers.java deleted file mode 100644 index 733cd4a21acca..0000000000000 --- a/test/framework/src/main/java/org/opensearch/test/hamcrest/CollectionMatchers.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.test.hamcrest; - -import org.opensearch.common.collect.ImmutableOpenMap; -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; - -import java.util.Arrays; -import java.util.List; - -/** - * Matchers for easier handling of our custom collections, - * for example ImmutableOpenMap - */ -public class CollectionMatchers { - - public static class ImmutableOpenMapHasKeyMatcher extends TypeSafeMatcher { - - private final String key; - - public ImmutableOpenMapHasKeyMatcher(String key) { - this.key = key; - } - - @Override - protected boolean matchesSafely(ImmutableOpenMap item) { - return item.containsKey(key); - } - - @Override - public void describeMismatchSafely(final ImmutableOpenMap map, final Description mismatchDescription) { - if (map.size() == 0) { - mismatchDescription.appendText("was empty"); - } else { - mismatchDescription.appendText(" was ").appendValue(map); - } - } - - @Override - public void describeTo(Description description) { - description.appendText("ImmutableOpenMap should contain key " + key); - } - } - - public static class ImmutableOpenMapHasAllKeysMatcher extends TypeSafeMatcher { - - private final List keys; - private String missingKey; - - public ImmutableOpenMapHasAllKeysMatcher(final String... keys) { - this.keys = Arrays.asList(keys); - } - - @Override - protected boolean matchesSafely(ImmutableOpenMap item) { - for (String key : keys) { - if (!item.containsKey(key)) { - missingKey = key; - return false; - } - } - - return true; - } - - @Override - public void describeMismatchSafely(final ImmutableOpenMap map, final Description mismatchDescription) { - if (map.size() == 0) { - mismatchDescription.appendText("was empty"); - } else { - mismatchDescription.appendText("was ").appendValue(map.keys()); - } - } - - @Override - public void describeTo(Description description) { - description.appendText("ImmutableOpenMap should contain all keys ") - .appendValue(keys) - .appendText(", but key [") - .appendValue(missingKey) - .appendText("] is missing"); - } - } - -}