Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2057,12 +2057,16 @@ protected Map<String, Object> getIndexMappingAsMap(String index) throws IOExcept
}

protected static boolean indexExists(String index) throws IOException {
return indexExists(client(), index);
}

protected static boolean indexExists(RestClient client, String index) throws IOException {
// We use the /_cluster/health/{index} API to ensure the index exists on the master node - which means all nodes see the index.
Request request = new Request("GET", "/_cluster/health/" + index);
request.addParameter("timeout", "0");
request.addParameter("level", "indices");
try {
final var response = client().performRequest(request);
final var response = client.performRequest(request);
@SuppressWarnings("unchecked")
final var indices = (Map<String, Object>) entityAsMap(response).get("indices");
return indices.containsKey(index);
Expand Down Expand Up @@ -2122,9 +2126,16 @@ protected static boolean aliasExists(String index, String alias) throws IOExcept
/**
* Returns a list of the data stream's backing index names.
*/
@SuppressWarnings("unchecked")
protected static List<String> getDataStreamBackingIndexNames(String dataStreamName) throws IOException {
Map<String, Object> response = getAsMap(client(), "/_data_stream/" + dataStreamName);
return getDataStreamBackingIndexNames(client(), dataStreamName);
}

/**
* Returns a list of the data stream's backing index names.
*/
@SuppressWarnings("unchecked")
protected static List<String> getDataStreamBackingIndexNames(RestClient client, String dataStreamName) throws IOException {
Map<String, Object> response = getAsMap(client, "/_data_stream/" + dataStreamName);
List<?> dataStreams = (List<?>) response.get("data_streams");
assertThat(dataStreams.size(), equalTo(1));
Map<?, ?> dataStream = (Map<?, ?>) dataStreams.getFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.cluster.metadata.DataStream;
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.LazyInitializable;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xcontent.ToXContent;
Expand All @@ -31,7 +29,6 @@
import org.junit.Before;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
Expand All @@ -46,7 +43,6 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;

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

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

protected static boolean indexExists(String index) throws IOException {
Response response = adminClient().performRequest(new Request("HEAD", "/" + index));
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
}
Comment on lines -357 to -360
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was already defined (with a better implementation) in ESRestTestCase.


protected static List<String> verifyDataStream(final RestClient client, final String name, final String... expectedBackingIndices)
/**
* Verify that the specified data stream has the expected backing index generations.
*/
protected static List<String> verifyDataStream(final RestClient client, final String name, final int... expectedBackingIndices)
throws IOException {
Request request = new Request("GET", "/_data_stream/" + name);
Map<String, ?> response = toMap(client.performRequest(request));
List<?> retrievedDataStreams = (List<?>) response.get("data_streams");
assertThat(retrievedDataStreams, hasSize(1));
List<?> actualBackingIndexItems = (List<?>) ((Map<?, ?>) retrievedDataStreams.get(0)).get("indices");
assertThat(actualBackingIndexItems, hasSize(expectedBackingIndices.length));
final List<String> actualBackingIndices = new ArrayList<>();
final List<String> actualBackingIndices = getDataStreamBackingIndexNames(client, name);
for (int i = 0; i < expectedBackingIndices.length; i++) {
Map<?, ?> actualBackingIndexItem = (Map<?, ?>) actualBackingIndexItems.get(i);
String actualBackingIndex = (String) actualBackingIndexItem.get("index_name");
String expectedBackingIndex = expectedBackingIndices[i];

String actualDataStreamName = actualBackingIndex.substring(5, actualBackingIndex.indexOf('-', 5));
String expectedDataStreamName = expectedBackingIndex.substring(5, expectedBackingIndex.indexOf('-', 5));
assertThat(actualDataStreamName, equalTo(expectedDataStreamName));

int actualGeneration = Integer.parseInt(actualBackingIndex.substring(actualBackingIndex.lastIndexOf('-')));
int expectedGeneration = Integer.parseInt(expectedBackingIndex.substring(expectedBackingIndex.lastIndexOf('-')));
assertThat(actualGeneration, equalTo(expectedGeneration));
actualBackingIndices.add(actualBackingIndex);
String actualBackingIndex = actualBackingIndices.get(i);
int expectedBackingIndexGeneration = expectedBackingIndices[i];
assertThat(actualBackingIndex, DataStreamTestHelper.backingIndexEqualTo(name, expectedBackingIndexGeneration));
}
return List.copyOf(actualBackingIndices);
}
Expand Down Expand Up @@ -408,17 +387,6 @@ protected static void createAutoFollowPattern(
assertOK(client.performRequest(request));
}

/**
* Fix point in time when data stream backing index is first time queried.
* This is required to avoid failures when running test at midnight.
* (index is created for day0, but assertions are executed for day1 assuming different time based index name that does not exist)
*/
private final LazyInitializable<Long, RuntimeException> time = new LazyInitializable<>(System::currentTimeMillis);
Comment on lines -411 to -416
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is nice, but it doesn't completely avoid any issues, as the cluster(s) don't use this time supplier.


protected String backingIndexName(String dataStreamName, int generation) {
return DataStream.getDefaultBackingIndexName(dataStreamName, generation, time.getOrCompute());
}

protected RestClient buildLeaderClient() throws IOException {
assert targetCluster != TargetCluster.LEADER;
return buildClient(getLeaderCluster().getHttpAddresses());
Expand Down
Loading