Skip to content

Commit ea80078

Browse files
committed
Adapt test and expose stats
1 parent 9b77eae commit ea80078

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.List;
3535

3636
import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING;
37-
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_CACHE_ENABLED_SETTING;
3837
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_DIRECTORY_FACTORY_KEY;
3938

4039
public abstract class AbstractTransportSearchableSnapshotsAction
@@ -76,9 +75,7 @@ protected ShardsIterator shards(ClusterState state, Request request, String[] co
7675
if (indexMetaData != null) {
7776
Settings indexSettings = indexMetaData.getSettings();
7877
if (INDEX_STORE_TYPE_SETTING.get(indexSettings).equals(SNAPSHOT_DIRECTORY_FACTORY_KEY)) {
79-
if (SNAPSHOT_CACHE_ENABLED_SETTING.get(indexSettings)) {
80-
searchableSnapshotIndices.add(concreteIndex);
81-
}
78+
searchableSnapshotIndices.add(concreteIndex);
8279
}
8380
}
8481
}

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

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
*/
66
package org.elasticsearch.xpack.searchablesnapshots;
77

8+
import org.apache.lucene.index.IndexFileNames;
89
import org.apache.lucene.search.TotalHits;
910
import org.elasticsearch.ResourceNotFoundException;
10-
import org.elasticsearch.action.ActionFuture;
1111
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
1212
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
1313
import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse;
1414
import org.elasticsearch.action.index.IndexRequestBuilder;
1515
import org.elasticsearch.cluster.metadata.IndexMetaData;
1616
import org.elasticsearch.cluster.node.DiscoveryNode;
17+
import org.elasticsearch.cluster.routing.ShardRouting;
1718
import org.elasticsearch.common.Priority;
1819
import org.elasticsearch.common.Strings;
1920
import org.elasticsearch.common.settings.Settings;
@@ -26,9 +27,9 @@
2627
import org.elasticsearch.plugins.Plugin;
2728
import org.elasticsearch.snapshots.SnapshotInfo;
2829
import org.elasticsearch.test.ESIntegTestCase;
29-
import org.elasticsearch.xpack.core.searchablesnapshots.SearchableSnapshotShardStats;
3030
import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotAction;
3131
import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest;
32+
import org.elasticsearch.xpack.core.searchablesnapshots.SearchableSnapshotShardStats;
3233
import org.elasticsearch.xpack.searchablesnapshots.action.SearchableSnapshotsStatsAction;
3334
import org.elasticsearch.xpack.searchablesnapshots.action.SearchableSnapshotsStatsRequest;
3435
import org.elasticsearch.xpack.searchablesnapshots.action.SearchableSnapshotsStatsResponse;
@@ -112,6 +113,9 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception {
112113
.setTrackTotalHits(true).setQuery(matchQuery("foo", "bar")).get().getHits().getTotalHits();
113114
logger.info("--> [{}] in total, of which [{}] match the query", originalAllHits, originalBarHits);
114115

116+
expectThrows(ResourceNotFoundException.class, "Searchable snapshot stats on a non snapshot searchable index should fail",
117+
() -> client().execute(SearchableSnapshotsStatsAction.INSTANCE, new SearchableSnapshotsStatsRequest()).actionGet());
118+
115119
CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(fsRepoName, snapshotName)
116120
.setWaitForCompletion(true).get();
117121
final SnapshotInfo snapshotInfo = createSnapshotResponse.getSnapshotInfo();
@@ -154,29 +158,11 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception {
154158
assertTrue(SearchableSnapshots.SNAPSHOT_INDEX_ID_SETTING.exists(settings));
155159

156160
assertRecovered(restoredIndexName, originalAllHits, originalBarHits);
157-
158-
final SearchableSnapshotsStatsRequest request = new SearchableSnapshotsStatsRequest(restoredIndexName);
159-
final ActionFuture<SearchableSnapshotsStatsResponse> future = client().execute(SearchableSnapshotsStatsAction.INSTANCE, request);
160-
if (cacheEnabled) {
161-
final SearchableSnapshotsStatsResponse statsResponse = future.actionGet();
162-
assertThat(statsResponse.getStats(), hasSize(getNumShards(restoredIndexName).totalNumShards));
163-
for (SearchableSnapshotShardStats stats : statsResponse.getStats()) {
164-
assertThat(stats.getShardRouting().getIndexName(), equalTo(restoredIndexName));
165-
assertThat(stats.getStats().size(), greaterThan(0));
166-
for (SearchableSnapshotShardStats.CacheIndexInputStats indexInputStats : stats.getStats()) {
167-
for (String ext : nonCachedExtensions) {
168-
if (indexInputStats.getFileName().endsWith(ext)) {
169-
assertEquals(indexInputStats.getFileName(), 0, indexInputStats.getOpenCount());
170-
}
171-
}
172-
}
173-
}
174-
} else {
175-
expectThrows(ResourceNotFoundException.class, future::actionGet);
176-
}
161+
assertSearchableSnapshotStats(restoredIndexName, cacheEnabled, nonCachedExtensions);
177162

178163
internalCluster().fullRestart();
179164
assertRecovered(restoredIndexName, originalAllHits, originalBarHits);
165+
assertSearchableSnapshotStats(restoredIndexName, cacheEnabled, nonCachedExtensions);
180166

181167
internalCluster().ensureAtLeastNumDataNodes(2);
182168

@@ -192,6 +178,7 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception {
192178
.setWaitForNoRelocatingShards(true).setWaitForEvents(Priority.LANGUID).get().isTimedOut());
193179

194180
assertRecovered(restoredIndexName, originalAllHits, originalBarHits);
181+
assertSearchableSnapshotStats(restoredIndexName, cacheEnabled, nonCachedExtensions);
195182
}
196183

197184
private void assertRecovered(String indexName, TotalHits originalAllHits, TotalHits originalBarHits) throws Exception {
@@ -239,4 +226,41 @@ private void assertRecovered(String indexName, TotalHits originalAllHits, TotalH
239226
assertThat(barTotalHits, equalTo(originalBarHits));
240227
}
241228
}
229+
230+
private void assertSearchableSnapshotStats(String indexName, boolean cacheEnabled, List<String> nonCachedExtensions) {
231+
final SearchableSnapshotsStatsResponse statsResponse = client().execute(SearchableSnapshotsStatsAction.INSTANCE,
232+
new SearchableSnapshotsStatsRequest(indexName)).actionGet();
233+
final NumShards restoredNumShards = getNumShards(indexName);
234+
assertThat(statsResponse.getStats(), hasSize(restoredNumShards.totalNumShards));
235+
236+
for (SearchableSnapshotShardStats stats : statsResponse.getStats()) {
237+
final ShardRouting shardRouting = stats.getShardRouting();
238+
assertThat(stats.getShardRouting().getIndexName(), equalTo(indexName));
239+
assertThat("Expecting stats to exist for at least 1 Lucene file", stats.getStats().size(), greaterThan(0));
240+
for (SearchableSnapshotShardStats.CacheIndexInputStats indexInputStats : stats.getStats()) {
241+
final String fileName = indexInputStats.getFileName();
242+
assertThat("Unexpected open count for " + fileName + " of shard " + shardRouting,
243+
indexInputStats.getOpenCount(), greaterThan(0L));
244+
assertThat("Unexpected close count for " + fileName + " of shard " + shardRouting,
245+
indexInputStats.getCloseCount(), lessThanOrEqualTo(indexInputStats.getOpenCount()));
246+
assertThat("Unexpected file length for " + fileName + " of shard " + shardRouting,
247+
indexInputStats.getFileLength(), greaterThan(0L));
248+
249+
if (cacheEnabled == false || nonCachedExtensions.contains(IndexFileNames.getExtension(fileName))) {
250+
assertThat("Expected at least 1 optimized or direct read for " + fileName + " of shard " + shardRouting,
251+
Math.max(indexInputStats.getOptimizedBytesRead().getCount(), indexInputStats.getDirectBytesRead().getCount()),
252+
greaterThan(0L));
253+
assertThat("Expected no cache read or write for " + fileName + " of shard " + shardRouting,
254+
Math.max(indexInputStats.getCachedBytesRead().getCount(), indexInputStats.getCachedBytesWritten().getCount()),
255+
equalTo(0L));
256+
} else {
257+
assertThat("Expected at least 1 cache read or write for " + fileName + " of shard " + shardRouting,
258+
Math.max(indexInputStats.getCachedBytesRead().getCount(), indexInputStats.getCachedBytesWritten().getCount()),
259+
greaterThan(0L));
260+
assertThat("Expected no optimized read for " + fileName + " of shard " + shardRouting,
261+
indexInputStats.getOptimizedBytesRead().getCount(), equalTo(0L));
262+
}
263+
}
264+
}
265+
}
242266
}

0 commit comments

Comments
 (0)