Skip to content

Commit 4256fe4

Browse files
committed
Remove test usages of getDefaultBackingIndexName in CCR tests
We replace usages of time sensitive `DataStream#getDefaultBackingIndexName` with the retrieval of the name via an API call. The problem with using the time sensitive method is that we can have test failures around midnight. Relates #123376
1 parent a4cb8d2 commit 4256fe4

File tree

6 files changed

+100
-231
lines changed

6 files changed

+100
-231
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,12 +2044,16 @@ protected Map<String, Object> getIndexMappingAsMap(String index) throws IOExcept
20442044
}
20452045

20462046
protected static boolean indexExists(String index) throws IOException {
2047+
return indexExists(client(), index);
2048+
}
2049+
2050+
protected static boolean indexExists(RestClient client, String index) throws IOException {
20472051
// We use the /_cluster/health/{index} API to ensure the index exists on the master node - which means all nodes see the index.
20482052
Request request = new Request("GET", "/_cluster/health/" + index);
20492053
request.addParameter("timeout", "0");
20502054
request.addParameter("level", "indices");
20512055
try {
2052-
final var response = client().performRequest(request);
2056+
final var response = client.performRequest(request);
20532057
@SuppressWarnings("unchecked")
20542058
final var indices = (Map<String, Object>) entityAsMap(response).get("indices");
20552059
return indices.containsKey(index);
@@ -2109,9 +2113,16 @@ protected static boolean aliasExists(String index, String alias) throws IOExcept
21092113
/**
21102114
* Returns a list of the data stream's backing index names.
21112115
*/
2112-
@SuppressWarnings("unchecked")
21132116
protected static List<String> getDataStreamBackingIndexNames(String dataStreamName) throws IOException {
2114-
Map<String, Object> response = getAsMap(client(), "/_data_stream/" + dataStreamName);
2117+
return getDataStreamBackingIndexNames(client(), dataStreamName);
2118+
}
2119+
2120+
/**
2121+
* Returns a list of the data stream's backing index names.
2122+
*/
2123+
@SuppressWarnings("unchecked")
2124+
protected static List<String> getDataStreamBackingIndexNames(RestClient client, String dataStreamName) throws IOException {
2125+
Map<String, Object> response = getAsMap(client, "/_data_stream/" + dataStreamName);
21152126
List<?> dataStreams = (List<?>) response.get("data_streams");
21162127
assertThat(dataStreams.size(), equalTo(1));
21172128
Map<?, ?> dataStream = (Map<?, ?>) dataStreams.getFirst();

x-pack/plugin/ccr/src/javaRestTest/java/org/elasticsearch/xpack/ccr/AbstractCCRRestTestCase.java

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
import org.elasticsearch.client.Response;
1717
import org.elasticsearch.client.ResponseException;
1818
import org.elasticsearch.client.RestClient;
19-
import org.elasticsearch.cluster.metadata.DataStream;
19+
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
2020
import org.elasticsearch.common.Strings;
2121
import org.elasticsearch.common.settings.Settings;
22-
import org.elasticsearch.common.util.LazyInitializable;
2322
import org.elasticsearch.common.xcontent.XContentHelper;
2423
import org.elasticsearch.common.xcontent.support.XContentMapValues;
25-
import org.elasticsearch.rest.RestStatus;
2624
import org.elasticsearch.test.cluster.ElasticsearchCluster;
2725
import org.elasticsearch.test.rest.ESRestTestCase;
2826
import org.elasticsearch.xcontent.ToXContent;
@@ -31,7 +29,6 @@
3129
import org.junit.Before;
3230

3331
import java.io.IOException;
34-
import java.util.ArrayList;
3532
import java.util.Arrays;
3633
import java.util.Comparator;
3734
import java.util.HashSet;
@@ -46,7 +43,6 @@
4643
import static org.hamcrest.Matchers.equalTo;
4744
import static org.hamcrest.Matchers.greaterThan;
4845
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
49-
import static org.hamcrest.Matchers.hasSize;
5046

5147
@TestCaseOrdering(AbstractCCRRestTestCase.TargetClusterTestOrdering.class)
5248
public abstract class AbstractCCRRestTestCase extends ESRestTestCase {
@@ -354,33 +350,16 @@ protected Set<CcrNodeTask> getCcrNodeTasks() throws IOException {
354350

355351
protected record CcrNodeTask(String remoteCluster, String leaderIndex, String followerIndex, int shardId) {}
356352

357-
protected static boolean indexExists(String index) throws IOException {
358-
Response response = adminClient().performRequest(new Request("HEAD", "/" + index));
359-
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
360-
}
361-
362-
protected static List<String> verifyDataStream(final RestClient client, final String name, final String... expectedBackingIndices)
353+
/**
354+
* Verify that the specified data stream has the expected backing index generations.
355+
*/
356+
protected static List<String> verifyDataStream(final RestClient client, final String name, final int... expectedBackingIndices)
363357
throws IOException {
364-
Request request = new Request("GET", "/_data_stream/" + name);
365-
Map<String, ?> response = toMap(client.performRequest(request));
366-
List<?> retrievedDataStreams = (List<?>) response.get("data_streams");
367-
assertThat(retrievedDataStreams, hasSize(1));
368-
List<?> actualBackingIndexItems = (List<?>) ((Map<?, ?>) retrievedDataStreams.get(0)).get("indices");
369-
assertThat(actualBackingIndexItems, hasSize(expectedBackingIndices.length));
370-
final List<String> actualBackingIndices = new ArrayList<>();
358+
final List<String> actualBackingIndices = getDataStreamBackingIndexNames(client, name);
371359
for (int i = 0; i < expectedBackingIndices.length; i++) {
372-
Map<?, ?> actualBackingIndexItem = (Map<?, ?>) actualBackingIndexItems.get(i);
373-
String actualBackingIndex = (String) actualBackingIndexItem.get("index_name");
374-
String expectedBackingIndex = expectedBackingIndices[i];
375-
376-
String actualDataStreamName = actualBackingIndex.substring(5, actualBackingIndex.indexOf('-', 5));
377-
String expectedDataStreamName = expectedBackingIndex.substring(5, expectedBackingIndex.indexOf('-', 5));
378-
assertThat(actualDataStreamName, equalTo(expectedDataStreamName));
379-
380-
int actualGeneration = Integer.parseInt(actualBackingIndex.substring(actualBackingIndex.lastIndexOf('-')));
381-
int expectedGeneration = Integer.parseInt(expectedBackingIndex.substring(expectedBackingIndex.lastIndexOf('-')));
382-
assertThat(actualGeneration, equalTo(expectedGeneration));
383-
actualBackingIndices.add(actualBackingIndex);
360+
String actualBackingIndex = actualBackingIndices.get(i);
361+
int expectedBackingIndexGeneration = expectedBackingIndices[i];
362+
assertThat(actualBackingIndex, DataStreamTestHelper.backingIndexEqualTo(name, expectedBackingIndexGeneration));
384363
}
385364
return List.copyOf(actualBackingIndices);
386365
}
@@ -408,17 +387,6 @@ protected static void createAutoFollowPattern(
408387
assertOK(client.performRequest(request));
409388
}
410389

411-
/**
412-
* Fix point in time when data stream backing index is first time queried.
413-
* This is required to avoid failures when running test at midnight.
414-
* (index is created for day0, but assertions are executed for day1 assuming different time based index name that does not exist)
415-
*/
416-
private final LazyInitializable<Long, RuntimeException> time = new LazyInitializable<>(System::currentTimeMillis);
417-
418-
protected String backingIndexName(String dataStreamName, int generation) {
419-
return DataStream.getDefaultBackingIndexName(dataStreamName, generation, time.getOrCompute());
420-
}
421-
422390
protected RestClient buildLeaderClient() throws IOException {
423391
assert targetCluster != TargetCluster.LEADER;
424392
return buildClient(getLeaderCluster().getHttpAddresses());

0 commit comments

Comments
 (0)