Skip to content

Commit 069605b

Browse files
authored
Do not count shard changes tasks against REST tests (#33738)
When executing CCR REST tests it is going to be expected after global checkpoint polling goes in that shard changes tasks can still be pending at the end of the test. One way to deal with this is to set a low timeout on these polls, but then that means we are not executing our REST tests with our default production settings and instead would be using an unrealistic low timeout. Alternatively, since we expect these tasks to be there, we can not count them against the test. That is what this commit does.
1 parent db40315 commit 069605b

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

x-pack/plugin/ccr/qa/rest/src/test/java/org/elasticsearch/xpack/ccr/CcrRestIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.util.concurrent.ThreadContext;
1313
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
1414
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
15+
import org.elasticsearch.xpack.ccr.action.ShardChangesAction;
1516
import org.elasticsearch.xpack.test.rest.XPackRestTestHelper;
1617
import org.junit.After;
1718

@@ -36,7 +37,7 @@ protected Settings restClientSettings() {
3637

3738
@After
3839
public void cleanup() throws Exception {
39-
XPackRestTestHelper.waitForPendingTasks(adminClient());
40+
XPackRestTestHelper.waitForPendingTasks(adminClient(), taskName -> taskName.startsWith(ShardChangesAction.NAME));
4041
}
4142

4243
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestTestHelper.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.client.Response;
1515
import org.elasticsearch.client.ResponseException;
1616
import org.elasticsearch.client.RestClient;
17+
import org.elasticsearch.common.CheckedRunnable;
1718
import org.elasticsearch.common.xcontent.XContentHelper;
1819
import org.elasticsearch.common.xcontent.json.JsonXContent;
1920
import org.elasticsearch.test.ESTestCase;
@@ -29,7 +30,9 @@
2930
import java.util.List;
3031
import java.util.Map;
3132
import java.util.concurrent.atomic.AtomicReference;
33+
import java.util.function.Predicate;
3234

35+
import static org.elasticsearch.test.ESTestCase.assertBusy;
3336
import static org.junit.Assert.assertEquals;
3437

3538
public final class XPackRestTestHelper {
@@ -84,34 +87,54 @@ public static void waitForMlTemplates(RestClient client) throws InterruptedExcep
8487
}
8588

8689
/**
87-
* Waits for pending tasks to complete
90+
* Wait for outstanding tasks to complete. The specified admin client is used to check the outstanding tasks and this is done using
91+
* {@link ESTestCase#assertBusy(CheckedRunnable)} to give a chance to any outstanding tasks to complete.
92+
*
93+
* @param adminClient the admin client
94+
* @throws Exception if an exception is thrown while checking the outstanding tasks
8895
*/
89-
public static void waitForPendingTasks(RestClient adminClient) throws Exception {
90-
ESTestCase.assertBusy(() -> {
96+
public static void waitForPendingTasks(final RestClient adminClient) throws Exception {
97+
waitForPendingTasks(adminClient, taskName -> false);
98+
}
99+
100+
/**
101+
* Wait for outstanding tasks to complete. The specified admin client is used to check the outstanding tasks and this is done using
102+
* {@link ESTestCase#assertBusy(CheckedRunnable)} to give a chance to any outstanding tasks to complete. The specified filter is used
103+
* to filter out outstanding tasks that are expected to be there.
104+
*
105+
* @param adminClient the admin client
106+
* @param taskFilter predicate used to filter tasks that are expected to be there
107+
* @throws Exception if an exception is thrown while checking the outstanding tasks
108+
*/
109+
public static void waitForPendingTasks(final RestClient adminClient, final Predicate<String> taskFilter) throws Exception {
110+
assertBusy(() -> {
91111
try {
92-
Request request = new Request("GET", "/_cat/tasks");
112+
final Request request = new Request("GET", "/_cat/tasks");
93113
request.addParameter("detailed", "true");
94-
Response response = adminClient.performRequest(request);
95-
// Check to see if there are tasks still active. We exclude the
96-
// list tasks
97-
// actions tasks form this otherwise we will always fail
114+
final Response response = adminClient.performRequest(request);
115+
/*
116+
* Check to see if there are outstanding tasks; we exclude the list task itself, and any expected outstanding tasks using
117+
* the specified task filter.
118+
*/
98119
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
99120
try (BufferedReader responseReader = new BufferedReader(
100121
new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
101122
int activeTasks = 0;
102123
String line;
103-
StringBuilder tasksListString = new StringBuilder();
124+
final StringBuilder tasksListString = new StringBuilder();
104125
while ((line = responseReader.readLine()) != null) {
105-
if (line.startsWith(ListTasksAction.NAME) == false) {
106-
activeTasks++;
107-
tasksListString.append(line);
108-
tasksListString.append('\n');
126+
final String taskName = line.split("\\s+")[0];
127+
if (taskName.startsWith(ListTasksAction.NAME) || taskFilter.test(taskName)) {
128+
continue;
109129
}
130+
activeTasks++;
131+
tasksListString.append(line);
132+
tasksListString.append('\n');
110133
}
111134
assertEquals(activeTasks + " active tasks found:\n" + tasksListString, 0, activeTasks);
112135
}
113136
}
114-
} catch (IOException e) {
137+
} catch (final IOException e) {
115138
throw new AssertionError("Error getting active tasks list", e);
116139
}
117140
});

0 commit comments

Comments
 (0)