Skip to content

Commit 6d6c7d5

Browse files
authored
Remove SnapshotClient from HLRC (#85845)
This removes the `SnapshotClient` from the high level rest client, rewriting the tests to use the low-level client instead. Relates to #83423
1 parent 0b999c3 commit 6d6c7d5

File tree

7 files changed

+174
-749
lines changed

7 files changed

+174
-749
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ public class RestHighLevelClient implements Closeable {
271271
/** Do not access directly but through getVersionValidationFuture() */
272272
private volatile ListenableFuture<Optional<String>> versionValidationFuture;
273273

274-
private final SnapshotClient snapshotClient = new SnapshotClient(this);
275274
private final SecurityClient securityClient = new SecurityClient(this);
276275
private final EqlClient eqlClient = new EqlClient(this);
277276

@@ -345,15 +344,6 @@ public final void close() throws IOException {
345344
doClose.accept(client);
346345
}
347346

348-
/**
349-
* Provides a {@link SnapshotClient} which can be used to access the Snapshot API.
350-
*
351-
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html">Snapshot API on elastic.co</a>
352-
*/
353-
public final SnapshotClient snapshot() {
354-
return snapshotClient;
355-
}
356-
357347
/**
358348
* Provides methods for accessing the Elastic Licensed Security APIs that
359349
* are shipped with the Elastic Stack distribution of Elasticsearch. All of

client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java

Lines changed: 0 additions & 526 deletions
This file was deleted.

qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java

Lines changed: 80 additions & 108 deletions
Large diffs are not rendered by default.

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,19 @@ public static void assertOK(Response response) {
14281428
assertThat(response.getStatusLine().getStatusCode(), anyOf(equalTo(200), equalTo(201)));
14291429
}
14301430

1431+
/**
1432+
* Assert that the index in question has the given number of documents present
1433+
*/
1434+
public static void assertDocCount(RestClient client, String indexName, long docCount) throws IOException {
1435+
Request countReq = new Request("GET", "/" + indexName + "/_count");
1436+
ObjectPath resp = ObjectPath.createFromResponse(client.performRequest(countReq));
1437+
assertEquals(
1438+
"expected " + docCount + " documents but it was a different number",
1439+
docCount,
1440+
Long.parseLong(resp.evaluate("count").toString())
1441+
);
1442+
}
1443+
14311444
public static void assertAcknowledged(Response response) throws IOException {
14321445
assertOK(response);
14331446
String jsonBody = EntityUtils.toString(response.getEntity());

x-pack/plugin/eql/qa/correctness/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlDataLoader.java

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
1616
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
1717
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
18-
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest;
19-
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
2018
import org.elasticsearch.client.Request;
21-
import org.elasticsearch.client.RequestOptions;
2219
import org.elasticsearch.client.RestClient;
2320
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
24-
import org.elasticsearch.client.RestHighLevelClient;
25-
import org.elasticsearch.client.core.CountRequest;
21+
import org.elasticsearch.common.Strings;
2622
import org.elasticsearch.common.logging.LogConfigurator;
2723
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.test.rest.ESRestTestCase;
25+
import org.elasticsearch.test.rest.ObjectPath;
2826

2927
import java.io.IOException;
3028
import java.io.InputStream;
31-
import java.util.List;
3229
import java.util.Properties;
3330

3431
import static org.elasticsearch.test.ESTestCase.assertEquals;
@@ -60,8 +57,7 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli
6057
.build()
6158
) {
6259
Properties configuration = loadConfiguration();
63-
restoreSnapshot(new RestHighLevelClient(client, ignore -> {}, List.of()) {
64-
}, configuration);
60+
restoreSnapshot(client, configuration);
6561
}
6662
}
6763

@@ -73,44 +69,40 @@ static Properties loadConfiguration() throws IOException {
7369
}
7470
}
7571

76-
static void restoreSnapshot(RestHighLevelClient restHighLevelClient, Properties cfg) throws IOException {
77-
if (restHighLevelClient.getLowLevelClient()
78-
.performRequest(new Request("HEAD", "/" + cfg.getProperty("index_name")))
79-
.getStatusLine()
80-
.getStatusCode() == 404) {
81-
restHighLevelClient.snapshot()
82-
.createRepository(
83-
new PutRepositoryRequest(cfg.getProperty("gcs_repo_name")).type("gcs")
72+
static void restoreSnapshot(RestClient client, Properties cfg) throws IOException {
73+
int status = client.performRequest(new Request("HEAD", "/" + cfg.getProperty("index_name"))).getStatusLine().getStatusCode();
74+
if (status == 404) {
75+
Request createRepo = new Request("PUT", "/_snapshot/" + cfg.getProperty("gcs_repo_name"));
76+
createRepo.setJsonEntity(
77+
Strings.toString(
78+
new PutRepositoryRequest().type("gcs")
8479
.settings(
8580
Settings.builder()
8681
.put("bucket", cfg.getProperty("gcs_bucket_name"))
8782
.put("base_path", cfg.getProperty("gcs_base_path"))
8883
.put("client", cfg.getProperty("gcs_client_name"))
8984
.build()
90-
),
91-
RequestOptions.DEFAULT
92-
);
93-
RestoreSnapshotResponse resp = restHighLevelClient.snapshot()
94-
.restore(
95-
new RestoreSnapshotRequest(cfg.getProperty("gcs_repo_name"), cfg.getProperty("gcs_snapshot_name")).waitForCompletion(
96-
true
97-
),
98-
RequestOptions.DEFAULT
99-
);
85+
)
86+
)
87+
);
88+
client.performRequest(createRepo);
10089

90+
Request restoreRequest = new Request(
91+
"POST",
92+
"/_snapshot/" + cfg.getProperty("gcs_repo_name") + "/" + cfg.getProperty("gcs_snapshot_name") + "/_restore"
93+
);
94+
restoreRequest.addParameter("wait_for_completion", "true");
95+
ObjectPath restore = ObjectPath.createFromResponse(client.performRequest(restoreRequest));
10196
assertEquals(
10297
"Unable to restore snapshot: "
103-
+ resp.getRestoreInfo().toString()
98+
+ restore
10499
+ System.lineSeparator()
105100
+ "Please check server logs to find the underlying issue.",
106101
1,
107-
resp.getRestoreInfo().successfulShards()
102+
(int) restore.evaluate("snapshot.shards.successful")
108103
);
109104

110-
assertEquals(
111-
Long.parseLong(cfg.getProperty("index_doc_count")),
112-
restHighLevelClient.count(new CountRequest(cfg.getProperty("index_name")), RequestOptions.DEFAULT).getCount()
113-
);
105+
ESRestTestCase.assertDocCount(client, cfg.getProperty("index_name"), Long.parseLong(cfg.getProperty("index_doc_count")));
114106
}
115107
}
116108
}

x-pack/plugin/eql/qa/correctness/src/javaRestTest/java/org/elasticsearch/xpack/eql/EsEQLCorrectnessIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static void init() throws IOException {
6969

7070
@Before
7171
public void restoreDataFromGcsRepo() throws Exception {
72-
EqlDataLoader.restoreSnapshot(highLevelClient(), CFG);
72+
EqlDataLoader.restoreSnapshot(client(), CFG);
7373
}
7474

7575
@After

x-pack/qa/repository-old-versions/src/test/java/org/elasticsearch/oldrepos/OldRepositoryAccessIT.java

Lines changed: 56 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
import org.apache.http.entity.StringEntity;
1313
import org.elasticsearch.Version;
1414
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
15-
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest;
1615
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest;
17-
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
18-
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus;
19-
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest;
20-
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse;
2116
import org.elasticsearch.action.search.SearchRequest;
2217
import org.elasticsearch.action.search.SearchResponse;
2318
import org.elasticsearch.action.support.master.AcknowledgedResponse;
@@ -27,24 +22,20 @@
2722
import org.elasticsearch.client.RestClient;
2823
import org.elasticsearch.client.RestHighLevelClient;
2924
import org.elasticsearch.client.core.ShardsAcknowledgedResponse;
30-
import org.elasticsearch.cluster.SnapshotsInProgress;
3125
import org.elasticsearch.cluster.routing.Murmur3HashFunction;
3226
import org.elasticsearch.common.Strings;
3327
import org.elasticsearch.common.document.DocumentField;
3428
import org.elasticsearch.common.settings.SecureString;
3529
import org.elasticsearch.common.settings.Settings;
3630
import org.elasticsearch.common.util.concurrent.ThreadContext;
37-
import org.elasticsearch.common.util.set.Sets;
3831
import org.elasticsearch.core.Booleans;
3932
import org.elasticsearch.core.PathUtils;
4033
import org.elasticsearch.index.query.QueryBuilders;
4134
import org.elasticsearch.search.SearchHit;
4235
import org.elasticsearch.search.builder.SearchSourceBuilder;
4336
import org.elasticsearch.search.sort.SortBuilders;
4437
import org.elasticsearch.search.sort.SortOrder;
45-
import org.elasticsearch.snapshots.SnapshotInfo;
4638
import org.elasticsearch.snapshots.SnapshotState;
47-
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
4839
import org.elasticsearch.test.rest.ESRestTestCase;
4940
import org.elasticsearch.test.rest.ObjectPath;
5041
import org.elasticsearch.xcontent.XContentBuilder;
@@ -61,10 +52,11 @@
6152
import java.util.Set;
6253
import java.util.stream.Collectors;
6354

55+
import static org.hamcrest.Matchers.contains;
6456
import static org.hamcrest.Matchers.empty;
57+
import static org.hamcrest.Matchers.equalTo;
6558
import static org.hamcrest.Matchers.greaterThan;
6659
import static org.hamcrest.Matchers.hasKey;
67-
import static org.hamcrest.Matchers.hasSize;
6860
import static org.hamcrest.Matchers.instanceOf;
6961
import static org.hamcrest.Matchers.not;
7062
import static org.hamcrest.Matchers.startsWith;
@@ -204,58 +196,54 @@ private void beforeRestart(
204196
if (sourceOnlyRepository) {
205197
repoSettingsBuilder.put("delegate_type", "fs");
206198
}
207-
ElasticsearchAssertions.assertAcked(
208-
client.snapshot()
209-
.createRepository(
210-
new PutRepositoryRequest(repoName).type(sourceOnlyRepository ? "source" : "fs").settings(repoSettingsBuilder),
211-
RequestOptions.DEFAULT
212-
)
199+
Request createRepo = new Request("PUT", "/_snapshot/" + repoName);
200+
createRepo.setJsonEntity(
201+
Strings.toString(new PutRepositoryRequest().type(sourceOnlyRepository ? "source" : "fs").settings(repoSettingsBuilder.build()))
213202
);
203+
assertAcknowledged(client().performRequest(createRepo));
214204

215205
// list snapshots on new ES
216-
List<SnapshotInfo> snapshotInfos = client.snapshot()
217-
.get(new GetSnapshotsRequest(repoName).snapshots(new String[] { "_all" }), RequestOptions.DEFAULT)
218-
.getSnapshots();
219-
assertThat(snapshotInfos, hasSize(1));
220-
SnapshotInfo snapshotInfo = snapshotInfos.get(0);
221-
assertEquals(snapshotName, snapshotInfo.snapshotId().getName());
222-
assertEquals(repoName, snapshotInfo.repository());
223-
assertEquals(Arrays.asList(indexName), snapshotInfo.indices());
224-
assertEquals(SnapshotState.SUCCESS, snapshotInfo.state());
225-
assertEquals(numberOfShards, snapshotInfo.successfulShards());
226-
assertEquals(numberOfShards, snapshotInfo.totalShards());
227-
assertEquals(0, snapshotInfo.failedShards());
228-
assertEquals(oldVersion, snapshotInfo.version());
206+
Request getSnaps = new Request("GET", "/_snapshot/" + repoName + "/_all");
207+
Response getResponse = client().performRequest(getSnaps);
208+
ObjectPath getResp = ObjectPath.createFromResponse(getResponse);
209+
assertThat(getResp.evaluate("total"), equalTo(1));
210+
assertThat(getResp.evaluate("snapshots.0.snapshot"), equalTo(snapshotName));
211+
assertThat(getResp.evaluate("snapshots.0.repository"), equalTo(repoName));
212+
assertThat(getResp.evaluate("snapshots.0.indices"), contains(indexName));
213+
assertThat(getResp.evaluate("snapshots.0.state"), equalTo(SnapshotState.SUCCESS.toString()));
214+
assertEquals(numberOfShards, (int) getResp.evaluate("snapshots.0.shards.successful"));
215+
assertEquals(numberOfShards, (int) getResp.evaluate("snapshots.0.shards.total"));
216+
assertEquals(0, (int) getResp.evaluate("snapshots.0.shards.failed"));
217+
assertEquals(oldVersion.toString(), getResp.evaluate("snapshots.0.version"));
229218

230219
// list specific snapshot on new ES
231-
snapshotInfos = client.snapshot()
232-
.get(new GetSnapshotsRequest(repoName).snapshots(new String[] { snapshotName }), RequestOptions.DEFAULT)
233-
.getSnapshots();
234-
assertThat(snapshotInfos, hasSize(1));
235-
snapshotInfo = snapshotInfos.get(0);
236-
assertEquals(snapshotName, snapshotInfo.snapshotId().getName());
237-
assertEquals(repoName, snapshotInfo.repository());
238-
assertEquals(Arrays.asList(indexName), snapshotInfo.indices());
239-
assertEquals(SnapshotState.SUCCESS, snapshotInfo.state());
240-
assertEquals(numberOfShards, snapshotInfo.successfulShards());
241-
assertEquals(numberOfShards, snapshotInfo.totalShards());
242-
assertEquals(0, snapshotInfo.failedShards());
243-
assertEquals(oldVersion, snapshotInfo.version());
220+
getSnaps = new Request("GET", "/_snapshot/" + repoName + "/" + snapshotName);
221+
getResponse = client().performRequest(getSnaps);
222+
getResp = ObjectPath.createFromResponse(getResponse);
223+
assertThat(getResp.evaluate("total"), equalTo(1));
224+
assertThat(getResp.evaluate("snapshots.0.snapshot"), equalTo(snapshotName));
225+
assertThat(getResp.evaluate("snapshots.0.repository"), equalTo(repoName));
226+
assertThat(getResp.evaluate("snapshots.0.indices"), contains(indexName));
227+
assertThat(getResp.evaluate("snapshots.0.state"), equalTo(SnapshotState.SUCCESS.toString()));
228+
assertEquals(numberOfShards, (int) getResp.evaluate("snapshots.0.shards.successful"));
229+
assertEquals(numberOfShards, (int) getResp.evaluate("snapshots.0.shards.total"));
230+
assertEquals(0, (int) getResp.evaluate("snapshots.0.shards.failed"));
231+
assertEquals(oldVersion.toString(), getResp.evaluate("snapshots.0.version"));
244232

245233
// list advanced snapshot info on new ES
246-
SnapshotsStatusResponse snapshotsStatusResponse = client.snapshot()
247-
.status(new SnapshotsStatusRequest(repoName).snapshots(new String[] { snapshotName }), RequestOptions.DEFAULT);
248-
assertThat(snapshotsStatusResponse.getSnapshots(), hasSize(1));
249-
SnapshotStatus snapshotStatus = snapshotsStatusResponse.getSnapshots().get(0);
250-
assertEquals(snapshotName, snapshotStatus.getSnapshot().getSnapshotId().getName());
251-
assertEquals(repoName, snapshotStatus.getSnapshot().getRepository());
252-
assertEquals(Sets.newHashSet(indexName), snapshotStatus.getIndices().keySet());
253-
assertEquals(SnapshotsInProgress.State.SUCCESS, snapshotStatus.getState());
254-
assertEquals(numberOfShards, snapshotStatus.getShardsStats().getDoneShards());
255-
assertEquals(numberOfShards, snapshotStatus.getShardsStats().getTotalShards());
256-
assertEquals(0, snapshotStatus.getShardsStats().getFailedShards());
257-
assertThat(snapshotStatus.getStats().getTotalSize(), greaterThan(0L));
258-
assertThat(snapshotStatus.getStats().getTotalFileCount(), greaterThan(0));
234+
getSnaps = new Request("GET", "/_snapshot/" + repoName + "/" + snapshotName + "/_status");
235+
getResponse = client().performRequest(getSnaps);
236+
getResp = ObjectPath.createFromResponse(getResponse);
237+
assertThat(((List<?>) getResp.evaluate("snapshots")).size(), equalTo(1));
238+
assertThat(getResp.evaluate("snapshots.0.snapshot"), equalTo(snapshotName));
239+
assertThat(getResp.evaluate("snapshots.0.repository"), equalTo(repoName));
240+
assertThat(((Map<?, ?>) getResp.evaluate("snapshots.0.indices")).keySet(), contains(indexName));
241+
assertThat(getResp.evaluate("snapshots.0.state"), equalTo(SnapshotState.SUCCESS.toString()));
242+
assertEquals(numberOfShards, (int) getResp.evaluate("snapshots.0.shards_stats.done"));
243+
assertEquals(numberOfShards, (int) getResp.evaluate("snapshots.0.shards_stats.total"));
244+
assertEquals(0, (int) getResp.evaluate("snapshots.0.shards_stats.failed"));
245+
assertThat(getResp.evaluate("snapshots.0.stats.total.size_in_bytes"), greaterThan(0));
246+
assertThat(getResp.evaluate("snapshots.0.stats.total.file_count"), greaterThan(0));
259247

260248
// restore / mount and check whether searches work
261249
restoreMountAndVerify(
@@ -271,10 +259,9 @@ private void beforeRestart(
271259
);
272260

273261
// close indices
274-
RestClient llClient = client.getLowLevelClient();
275-
assertTrue(closeIndex(llClient, "restored_" + indexName).isShardsAcknowledged());
276-
assertTrue(closeIndex(llClient, "mounted_full_copy_" + indexName).isShardsAcknowledged());
277-
assertTrue(closeIndex(llClient, "mounted_shared_cache_" + indexName).isShardsAcknowledged());
262+
assertTrue(closeIndex(client(), "restored_" + indexName).isShardsAcknowledged());
263+
assertTrue(closeIndex(client(), "mounted_full_copy_" + indexName).isShardsAcknowledged());
264+
assertTrue(closeIndex(client(), "mounted_shared_cache_" + indexName).isShardsAcknowledged());
278265

279266
// restore / mount again
280267
restoreMountAndVerify(
@@ -311,23 +298,20 @@ private void restoreMountAndVerify(
311298
String snapshotName
312299
) throws IOException {
313300
// restore index
314-
RestoreSnapshotResponse restoreSnapshotResponse = client.snapshot()
315-
.restore(
316-
new RestoreSnapshotRequest(repoName, snapshotName).indices(indexName)
317-
.renamePattern("(.+)")
318-
.renameReplacement("restored_$1")
319-
.waitForCompletion(true),
320-
RequestOptions.DEFAULT
321-
);
322-
assertNotNull(restoreSnapshotResponse.getRestoreInfo());
323-
assertEquals(numberOfShards, restoreSnapshotResponse.getRestoreInfo().totalShards());
324-
assertEquals(numberOfShards, restoreSnapshotResponse.getRestoreInfo().successfulShards());
301+
Request restoreRequest = new Request("POST", "/_snapshot/" + repoName + "/" + snapshotName + "/_restore");
302+
restoreRequest.setJsonEntity(
303+
Strings.toString(new RestoreSnapshotRequest().indices(indexName).renamePattern("(.+)").renameReplacement("restored_$1"))
304+
);
305+
restoreRequest.addParameter("wait_for_completion", "true");
306+
Response restoreResponse = client().performRequest(restoreRequest);
307+
ObjectPath restore = ObjectPath.createFromResponse(restoreResponse);
308+
assertEquals(numberOfShards, (int) restore.evaluate("snapshot.shards.total"));
309+
assertEquals(numberOfShards, (int) restore.evaluate("snapshot.shards.successful"));
325310

326311
ensureGreen("restored_" + indexName);
327312

328313
String restoredIndex = "restored_" + indexName;
329-
RestClient llClient = client.getLowLevelClient();
330-
var response = responseAsMap(llClient.performRequest(new Request("GET", "/" + restoredIndex + "/_mapping")));
314+
var response = responseAsMap(client().performRequest(new Request("GET", "/" + restoredIndex + "/_mapping")));
331315
Map<?, ?> mapping = ObjectPath.evaluate(response, restoredIndex + ".mappings");
332316
logger.info("mapping for {}: {}", restoredIndex, mapping);
333317
assertThat(mapping, hasKey("_meta"));

0 commit comments

Comments
 (0)