Skip to content

Commit 0d8412a

Browse files
authored
Fix FollowIndexSecurityIT.testAutoFollowPatterns (#87853) (#87915)
The test FollowIndexSecurityIT.testAutoFollowPatterns sometimes fails when verifying the monitoring documents about auto-follow stats. I wasn't able to reproduce locally but I suspect that monitoring collects auto-follow stats before they are updated. Instead it should collect auto follow stats monitoring documents once indices are effectively followed. There are also some index / auto-follow pattern conflicts with other tests in the same class, so this PR also changes that. In case this fix is not enough, the full monitoring documents should appear in test log to help further debugging. Closes #84888
1 parent daf612f commit 0d8412a

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

x-pack/plugin/ccr/qa/security/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ testClusters.register('follow-cluster') {
2525
}
2626
setting 'xpack.license.self_generated.type', 'trial'
2727
setting 'xpack.security.enabled', 'true'
28-
setting 'xpack.monitoring.collection.enabled', 'true'
28+
setting 'xpack.monitoring.collection.enabled', 'false' // will be enabled by tests
2929
extraConfigFile 'roles.yml', file('follower-roles.yml')
3030
user username: "test_admin", role: "superuser"
3131
user username: "test_ccr", role: "ccruser"

x-pack/plugin/ccr/qa/security/follower-roles.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ccruser:
22
cluster:
33
- manage_ccr
44
indices:
5-
- names: [ 'allowed-index', 'forget-follower', 'logs-eu*' ]
5+
- names: [ 'allowed-index', 'forget-follower', 'logs-eu*', 'testautofollowpatterns-eu*' ]
66
privileges:
77
- monitor
88
- read

x-pack/plugin/ccr/qa/security/leader-roles.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ccruser:
22
cluster:
33
- read_ccr
44
indices:
5-
- names: [ 'allowed-index', 'clean-leader', 'forget-leader', 'logs-eu*' ]
5+
- names: [ 'allowed-index', 'clean-leader', 'forget-leader', 'logs-eu*', 'testautofollowpatterns-eu*' ]
66
privileges:
77
- manage
88
- read

x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
*/
77
package org.elasticsearch.xpack.ccr;
88

9+
import org.apache.logging.log4j.Logger;
910
import org.elasticsearch.client.Request;
11+
import org.elasticsearch.client.RequestOptions;
1012
import org.elasticsearch.client.Response;
1113
import org.elasticsearch.client.ResponseException;
1214
import org.elasticsearch.client.RestClient;
15+
import org.elasticsearch.client.WarningsHandler;
1316
import org.elasticsearch.common.settings.SecureString;
1417
import org.elasticsearch.common.settings.Settings;
1518
import org.elasticsearch.common.util.concurrent.ThreadContext;
1619
import org.elasticsearch.common.xcontent.support.XContentMapValues;
20+
import org.elasticsearch.core.CheckedRunnable;
1721
import org.elasticsearch.index.seqno.ReplicationTracker;
1822
import org.elasticsearch.test.rest.ObjectPath;
1923

@@ -69,7 +73,9 @@ public void testFollowIndex() throws Exception {
6973
followIndex("leader_cluster", allowedIndex, allowedIndex);
7074
assertBusy(() -> verifyDocuments(allowedIndex, numDocs, "*:*"));
7175
assertThat(getCcrNodeTasks(), contains(new CcrNodeTask("leader_cluster", allowedIndex, allowedIndex, 0)));
72-
assertBusy(() -> verifyCcrMonitoring(allowedIndex, allowedIndex), 120L, TimeUnit.SECONDS);
76+
77+
withMonitoring(logger, () -> { assertBusy(() -> verifyCcrMonitoring(allowedIndex, allowedIndex), 120L, TimeUnit.SECONDS); });
78+
7379
pauseFollow(allowedIndex);
7480
// Make sure that there are no other ccr relates operations running:
7581
assertBusy(() -> {
@@ -141,20 +147,22 @@ public void testFollowIndex() throws Exception {
141147
public void testAutoFollowPatterns() throws Exception {
142148
assumeTrue("Test should only run with target_cluster=follow", "follow".equals(targetCluster));
143149

144-
String allowedIndex = "logs-eu_20190101";
145-
String disallowedIndex = "logs-us_20190101";
150+
final String prefix = getTestName().toLowerCase(Locale.ROOT);
151+
String allowedIndex = prefix + "-eu_20190101";
152+
String disallowedIndex = prefix + "-us_20190101";
146153

154+
final String pattern = "pattern_" + prefix;
147155
{
148-
Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern");
156+
Request request = new Request("PUT", "/_ccr/auto_follow/" + pattern);
149157
request.setJsonEntity("""
150-
{"leader_index_patterns": ["logs-*"], "remote_cluster": "leader_cluster"}""");
158+
{"leader_index_patterns": ["testautofollowpatterns-*"], "remote_cluster": "leader_cluster"}""");
151159
Exception e = expectThrows(ResponseException.class, () -> assertOK(client().performRequest(request)));
152-
assertThat(e.getMessage(), containsString("insufficient privileges to follow index [logs-*]"));
160+
assertThat(e.getMessage(), containsString("insufficient privileges to follow index [testautofollowpatterns-*]"));
153161
}
154162

155-
Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern");
163+
Request request = new Request("PUT", "/_ccr/auto_follow/" + pattern);
156164
request.setJsonEntity("""
157-
{"leader_index_patterns": ["logs-eu*"], "remote_cluster": "leader_cluster"}""");
165+
{"leader_index_patterns": ["testautofollowpatterns-eu*"], "remote_cluster": "leader_cluster"}""");
158166
assertOK(client().performRequest(request));
159167

160168
try (RestClient leaderClient = buildLeaderClient()) {
@@ -176,12 +184,14 @@ public void testAutoFollowPatterns() throws Exception {
176184
assertBusy(() -> ensureYellow(allowedIndex), 30, TimeUnit.SECONDS);
177185
assertBusy(() -> verifyDocuments(allowedIndex, 5, "*:*"), 30, TimeUnit.SECONDS);
178186
assertThat(indexExists(disallowedIndex), is(false));
179-
assertBusy(() -> verifyCcrMonitoring(allowedIndex, allowedIndex), 120L, TimeUnit.SECONDS);
180-
assertBusy(ESCCRRestTestCase::verifyAutoFollowMonitoring, 120L, TimeUnit.SECONDS);
187+
withMonitoring(logger, () -> {
188+
assertBusy(() -> verifyCcrMonitoring(allowedIndex, allowedIndex), 120L, TimeUnit.SECONDS);
189+
assertBusy(ESCCRRestTestCase::verifyAutoFollowMonitoring, 120L, TimeUnit.SECONDS);
190+
});
181191
} finally {
182192
// Cleanup by deleting auto follow pattern and pause following:
183193
try {
184-
deleteAutoFollowPattern("test_pattern");
194+
deleteAutoFollowPattern(pattern);
185195
pauseFollow(allowedIndex);
186196
} catch (Throwable e) {
187197
logger.warn("Failed to cleanup after the test", e);
@@ -306,6 +316,30 @@ public void testUnPromoteAndFollowDataStream() throws Exception {
306316
}
307317
}
308318

319+
private static void withMonitoring(Logger logger, CheckedRunnable<Exception> runnable) throws Exception {
320+
Request enableMonitoring = new Request("PUT", "/_cluster/settings");
321+
enableMonitoring.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE).build());
322+
enableMonitoring.setJsonEntity(
323+
"{\"persistent\":{" + "\"xpack.monitoring.collection.enabled\":true," + "\"xpack.monitoring.collection.interval\":\"1s\"" + "}}"
324+
);
325+
assertOK(adminClient().performRequest(enableMonitoring));
326+
logger.info("monitoring collection enabled");
327+
try {
328+
runnable.run();
329+
} finally {
330+
Request disableMonitoring = new Request("PUT", "/_cluster/settings");
331+
disableMonitoring.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE).build());
332+
disableMonitoring.setJsonEntity(
333+
"{\"persistent\":{"
334+
+ "\"xpack.monitoring.collection.enabled\":null,"
335+
+ "\"xpack.monitoring.collection.interval\":null"
336+
+ "}}"
337+
);
338+
assertOK(adminClient().performRequest(disableMonitoring));
339+
logger.info("monitoring collection disabled");
340+
}
341+
}
342+
309343
private static void assertNoPersistentTasks() throws IOException {
310344
Map<String, Object> clusterState = toMap(adminClient().performRequest(new Request("GET", "/_cluster/state")));
311345
List<?> tasks = (List<?>) XContentMapValues.extractValue("metadata.persistent_tasks.tasks", clusterState);

x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,15 @@ protected static void verifyAutoFollowMonitoring() throws IOException {
220220
Request request = new Request("GET", "/.monitoring-*/_search");
221221
request.setJsonEntity("""
222222
{"query": {"term": {"type": "ccr_auto_follow_stats"}}}""");
223+
String responseEntity;
223224
Map<String, ?> response;
224225
try {
225-
response = toMap(adminClient().performRequest(request));
226+
responseEntity = EntityUtils.toString(adminClient().performRequest(request).getEntity());
227+
response = toMap(responseEntity);
226228
} catch (ResponseException e) {
227229
throw new AssertionError("error while searching", e);
228230
}
229-
231+
assertNotNull(responseEntity);
230232
int numberOfSuccessfulFollowIndices = 0;
231233

232234
List<?> hits = (List<?>) XContentMapValues.extractValue("hits.hits", response);
@@ -242,7 +244,11 @@ protected static void verifyAutoFollowMonitoring() throws IOException {
242244
numberOfSuccessfulFollowIndices = Math.max(numberOfSuccessfulFollowIndices, foundNumberOfOperationsReceived);
243245
}
244246

245-
assertThat(numberOfSuccessfulFollowIndices, greaterThanOrEqualTo(1));
247+
assertThat(
248+
"Unexpected number of followed indices [" + responseEntity + ']',
249+
numberOfSuccessfulFollowIndices,
250+
greaterThanOrEqualTo(1)
251+
);
246252
}
247253

248254
protected static Map<String, Object> toMap(Response response) throws IOException {

0 commit comments

Comments
 (0)