|
16 | 16 | import org.elasticsearch.client.Response;
|
17 | 17 | import org.elasticsearch.client.ResponseException;
|
18 | 18 | import org.elasticsearch.client.RestClient;
|
19 |
| -import org.elasticsearch.cluster.metadata.DataStream; |
| 19 | +import org.elasticsearch.cluster.metadata.DataStreamTestHelper; |
20 | 20 | import org.elasticsearch.common.Strings;
|
21 | 21 | import org.elasticsearch.common.settings.Settings;
|
22 |
| -import org.elasticsearch.common.util.LazyInitializable; |
23 | 22 | import org.elasticsearch.common.xcontent.XContentHelper;
|
24 | 23 | import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
25 |
| -import org.elasticsearch.rest.RestStatus; |
26 | 24 | import org.elasticsearch.test.cluster.ElasticsearchCluster;
|
27 | 25 | import org.elasticsearch.test.rest.ESRestTestCase;
|
28 | 26 | import org.elasticsearch.xcontent.ToXContent;
|
|
31 | 29 | import org.junit.Before;
|
32 | 30 |
|
33 | 31 | import java.io.IOException;
|
34 |
| -import java.util.ArrayList; |
35 | 32 | import java.util.Arrays;
|
36 | 33 | import java.util.Comparator;
|
37 | 34 | import java.util.HashSet;
|
|
46 | 43 | import static org.hamcrest.Matchers.equalTo;
|
47 | 44 | import static org.hamcrest.Matchers.greaterThan;
|
48 | 45 | import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
49 |
| -import static org.hamcrest.Matchers.hasSize; |
50 | 46 |
|
51 | 47 | @TestCaseOrdering(AbstractCCRRestTestCase.TargetClusterTestOrdering.class)
|
52 | 48 | public abstract class AbstractCCRRestTestCase extends ESRestTestCase {
|
@@ -354,33 +350,16 @@ protected Set<CcrNodeTask> getCcrNodeTasks() throws IOException {
|
354 | 350 |
|
355 | 351 | protected record CcrNodeTask(String remoteCluster, String leaderIndex, String followerIndex, int shardId) {}
|
356 | 352 |
|
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) |
363 | 357 | 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); |
371 | 359 | 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)); |
384 | 363 | }
|
385 | 364 | return List.copyOf(actualBackingIndices);
|
386 | 365 | }
|
@@ -408,17 +387,6 @@ protected static void createAutoFollowPattern(
|
408 | 387 | assertOK(client.performRequest(request));
|
409 | 388 | }
|
410 | 389 |
|
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 |
| - |
422 | 390 | protected RestClient buildLeaderClient() throws IOException {
|
423 | 391 | assert targetCluster != TargetCluster.LEADER;
|
424 | 392 | return buildClient(getLeaderCluster().getHttpAddresses());
|
|
0 commit comments