Skip to content

Commit a7f09b1

Browse files
authored
Remove test usages of getDefaultBackingIndexName in CCR tests (#127693)
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 99f67fa commit a7f09b1

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -2057,12 +2057,16 @@ protected Map<String, Object> getIndexMappingAsMap(String index) throws IOExcept
20572057
}
20582058

20592059
protected static boolean indexExists(String index) throws IOException {
2060+
return indexExists(client(), index);
2061+
}
2062+
2063+
protected static boolean indexExists(RestClient client, String index) throws IOException {
20602064
// We use the /_cluster/health/{index} API to ensure the index exists on the master node - which means all nodes see the index.
20612065
Request request = new Request("GET", "/_cluster/health/" + index);
20622066
request.addParameter("timeout", "0");
20632067
request.addParameter("level", "indices");
20642068
try {
2065-
final var response = client().performRequest(request);
2069+
final var response = client.performRequest(request);
20662070
@SuppressWarnings("unchecked")
20672071
final var indices = (Map<String, Object>) entityAsMap(response).get("indices");
20682072
return indices.containsKey(index);
@@ -2122,9 +2126,16 @@ protected static boolean aliasExists(String index, String alias) throws IOExcept
21222126
/**
21232127
* Returns a list of the data stream's backing index names.
21242128
*/
2125-
@SuppressWarnings("unchecked")
21262129
protected static List<String> getDataStreamBackingIndexNames(String dataStreamName) throws IOException {
2127-
Map<String, Object> response = getAsMap(client(), "/_data_stream/" + dataStreamName);
2130+
return getDataStreamBackingIndexNames(client(), dataStreamName);
2131+
}
2132+
2133+
/**
2134+
* Returns a list of the data stream's backing index names.
2135+
*/
2136+
@SuppressWarnings("unchecked")
2137+
protected static List<String> getDataStreamBackingIndexNames(RestClient client, String dataStreamName) throws IOException {
2138+
Map<String, Object> response = getAsMap(client, "/_data_stream/" + dataStreamName);
21282139
List<?> dataStreams = (List<?>) response.get("data_streams");
21292140
assertThat(dataStreams.size(), equalTo(1));
21302141
Map<?, ?> dataStream = (Map<?, ?>) dataStreams.getFirst();

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

+9-41
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)