Skip to content

Commit e7e3530

Browse files
authored
Remove more usages of .watches system index. (#65871)
Backporting #65835 to 7.x branch. Make use of the new query watches api to simply list watches or to delete all watches using the delete watch api. Relates to #62501
1 parent 3f0bd64 commit e7e3530

File tree

5 files changed

+24
-50
lines changed

5 files changed

+24
-50
lines changed

x-pack/docs/en/watcher/managing-watches.asciidoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ For example, the following returns the first 100 watches:
2828

2929
[source,console]
3030
--------------------------------------------------
31-
GET .watches/_search
31+
GET /_watcher/_query/watches
3232
{
3333
"size" : 100
3434
}
3535
--------------------------------------------------
36-
// TEST[skip:deprecation warning]

x-pack/plugin/watcher/qa/common/src/main/java/org/elasticsearch/xpack/watcher/WatcherRestTestCase.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import org.junit.Assert;
1616
import org.junit.Before;
1717

18+
import java.io.IOException;
19+
import java.util.List;
20+
import java.util.Map;
1821
import java.util.concurrent.TimeUnit;
1922

2023
/**
@@ -70,16 +73,21 @@ public final void stopWatcher() throws Exception {
7073
throw new AssertionError("unknown state[" + state + "]");
7174
}
7275
}, 60, TimeUnit.SECONDS);
76+
deleteAllWatcherData();
77+
}
78+
79+
public static void deleteAllWatcherData() throws IOException {
80+
Request queryWatchesRequest = new Request("GET", "/_watcher/_query/watches");
81+
ObjectPath response = ObjectPath.createFromResponse(ESRestTestCase.adminClient().performRequest(queryWatchesRequest));
7382

74-
Request deleteWatchesIndexRequest = new Request("DELETE", ".watches");
75-
deleteWatchesIndexRequest.addParameter("ignore_unavailable", "true");
76-
deleteWatchesIndexRequest.setOptions(
77-
expectWarnings(
78-
"this request accesses system indices: [.watches], but in a future major "
79-
+ "version, direct access to system indices will be prevented by default"
80-
)
81-
);
82-
ESRestTestCase.adminClient().performRequest(deleteWatchesIndexRequest);
83+
int totalCount = response.evaluate("count");
84+
List<Map<?, ?>> watches = response.evaluate("watches");
85+
assert watches.size() == totalCount : "number of watches returned is unequal to the total number of watches";
86+
for (Map<?, ?> watch : watches) {
87+
String id = (String) watch.get("_id");
88+
Request deleteWatchRequest = new Request("DELETE", "/_watcher/watch/" + id);
89+
assertOK(ESRestTestCase.adminClient().performRequest(deleteWatchRequest));
90+
}
8391

8492
Request deleteWatchHistoryRequest = new Request("DELETE", ".watcher-history-*");
8593
deleteWatchHistoryRequest.addParameter("ignore_unavailable", "true");

x-pack/plugin/watcher/qa/common/src/main/java/org/elasticsearch/xpack/watcher/WatcherYamlSuiteTestCase.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
import com.carrotsearch.randomizedtesting.annotations.Name;
99
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
10-
import org.elasticsearch.client.Request;
11-
import org.elasticsearch.client.RequestOptions;
1210
import org.elasticsearch.test.ESTestCase;
13-
import org.elasticsearch.test.rest.ESRestTestCase;
1411
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
1512
import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse;
1613
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
@@ -19,11 +16,11 @@
1916
import org.junit.Assert;
2017
import org.junit.Before;
2118

22-
import java.io.IOException;
2319
import java.util.concurrent.TimeUnit;
2420

2521
import static java.util.Collections.emptyList;
2622
import static java.util.Collections.emptyMap;
23+
import static org.elasticsearch.xpack.watcher.WatcherRestTestCase.deleteAllWatcherData;
2724

2825
/**
2926
* Parent test class for Watcher YAML based REST tests
@@ -63,7 +60,7 @@ public final void startWatcher() throws Exception {
6360
int watcherCount = (int) response.evaluate("stats.0.watch_count");
6461
if (watcherCount > 0) {
6562
logger.info("expected 0 active watches, but got [{}], deleting watcher indices again", watcherCount);
66-
deleteWatcherIndices();
63+
deleteAllWatcherData();
6764
}
6865
// all good here, we are done
6966
break;
@@ -100,24 +97,6 @@ public final void stopWatcher() throws Exception {
10097
throw new AssertionError("unknown state[" + state + "]");
10198
}
10299
}, 60, TimeUnit.SECONDS);
103-
deleteWatcherIndices();
104-
}
105-
106-
private static void deleteWatcherIndices() throws IOException {
107-
Request deleteWatchesIndexRequest = new Request("DELETE", ".watches");
108-
deleteWatchesIndexRequest.addParameter("ignore_unavailable", "true");
109-
final RequestOptions.Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
110-
requestOptions.setWarningsHandler(warnings -> {
111-
final String expectedWaring = "this request accesses system indices: [.watches], but in a future major version, direct "
112-
+ "access to system indices will be prevented by default";
113-
// There might not be a warning if the .watches index doesn't exist
114-
return (warnings.isEmpty() || warnings.get(0).equals(expectedWaring)) == false;
115-
});
116-
deleteWatchesIndexRequest.setOptions(requestOptions);
117-
ESRestTestCase.adminClient().performRequest(deleteWatchesIndexRequest);
118-
119-
Request deleteWatchHistoryRequest = new Request("DELETE", ".watcher-history-*");
120-
deleteWatchHistoryRequest.addParameter("ignore_unavailable", "true");
121-
ESRestTestCase.adminClient().performRequest(deleteWatchHistoryRequest);
100+
deleteAllWatcherData();
122101
}
123102
}

x-pack/plugin/watcher/qa/with-monitoring/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apply plugin: 'elasticsearch.java-rest-test'
22

33
dependencies {
44
javaRestTestImplementation project(':x-pack:qa')
5+
javaRestTestImplementation project(path: ':x-pack:plugin:watcher:qa:common')
56
}
67

78
testClusters.all {

x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package org.elasticsearch.smoketest;
77

88
import org.elasticsearch.client.Request;
9-
import org.elasticsearch.client.RequestOptions;
109
import org.elasticsearch.client.Response;
1110
import org.elasticsearch.common.Strings;
1211
import org.elasticsearch.test.rest.ESRestTestCase;
@@ -16,6 +15,7 @@
1615
import java.io.IOException;
1716

1817
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
18+
import static org.elasticsearch.xpack.watcher.WatcherRestTestCase.deleteAllWatcherData;
1919
import static org.hamcrest.Matchers.is;
2020

2121
public class MonitoringWithWatcherRestIT extends ESRestTestCase {
@@ -40,20 +40,7 @@ public void cleanExporters() throws Exception {
4040
.nullField("xpack.monitoring.exporters.*")
4141
.endObject().endObject()));
4242
adminClient().performRequest(cleanupSettingsRequest);
43-
final Request deleteRequest = new Request("DELETE", "/.watch*");
44-
final RequestOptions.Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
45-
requestOptions.setWarningsHandler(warnings -> {
46-
if (warnings.size() != 1) {
47-
return true;
48-
}
49-
// We don't know exactly which indices we're cleaning up in advance, so just accept all system index access warnings.
50-
final String warning = warnings.get(0);
51-
final boolean isSystemIndexWarning = warning.contains("this request accesses system indices")
52-
&& warning.contains("but in a future major version, direct access to system indices will be prevented by default");
53-
return isSystemIndexWarning == false;
54-
});
55-
deleteRequest.setOptions(requestOptions);
56-
adminClient().performRequest(deleteRequest);
43+
deleteAllWatcherData();
5744
}
5845

5946
@AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/59132" )

0 commit comments

Comments
 (0)