Skip to content

Commit 26886e0

Browse files
committed
[Metadata Immutability] Change different indices lookup objects from array type to lists
Changed the arrays to immutable List instances, added new versions of the getters which returns List instances. Resolves #8647 Signed-off-by: Abdul Muneer Kolarkunnu <muneer.kolarkunnu@netapp.com>
1 parent 098be2e commit 26886e0

File tree

7 files changed

+29
-26
lines changed

7 files changed

+29
-26
lines changed

server/src/main/java/org/opensearch/action/admin/cluster/health/ClusterHealthResponse.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.Locale;
6161
import java.util.Map;
6262
import java.util.Objects;
63+
import java.util.Set;
6364

6465
import static java.util.Collections.emptyMap;
6566
import static org.opensearch.core.xcontent.ConstructingObjectParser.constructorArg;
@@ -215,13 +216,13 @@ public ClusterHealthResponse(StreamInput in) throws IOException {
215216
}
216217

217218
/** needed for plugins BWC */
218-
public ClusterHealthResponse(String clusterName, String[] concreteIndices, ClusterState clusterState) {
219+
public ClusterHealthResponse(String clusterName, Set<String> concreteIndices, ClusterState clusterState) {
219220
this(clusterName, concreteIndices, clusterState, -1, -1, -1, TimeValue.timeValueHours(0));
220221
}
221222

222223
public ClusterHealthResponse(
223224
String clusterName,
224-
String[] concreteIndices,
225+
Set<String> concreteIndices,
225226
ClusterState clusterState,
226227
int numberOfPendingTasks,
227228
int numberOfInFlightFetch,
@@ -242,7 +243,7 @@ public ClusterHealthResponse(
242243
String clusterName,
243244
ClusterState clusterState,
244245
ClusterSettings clusterSettings,
245-
String[] concreteIndices,
246+
Set<String> concreteIndices,
246247
String awarenessAttributeName,
247248
int numberOfPendingTasks,
248249
int numberOfInFlightFetch,

server/src/main/java/org/opensearch/action/admin/cluster/health/TransportClusterHealthAction.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import org.opensearch.common.inject.Inject;
6060
import org.opensearch.common.unit.TimeValue;
6161
import org.opensearch.core.action.ActionListener;
62-
import org.opensearch.core.common.Strings;
6362
import org.opensearch.core.common.io.stream.StreamInput;
6463
import org.opensearch.core.common.util.CollectionUtils;
6564
import org.opensearch.discovery.ClusterManagerNotDiscoveredException;
@@ -71,6 +70,8 @@
7170
import org.opensearch.transport.TransportService;
7271

7372
import java.io.IOException;
73+
import java.util.Collections;
74+
import java.util.Set;
7475
import java.util.function.Consumer;
7576
import java.util.function.Predicate;
7677

@@ -490,10 +491,10 @@ private ClusterHealthResponse clusterHealth(
490491
logger.trace("Calculating health based on state version [{}]", clusterState.version());
491492
}
492493

493-
String[] concreteIndices;
494+
Set<String> concreteIndices;
494495
if (request.level().equals(ClusterHealthRequest.Level.AWARENESS_ATTRIBUTES)) {
495496
String awarenessAttribute = request.getAwarenessAttribute();
496-
concreteIndices = clusterState.getMetadata().getConcreteAllIndices().toArray(String[]::new);
497+
concreteIndices = clusterState.getMetadata().getConcreteAllIndices();
497498
return new ClusterHealthResponse(
498499
clusterState.getClusterName().value(),
499500
clusterState,
@@ -508,12 +509,12 @@ private ClusterHealthResponse clusterHealth(
508509
}
509510

510511
try {
511-
concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request);
512+
concreteIndices = Set.of(indexNameExpressionResolver.concreteIndexNames(clusterState, request));
512513
} catch (IndexNotFoundException e) {
513514
// one of the specified indices is not there - treat it as RED.
514515
ClusterHealthResponse response = new ClusterHealthResponse(
515516
clusterState.getClusterName().value(),
516-
Strings.EMPTY_ARRAY,
517+
Collections.emptySet(),
517518
clusterState,
518519
numberOfPendingTasks,
519520
numberOfInFlightFetch,

server/src/main/java/org/opensearch/action/admin/indices/datastream/GetDataStreamAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ protected void clusterManagerOperation(Request request, ClusterState state, Acti
336336
}
337337
ClusterStateHealth streamHealth = new ClusterStateHealth(
338338
state,
339-
dataStream.getIndices().stream().map(Index::getName).toArray(String[]::new)
339+
dataStream.getIndices().stream().map(Index::getName).collect(Collectors.toSet())
340340
);
341341
dataStreamInfos.add(new Response.DataStreamInfo(dataStream, streamHealth.getStatus(), indexTemplate));
342342
}

server/src/main/java/org/opensearch/cluster/health/ClusterStateHealth.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.List;
4848
import java.util.Map;
4949
import java.util.Objects;
50+
import java.util.Set;
5051

5152
/**
5253
* Cluster state health information
@@ -73,16 +74,16 @@ public final class ClusterStateHealth implements Iterable<ClusterIndexHealth>, W
7374
* @param clusterState The current cluster state. Must not be null.
7475
*/
7576
public ClusterStateHealth(final ClusterState clusterState) {
76-
this(clusterState, clusterState.metadata().getConcreteAllIndices().toArray(String[]::new));
77+
this(clusterState, clusterState.metadata().getConcreteAllIndices());
7778
}
7879

7980
/**
8081
* Creates a new <code>ClusterStateHealth</code> instance considering the current cluster state and the provided index names.
8182
*
8283
* @param clusterState The current cluster state. Must not be null.
83-
* @param concreteIndices An array of index names to consider. Must not be null but may be empty.
84+
* @param concreteIndices A set of index names to consider. Must not be null but may be empty.
8485
*/
85-
public ClusterStateHealth(final ClusterState clusterState, final String[] concreteIndices) {
86+
public ClusterStateHealth(final ClusterState clusterState, final Set<String> concreteIndices) {
8687
numberOfNodes = clusterState.nodes().getSize();
8788
numberOfDataNodes = clusterState.nodes().getDataNodes().size();
8889
hasDiscoveredClusterManager = clusterState.nodes().getClusterManagerNodeId() != null;

server/src/test/java/org/opensearch/action/admin/cluster/health/ClusterHealthResponsesTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.HashMap;
6363
import java.util.Locale;
6464
import java.util.Map;
65+
import java.util.Set;
6566
import java.util.function.Predicate;
6667
import java.util.regex.Pattern;
6768

@@ -93,7 +94,7 @@ public void testClusterHealth() throws IOException {
9394
TimeValue pendingTaskInQueueTime = TimeValue.timeValueMillis(randomIntBetween(1000, 100000));
9495
ClusterHealthResponse clusterHealth = new ClusterHealthResponse(
9596
"bla",
96-
new String[] { Metadata.ALL },
97+
Set.of(Metadata.ALL),
9798
clusterState,
9899
pendingTasks,
99100
inFlight,
@@ -121,7 +122,7 @@ public void testClusterHealthVerifyClusterManagerNodeDiscovery() throws IOExcept
121122
TimeValue pendingTaskInQueueTime = TimeValue.timeValueMillis(randomIntBetween(1000, 100000));
122123
ClusterHealthResponse clusterHealth = new ClusterHealthResponse(
123124
"bla",
124-
new String[] { Metadata.ALL },
125+
Set.of(Metadata.ALL),
125126
clusterState,
126127
pendingTasks,
127128
inFlight,

server/src/test/java/org/opensearch/action/admin/cluster/health/TransportClusterHealthActionTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@
5050

5151
import java.util.ArrayList;
5252
import java.util.List;
53+
import java.util.Set;
5354
import java.util.stream.IntStream;
5455

5556
import static org.hamcrest.core.IsEqual.equalTo;
5657

5758
public class TransportClusterHealthActionTests extends OpenSearchTestCase {
5859

5960
public void testWaitForInitializingShards() throws Exception {
60-
final String[] indices = { "test" };
61+
final Set<String> indices = Set.of("test");
6162
final ClusterHealthRequest request = new ClusterHealthRequest();
6263
request.waitForNoInitializingShards(true);
6364
ClusterState clusterState = randomClusterStateWithInitializingShards("test", 0);
@@ -76,7 +77,7 @@ public void testWaitForInitializingShards() throws Exception {
7677
}
7778

7879
public void testWaitForAllShards() {
79-
final String[] indices = { "test" };
80+
final Set<String> indices = Set.of("test");
8081
final ClusterHealthRequest request = new ClusterHealthRequest();
8182
request.waitForActiveShards(ActiveShardCount.ALL);
8283

server/src/test/java/org/opensearch/cluster/health/ClusterStateHealthTests.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,8 @@ public void testClusterHealth() throws IOException {
213213
.routingTable(routingTable.build())
214214
.nodes(clusterService.state().nodes())
215215
.build();
216-
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(
217-
clusterState,
218-
IndicesOptions.strictExpand(),
219-
(String[]) null
216+
Set<String> concreteIndices = Set.of(
217+
indexNameExpressionResolver.concreteIndexNames(clusterState, IndicesOptions.strictExpand(), (String[]) null)
220218
);
221219
ClusterStateHealth clusterStateHealth = new ClusterStateHealth(clusterState, concreteIndices);
222220
logger.info("cluster status: {}, expected {}", clusterStateHealth.getStatus(), counter.status());
@@ -230,7 +228,7 @@ public void testClusterHealth() throws IOException {
230228

231229
public void testClusterHealthOnIndexCreation() {
232230
final String indexName = "test-idx";
233-
final String[] indices = new String[] { indexName };
231+
final Set<String> indices = Set.of(indexName);
234232
final List<ClusterState> clusterStates = simulateIndexCreationStates(indexName, false);
235233
for (int i = 0; i < clusterStates.size(); i++) {
236234
// make sure cluster health is always YELLOW, up until the last state where it should be GREEN
@@ -246,7 +244,7 @@ public void testClusterHealthOnIndexCreation() {
246244

247245
public void testClusterHealthOnIndexCreationWithFailedAllocations() {
248246
final String indexName = "test-idx";
249-
final String[] indices = new String[] { indexName };
247+
final Set<String> indices = Set.of(indexName);
250248
final List<ClusterState> clusterStates = simulateIndexCreationStates(indexName, true);
251249
for (int i = 0; i < clusterStates.size(); i++) {
252250
// make sure cluster health is YELLOW up until the final cluster state, which contains primary shard
@@ -263,7 +261,7 @@ public void testClusterHealthOnIndexCreationWithFailedAllocations() {
263261

264262
public void testClusterHealthOnClusterRecovery() {
265263
final String indexName = "test-idx";
266-
final String[] indices = new String[] { indexName };
264+
final Set<String> indices = Set.of(indexName);
267265
final List<ClusterState> clusterStates = simulateClusterRecoveryStates(indexName, false, false);
268266
for (int i = 0; i < clusterStates.size(); i++) {
269267
// make sure cluster health is YELLOW up until the final cluster state, when it turns GREEN
@@ -279,7 +277,7 @@ public void testClusterHealthOnClusterRecovery() {
279277

280278
public void testClusterHealthOnClusterRecoveryWithFailures() {
281279
final String indexName = "test-idx";
282-
final String[] indices = new String[] { indexName };
280+
final Set<String> indices = Set.of(indexName);
283281
final List<ClusterState> clusterStates = simulateClusterRecoveryStates(indexName, false, true);
284282
for (int i = 0; i < clusterStates.size(); i++) {
285283
// make sure cluster health is YELLOW up until the final cluster state, which contains primary shard
@@ -296,7 +294,7 @@ public void testClusterHealthOnClusterRecoveryWithFailures() {
296294

297295
public void testClusterHealthOnClusterRecoveryWithPreviousAllocationIds() {
298296
final String indexName = "test-idx";
299-
final String[] indices = new String[] { indexName };
297+
final Set<String> indices = Set.of(indexName);
300298
final List<ClusterState> clusterStates = simulateClusterRecoveryStates(indexName, true, false);
301299
for (int i = 0; i < clusterStates.size(); i++) {
302300
// because there were previous allocation ids, we should be RED until the primaries are started,
@@ -319,7 +317,7 @@ public void testClusterHealthOnClusterRecoveryWithPreviousAllocationIds() {
319317

320318
public void testClusterHealthOnClusterRecoveryWithPreviousAllocationIdsAndAllocationFailures() {
321319
final String indexName = "test-idx";
322-
final String[] indices = new String[] { indexName };
320+
final Set<String> indices = Set.of(indexName);
323321
for (final ClusterState clusterState : simulateClusterRecoveryStates(indexName, true, true)) {
324322
final ClusterStateHealth health = new ClusterStateHealth(clusterState, indices);
325323
// if the inactive primaries are due solely to recovery (not failed allocation or previously being allocated)

0 commit comments

Comments
 (0)