Skip to content

Commit c7dd1c8

Browse files
authored
Fix FollowIndexSecurityIT.testAutoFollowPatterns (#87853) (#87918)
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 c2d69e5 commit c7dd1c8

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
@@ -34,7 +34,7 @@ def followCluster = testClusters.register('follow-cluster') {
3434
}
3535
setting 'xpack.license.self_generated.type', 'trial'
3636
setting 'xpack.security.enabled', 'true'
37-
setting 'xpack.monitoring.collection.enabled', 'true'
37+
setting 'xpack.monitoring.collection.enabled', 'false' // will be enabled by tests
3838
extraConfigFile 'roles.yml', file('follower-roles.yml')
3939
user username: "test_admin", role: "superuser"
4040
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.yaml.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(() -> {
@@ -143,18 +149,20 @@ public void testFollowIndex() throws Exception {
143149
public void testAutoFollowPatterns() throws Exception {
144150
assumeTrue("Test should only run with target_cluster=follow", "follow".equals(targetCluster));
145151

146-
String allowedIndex = "logs-eu_20190101";
147-
String disallowedIndex = "logs-us_20190101";
152+
final String prefix = getTestName().toLowerCase(Locale.ROOT);
153+
String allowedIndex = prefix + "-eu_20190101";
154+
String disallowedIndex = prefix + "-us_20190101";
148155

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

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

160168
try (RestClient leaderClient = buildLeaderClient()) {
@@ -175,12 +183,14 @@ public void testAutoFollowPatterns() throws Exception {
175183
assertBusy(() -> ensureYellow(allowedIndex), 30, TimeUnit.SECONDS);
176184
assertBusy(() -> verifyDocuments(allowedIndex, 5, "*:*"), 30, TimeUnit.SECONDS);
177185
assertThat(indexExists(disallowedIndex), is(false));
178-
assertBusy(() -> verifyCcrMonitoring(allowedIndex, allowedIndex), 120L, TimeUnit.SECONDS);
179-
assertBusy(ESCCRRestTestCase::verifyAutoFollowMonitoring, 120L, TimeUnit.SECONDS);
186+
withMonitoring(logger, () -> {
187+
assertBusy(() -> verifyCcrMonitoring(allowedIndex, allowedIndex), 120L, TimeUnit.SECONDS);
188+
assertBusy(ESCCRRestTestCase::verifyAutoFollowMonitoring, 120L, TimeUnit.SECONDS);
189+
});
180190
} finally {
181191
// Cleanup by deleting auto follow pattern and pause following:
182192
try {
183-
deleteAutoFollowPattern("test_pattern");
193+
deleteAutoFollowPattern(pattern);
184194
pauseFollow(allowedIndex);
185195
} catch (Throwable e) {
186196
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 verifyCcrMonitoring(final String expectedLeaderIndex, fina
220220
protected static void verifyAutoFollowMonitoring() throws IOException {
221221
Request request = new Request("GET", "/.monitoring-*/_search");
222222
request.setJsonEntity("{\"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)