Skip to content

Commit a481eaf

Browse files
committed
Update tests to provide uuid when creating a cluster state with indices
1 parent 33182fe commit a481eaf

File tree

59 files changed

+855
-725
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+855
-725
lines changed

server/src/internalClusterTest/java/org/elasticsearch/cluster/ClusterStateDiffIT.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,20 +300,25 @@ private ClusterState.Builder randomRoutingTable(ClusterState clusterState) {
300300
}
301301
int additionalIndexCount = randomIntBetween(1, 20);
302302
for (int i = 0; i < additionalIndexCount; i++) {
303-
builder.add(randomIndexRoutingTable("index-" + randomInt(), clusterState.nodes().getNodes().keySet().toArray(new String[0])));
303+
builder.add(
304+
randomIndexRoutingTable(
305+
new Index("index-" + randomInt(), randomUUID()),
306+
clusterState.nodes().getNodes().keySet().toArray(new String[0])
307+
)
308+
);
304309
}
305310
return ClusterState.builder(clusterState).routingTable(builder.build());
306311
}
307312

308313
/**
309314
* Randomly updates index routing table in the cluster state
310315
*/
311-
private IndexRoutingTable randomIndexRoutingTable(String index, String[] nodeIds) {
312-
IndexRoutingTable.Builder builder = IndexRoutingTable.builder(new Index(index, "_na_"));
316+
private IndexRoutingTable randomIndexRoutingTable(Index index, String[] nodeIds) {
317+
IndexRoutingTable.Builder builder = IndexRoutingTable.builder(index);
313318
int shardCount = randomInt(10);
314319

315320
for (int i = 0; i < shardCount; i++) {
316-
IndexShardRoutingTable.Builder indexShard = new IndexShardRoutingTable.Builder(new ShardId(index, "_na_", i));
321+
IndexShardRoutingTable.Builder indexShard = new IndexShardRoutingTable.Builder(new ShardId(index, i));
317322
int replicaCount = randomIntBetween(1, 10);
318323
Set<String> availableNodeIds = Sets.newHashSet(nodeIds);
319324
for (int j = 0; j < replicaCount; j++) {

server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private <T extends Exception> T serialize(T exception, TransportVersion version)
251251
}
252252

253253
public void testIllegalShardRoutingStateException() throws IOException {
254-
final ShardRouting routing = newShardRouting("test", 0, "xyz", false, ShardRoutingState.STARTED);
254+
final ShardRouting routing = newShardRouting("test", randomUUID(), 0, "xyz", false, ShardRoutingState.STARTED);
255255
final String routingAsString = routing.toString();
256256
IllegalShardRoutingStateException serialize = serialize(
257257
new IllegalShardRoutingStateException(routing, "foo", new NullPointerException())

server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.common.Strings;
3030
import org.elasticsearch.common.xcontent.ChunkedToXContent;
3131
import org.elasticsearch.common.xcontent.XContentHelper;
32+
import org.elasticsearch.index.Index;
3233
import org.elasticsearch.test.ESTestCase;
3334
import org.elasticsearch.test.gateway.TestGatewayAllocator;
3435
import org.elasticsearch.xcontent.ToXContent;
@@ -55,7 +56,7 @@ public class ClusterAllocationExplainActionTests extends ESTestCase {
5556

5657
public void testInitializingOrRelocatingShardExplanation() throws Exception {
5758
ShardRoutingState shardRoutingState = randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.RELOCATING);
58-
ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), shardRoutingState);
59+
ClusterState clusterState = ClusterStateCreationUtils.state(new Index("idx", randomUUID()), randomBoolean(), shardRoutingState);
5960

6061
assertThat(clusterState.metadata().projects(), aMapWithSize(1));
6162
final ProjectId projectId = clusterState.metadata().projects().keySet().iterator().next();
@@ -140,20 +141,25 @@ public ShardAllocationDecision decideShardAllocation(ShardRouting shard, Routing
140141

141142
public void testFindAnyUnassignedShardToExplain() {
142143
// find unassigned primary
143-
ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.UNASSIGNED);
144+
Index index = new Index("idx", randomUUID());
145+
ClusterState clusterState = ClusterStateCreationUtils.state(index, randomBoolean(), ShardRoutingState.UNASSIGNED);
144146
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT);
145147
Set<ProjectId> projectIds = clusterState.metadata().projects().keySet();
146148
ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState), projectIds);
147-
assertEquals(clusterState.getRoutingTable().index("idx").shard(0).primaryShard(), shard);
149+
assertEquals(clusterState.getRoutingTable().index(index).shard(0).primaryShard(), shard);
148150

149151
// find unassigned replica
150-
clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED);
152+
clusterState = ClusterStateCreationUtils.state(index, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED);
151153
request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT);
152154
shard = findShardToExplain(request, routingAllocation(clusterState), projectIds);
153-
assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().get(0), shard);
155+
assertEquals(clusterState.getRoutingTable().index(index).shard(0).replicaShards().get(0), shard);
154156

155157
// prefer unassigned primary to replica
156-
clusterState = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(new String[] { "idx1", "idx2" }, 1, 1);
158+
clusterState = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(
159+
new Index[] { new Index("idx1", randomUUID()), new Index("idx2", randomUUID()) },
160+
1,
161+
1
162+
);
157163
final String redIndex = randomBoolean() ? "idx1" : "idx2";
158164
final RoutingTable.Builder routingTableBuilder = RoutingTable.builder(clusterState.routingTable());
159165
for (final IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
@@ -183,7 +189,7 @@ public void testFindAnyUnassignedShardToExplain() {
183189

184190
// no unassigned shard to explain
185191
final ClusterState allStartedClusterState = ClusterStateCreationUtils.state(
186-
"idx",
192+
index,
187193
randomBoolean(),
188194
ShardRoutingState.STARTED,
189195
ShardRoutingState.STARTED
@@ -204,28 +210,36 @@ public void testFindAnyUnassignedShardToExplain() {
204210
}
205211

206212
public void testFindPrimaryShardToExplain() {
207-
ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), randomFrom(ShardRoutingState.values()));
213+
Index index = new Index("idx", randomUUID());
214+
ClusterState clusterState = ClusterStateCreationUtils.state(index, randomBoolean(), randomFrom(ShardRoutingState.values()));
208215
Set<ProjectId> projectIds = clusterState.metadata().projects().keySet();
209-
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT, "idx", 0, true, null);
216+
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT, index.getName(), 0, true, null);
210217
ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState), projectIds);
211-
assertEquals(clusterState.getRoutingTable().index("idx").shard(0).primaryShard(), shard);
218+
assertEquals(clusterState.getRoutingTable().index(index).shard(0).primaryShard(), shard);
212219
}
213220

214221
public void testFindAnyReplicaToExplain() {
222+
Index index = new Index("idx", randomUUID());
215223
// prefer unassigned replicas to started replicas
216224
ClusterState clusterState = ClusterStateCreationUtils.state(
217-
"idx",
225+
index,
218226
randomBoolean(),
219227
ShardRoutingState.STARTED,
220228
ShardRoutingState.STARTED,
221229
ShardRoutingState.UNASSIGNED
222230
);
223231
Set<ProjectId> projectIds = clusterState.metadata().projects().keySet();
224-
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT, "idx", 0, false, null);
232+
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(
233+
TEST_REQUEST_TIMEOUT,
234+
index.getName(),
235+
0,
236+
false,
237+
null
238+
);
225239
ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState), projectIds);
226240
assertEquals(
227241
clusterState.getRoutingTable()
228-
.index("idx")
242+
.index(index)
229243
.shard(0)
230244
.replicaShards()
231245
.stream()
@@ -237,7 +251,7 @@ public void testFindAnyReplicaToExplain() {
237251

238252
// prefer started replicas to initializing/relocating replicas
239253
clusterState = ClusterStateCreationUtils.state(
240-
"idx",
254+
index,
241255
randomBoolean(),
242256
ShardRoutingState.STARTED,
243257
randomFrom(ShardRoutingState.RELOCATING, ShardRoutingState.INITIALIZING),
@@ -246,7 +260,7 @@ public void testFindAnyReplicaToExplain() {
246260
request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT, "idx", 0, false, null);
247261
shard = findShardToExplain(request, routingAllocation(clusterState), projectIds);
248262
assertEquals(
249-
clusterState.getRoutingTable().index("idx").shard(0).replicaShards().stream().filter(ShardRouting::started).findFirst().get(),
263+
clusterState.getRoutingTable().index(index).shard(0).replicaShards().stream().filter(ShardRouting::started).findFirst().get(),
250264
shard
251265
);
252266
}
@@ -258,15 +272,16 @@ public void testFindShardAssignedToNode() {
258272
if (primary == false) {
259273
replicaStates = new ShardRoutingState[] { ShardRoutingState.STARTED };
260274
}
261-
ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, replicaStates);
275+
Index index = new Index("idx", randomUUID());
276+
ClusterState clusterState = ClusterStateCreationUtils.state(index, randomBoolean(), ShardRoutingState.STARTED, replicaStates);
262277
assertThat(clusterState.metadata().projects(), aMapWithSize(1));
263278
final ProjectId projectId = clusterState.metadata().projects().keySet().iterator().next();
264279
ShardRouting shardToExplain = primary
265-
? clusterState.routingTable(projectId).index("idx").shard(0).primaryShard()
266-
: clusterState.routingTable(projectId).index("idx").shard(0).replicaShards().get(0);
280+
? clusterState.routingTable(projectId).index(index).shard(0).primaryShard()
281+
: clusterState.routingTable(projectId).index(index).shard(0).replicaShards().get(0);
267282
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(
268283
TEST_REQUEST_TIMEOUT,
269-
"idx",
284+
index.getName(),
270285
0,
271286
primary,
272287
shardToExplain.currentNodeId()
@@ -285,7 +300,7 @@ public void testFindShardAssignedToNode() {
285300
}
286301
final ClusterAllocationExplainRequest failingRequest = new ClusterAllocationExplainRequest(
287302
TEST_REQUEST_TIMEOUT,
288-
"idx",
303+
index.getName(),
289304
0,
290305
primary,
291306
explainNode

server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetDesiredBalanceActionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void testGetDesiredBalance() throws Exception {
141141
RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
142142
for (int i = 0; i < randomInt(8); i++) {
143143
String indexName = randomAlphaOfLength(8);
144-
Settings.Builder settings = indexSettings(IndexVersion.current(), 1, 0);
144+
Settings.Builder settings = indexSettings(IndexVersion.current(), randomUUID(), 1, 0);
145145
if (randomBoolean()) {
146146
settings.put(DataTier.TIER_PREFERENCE_SETTING.getKey(), randomFrom("data_hot", "data_warm", "data_cold"));
147147
}

server/src/test/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseActionTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.cluster.service.ClusterService;
3535
import org.elasticsearch.common.settings.Settings;
3636
import org.elasticsearch.core.TimeValue;
37+
import org.elasticsearch.index.Index;
3738
import org.elasticsearch.index.IndexNotFoundException;
3839
import org.elasticsearch.index.IndexVersion;
3940
import org.elasticsearch.index.engine.Engine;
@@ -258,8 +259,8 @@ public void testVerifyShardBeforeIndexClosingFailed() {
258259
}
259260

260261
public void testUnavailableShardsMarkedAsStale() throws Exception {
261-
final String index = "test";
262-
final ShardId shardId = new ShardId(index, "_na_", 0);
262+
final Index index = new Index("test", randomUUID());
263+
final ShardId shardId = new ShardId(index, 0);
263264

264265
final int nbReplicas = randomIntBetween(1, 10);
265266
final ShardRoutingState[] replicaStates = new ShardRoutingState[nbReplicas];

server/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexActionTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class GetIndexActionTests extends ESSingleNodeTestCase {
4949
private IndicesService indicesService;
5050
private ThreadPool threadPool;
5151
private SettingsFilter settingsFilter;
52-
private final String indexName = "test_index";
52+
private final Index index = new Index("test_index", randomUUID());
5353

5454
private TestTransportGetIndexAction getIndexAction;
5555

@@ -83,30 +83,30 @@ public void tearDown() throws Exception {
8383
}
8484

8585
public void testIncludeDefaults() {
86-
GetIndexRequest defaultsRequest = new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(indexName).includeDefaults(true);
86+
GetIndexRequest defaultsRequest = new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(index.getName()).includeDefaults(true);
8787
ActionTestUtils.execute(
8888
getIndexAction,
8989
null,
9090
defaultsRequest,
9191
ActionTestUtils.assertNoFailureListener(
9292
defaultsResponse -> assertNotNull(
9393
"index.refresh_interval should be set as we are including defaults",
94-
defaultsResponse.getSetting(indexName, "index.refresh_interval")
94+
defaultsResponse.getSetting(index.getName(), "index.refresh_interval")
9595
)
9696
)
9797
);
9898
}
9999

100100
public void testDoNotIncludeDefaults() {
101-
GetIndexRequest noDefaultsRequest = new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(indexName);
101+
GetIndexRequest noDefaultsRequest = new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(index.getName());
102102
ActionTestUtils.execute(
103103
getIndexAction,
104104
null,
105105
noDefaultsRequest,
106106
ActionTestUtils.assertNoFailureListener(
107107
noDefaultsResponse -> assertNull(
108108
"index.refresh_interval should be null as it was never set",
109-
noDefaultsResponse.getSetting(indexName, "index.refresh_interval")
109+
noDefaultsResponse.getSetting(index.getName(), "index.refresh_interval")
110110
)
111111
)
112112
);
@@ -135,7 +135,7 @@ protected void localClusterStateOperation(
135135
ProjectState state,
136136
ActionListener<GetIndexResponse> listener
137137
) throws Exception {
138-
ProjectState stateWithIndex = ClusterStateCreationUtils.state(indexName, 1, 1).projectState(ProjectId.DEFAULT);
138+
ProjectState stateWithIndex = ClusterStateCreationUtils.state(index, 1, 1).projectState(ProjectId.DEFAULT);
139139
super.localClusterStateOperation(task, request, stateWithIndex, listener);
140140
}
141141
}

server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class IndicesSegmentResponseTests extends ESTestCase {
3131

3232
public void testToXContentSerialiationWithSortedFields() throws Exception {
33-
ShardRouting shardRouting = TestShardRouting.newShardRouting("foo", 0, "node_id", true, ShardRoutingState.STARTED);
33+
ShardRouting shardRouting = TestShardRouting.newShardRouting("foo", randomUUID(), 0, "node_id", true, ShardRoutingState.STARTED);
3434
Segment segment = new Segment("my");
3535

3636
SortField sortField = new SortField("foo", SortField.Type.STRING);
@@ -54,7 +54,7 @@ public void testChunking() {
5454
final int indices = randomIntBetween(1, 10);
5555
final List<ShardRouting> routings = new ArrayList<>(indices);
5656
for (int i = 0; i < indices; i++) {
57-
routings.add(TestShardRouting.newShardRouting("index-" + i, 0, "node_id", true, ShardRoutingState.STARTED));
57+
routings.add(TestShardRouting.newShardRouting("index-" + i, randomUUID(), 0, "node_id", true, ShardRoutingState.STARTED));
5858
}
5959
Segment segment = new Segment("my");
6060
SortField sortField = new SortField("foo", SortField.Type.STRING);

0 commit comments

Comments
 (0)