Skip to content

Commit f1f598e

Browse files
authored
Exclude indices with cache disabled from searchable snapshots stats (#52770)
Searchable snapshots stats only make sense for indices with the setting SNAPSHOT_CACHE_ENABLED_SETTING enabled. This commit prevents non-cached indices to be reported in searchable snapshots stats.
1 parent c049b02 commit f1f598e

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/action/SearchableSnapshotsStatsResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected void addCustomXContentFields(XContentBuilder builder, Params params) t
4747
.map(SearchableSnapshotShardStats::getShardRouting)
4848
.map(ShardRouting::index)
4949
.sorted(Comparator.comparing(Index::getName))
50+
.distinct()
5051
.collect(toList());
5152

5253
builder.startObject("indices");

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/action/TransportSearchableSnapshotsStatsAction.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.stream.Collectors;
4242

4343
import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING;
44+
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotRepository.SNAPSHOT_CACHE_ENABLED_SETTING;
4445
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotRepository.SNAPSHOT_DIRECTORY_FACTORY_KEY;
4546

4647
public class TransportSearchableSnapshotsStatsAction extends TransportBroadcastByNodeAction<SearchableSnapshotsStatsRequest,
@@ -94,7 +95,9 @@ protected ShardsIterator shards(ClusterState state, SearchableSnapshotsStatsRequ
9495
if (indexMetaData != null) {
9596
Settings indexSettings = indexMetaData.getSettings();
9697
if (INDEX_STORE_TYPE_SETTING.get(indexSettings).equals(SNAPSHOT_DIRECTORY_FACTORY_KEY)) {
97-
searchableSnapshotIndices.add(concreteIndex);
98+
if (SNAPSHOT_CACHE_ENABLED_SETTING.get(indexSettings)) {
99+
searchableSnapshotIndices.add(concreteIndex);
100+
}
98101
}
99102
}
100103
}

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/AbstractSearchableSnapshotsRestTestCase.java

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

88
import org.apache.http.client.methods.HttpDelete;
9+
import org.apache.http.client.methods.HttpGet;
910
import org.apache.http.client.methods.HttpPost;
1011
import org.apache.http.client.methods.HttpPut;
1112
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
@@ -15,6 +16,8 @@
1516
import org.elasticsearch.client.ResponseException;
1617
import org.elasticsearch.cluster.metadata.IndexMetaData;
1718
import org.elasticsearch.common.Strings;
19+
import org.elasticsearch.common.bytes.BytesReference;
20+
import org.elasticsearch.common.io.Streams;
1821
import org.elasticsearch.common.settings.Settings;
1922
import org.elasticsearch.common.xcontent.XContentHelper;
2023
import org.elasticsearch.common.xcontent.XContentType;
@@ -54,9 +57,11 @@ public void testSearchableSnapshots() throws Exception {
5457
Settings.builder().put("delegate_type", repositoryType).put("readonly", true).put(repositorySettings).build());
5558

5659
final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
60+
final int numberOfShards = randomIntBetween(1, 5);
61+
5762
logger.info("creating index [{}]", indexName);
5863
createIndex(indexName, Settings.builder()
59-
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 5))
64+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numberOfShards)
6065
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
6166
.build());
6267
ensureGreen(indexName);
@@ -130,6 +135,10 @@ public void testSearchableSnapshots() throws Exception {
130135

131136
logger.info("deleting snapshot [{}]", snapshot);
132137
deleteSnapshot(repository, snapshot, false);
138+
139+
final Map<String, Object> searchableSnapshotStats = searchableSnapshotStats(restoredIndexName);
140+
assertThat("Expected searchable snapshots stats for " + numberOfShards + " shards but got " + searchableSnapshotStats,
141+
searchableSnapshotStats.size(), equalTo(numberOfShards));
133142
}
134143

135144
protected static void registerRepository(String repository, String type, boolean verify, Settings settings) throws IOException {
@@ -210,11 +219,27 @@ protected static Map<String, Object> search(String index, QueryBuilder query) th
210219
return responseAsMap;
211220
}
212221

222+
protected static Map<String, Object> searchableSnapshotStats(String index) throws IOException {
223+
final Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, '/' + index + "/_searchable_snapshots/stats"));
224+
assertThat("Failed to retrieve searchable snapshots stats for on index [" + index + "]: " + response,
225+
response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
226+
227+
final Map<String, Object> responseAsMap = responseAsMap(response);
228+
assertThat("Shard failures when retrieving searchable snapshots stats for index [" + index + "]: " + response,
229+
extractValue(responseAsMap, "_shards.failed"), equalTo(0));
230+
return extractValue(responseAsMap, "indices." + index + ".shards");
231+
}
232+
213233
protected static Map<String, Object> responseAsMap(Response response) throws IOException {
214234
final XContentType xContentType = XContentType.fromMediaTypeOrFormat(response.getEntity().getContentType().getValue());
215235
assertThat("Unknown XContentType", xContentType, notNullValue());
216-
try (InputStream responseBody = response.getEntity().getContent()) {
236+
237+
BytesReference bytesReference = Streams.readFully(response.getEntity().getContent());
238+
239+
try (InputStream responseBody = bytesReference.streamInput()) {
217240
return XContentHelper.convertToMap(xContentType.xContent(), responseBody, true);
241+
} catch (Exception e) {
242+
throw new IOException(bytesReference.utf8ToString(), e);
218243
}
219244
}
220245

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsIntegTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package org.elasticsearch.xpack.searchablesnapshots;
77

88
import org.apache.lucene.search.TotalHits;
9+
import org.elasticsearch.ResourceNotFoundException;
10+
import org.elasticsearch.action.ActionFuture;
911
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
1012
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
1113
import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse;
@@ -23,6 +25,10 @@
2325
import org.elasticsearch.plugins.Plugin;
2426
import org.elasticsearch.snapshots.SnapshotInfo;
2527
import org.elasticsearch.test.ESIntegTestCase;
28+
import org.elasticsearch.xpack.core.searchablesnapshots.SearchableSnapshotShardStats;
29+
import org.elasticsearch.xpack.searchablesnapshots.action.SearchableSnapshotsStatsAction;
30+
import org.elasticsearch.xpack.searchablesnapshots.action.SearchableSnapshotsStatsRequest;
31+
import org.elasticsearch.xpack.searchablesnapshots.action.SearchableSnapshotsStatsResponse;
2632
import org.elasticsearch.xpack.searchablesnapshots.cache.CacheService;
2733

2834
import java.nio.file.Path;
@@ -39,6 +45,7 @@
3945
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotRepository.SNAPSHOT_DIRECTORY_FACTORY_KEY;
4046
import static org.hamcrest.Matchers.equalTo;
4147
import static org.hamcrest.Matchers.greaterThan;
48+
import static org.hamcrest.Matchers.hasSize;
4249
import static org.hamcrest.Matchers.lessThanOrEqualTo;
4350

4451
public class SearchableSnapshotsIntegTests extends ESIntegTestCase {
@@ -117,12 +124,15 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception {
117124
.put("location", repo)
118125
.put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
119126

127+
final boolean cacheEnabled = randomBoolean();
128+
logger.info("--> restoring index [{}] with cache [{}]", restoredIndexName, cacheEnabled ? "enabled" : "disabled");
129+
120130
final RestoreSnapshotResponse restoreSnapshotResponse = client().admin().cluster()
121131
.prepareRestoreSnapshot(searchableRepoName, snapshotName).setIndices(indexName)
122132
.setRenamePattern(indexName)
123133
.setRenameReplacement(restoredIndexName)
124134
.setIndexSettings(Settings.builder()
125-
.put(SearchableSnapshotRepository.SNAPSHOT_CACHE_ENABLED_SETTING.getKey(), randomBoolean())
135+
.put(SearchableSnapshotRepository.SNAPSHOT_CACHE_ENABLED_SETTING.getKey(), cacheEnabled)
126136
.put(IndexSettings.INDEX_CHECK_ON_STARTUP.getKey(), Boolean.FALSE.toString())
127137
.build())
128138
.setWaitForCompletion(true).get();
@@ -139,6 +149,19 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception {
139149

140150
assertRecovered(restoredIndexName, originalAllHits, originalBarHits);
141151

152+
final SearchableSnapshotsStatsRequest request = new SearchableSnapshotsStatsRequest(restoredIndexName);
153+
final ActionFuture<SearchableSnapshotsStatsResponse> future = client().execute(SearchableSnapshotsStatsAction.INSTANCE, request);
154+
if (cacheEnabled) {
155+
final SearchableSnapshotsStatsResponse statsResponse = future.actionGet();
156+
assertThat(statsResponse.getStats(), hasSize(getNumShards(restoredIndexName).totalNumShards));
157+
for (SearchableSnapshotShardStats stats : statsResponse.getStats()) {
158+
assertThat(stats.getShardRouting().getIndexName(), equalTo(restoredIndexName));
159+
assertThat(stats.getStats().size(), greaterThan(0));
160+
}
161+
} else {
162+
expectThrows(ResourceNotFoundException.class, future::actionGet);
163+
}
164+
142165
internalCluster().fullRestart();
143166
assertRecovered(restoredIndexName, originalAllHits, originalBarHits);
144167

0 commit comments

Comments
 (0)