5
5
*/
6
6
package org .elasticsearch .xpack .searchablesnapshots ;
7
7
8
+ import org .apache .lucene .index .IndexFileNames ;
8
9
import org .apache .lucene .search .TotalHits ;
9
10
import org .elasticsearch .ResourceNotFoundException ;
10
- import org .elasticsearch .action .ActionFuture ;
11
11
import org .elasticsearch .action .admin .cluster .snapshots .create .CreateSnapshotResponse ;
12
12
import org .elasticsearch .action .admin .cluster .snapshots .restore .RestoreSnapshotResponse ;
13
13
import org .elasticsearch .action .admin .indices .recovery .RecoveryResponse ;
14
14
import org .elasticsearch .action .index .IndexRequestBuilder ;
15
15
import org .elasticsearch .cluster .metadata .IndexMetaData ;
16
16
import org .elasticsearch .cluster .node .DiscoveryNode ;
17
+ import org .elasticsearch .cluster .routing .ShardRouting ;
17
18
import org .elasticsearch .common .Priority ;
18
19
import org .elasticsearch .common .Strings ;
19
20
import org .elasticsearch .common .settings .Settings ;
26
27
import org .elasticsearch .plugins .Plugin ;
27
28
import org .elasticsearch .snapshots .SnapshotInfo ;
28
29
import org .elasticsearch .test .ESIntegTestCase ;
29
- import org .elasticsearch .xpack .core .searchablesnapshots .SearchableSnapshotShardStats ;
30
30
import org .elasticsearch .xpack .core .searchablesnapshots .MountSearchableSnapshotAction ;
31
31
import org .elasticsearch .xpack .core .searchablesnapshots .MountSearchableSnapshotRequest ;
32
+ import org .elasticsearch .xpack .core .searchablesnapshots .SearchableSnapshotShardStats ;
32
33
import org .elasticsearch .xpack .searchablesnapshots .action .SearchableSnapshotsStatsAction ;
33
34
import org .elasticsearch .xpack .searchablesnapshots .action .SearchableSnapshotsStatsRequest ;
34
35
import org .elasticsearch .xpack .searchablesnapshots .action .SearchableSnapshotsStatsResponse ;
@@ -112,6 +113,9 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception {
112
113
.setTrackTotalHits (true ).setQuery (matchQuery ("foo" , "bar" )).get ().getHits ().getTotalHits ();
113
114
logger .info ("--> [{}] in total, of which [{}] match the query" , originalAllHits , originalBarHits );
114
115
116
+ expectThrows (ResourceNotFoundException .class , "Searchable snapshot stats on a non snapshot searchable index should fail" ,
117
+ () -> client ().execute (SearchableSnapshotsStatsAction .INSTANCE , new SearchableSnapshotsStatsRequest ()).actionGet ());
118
+
115
119
CreateSnapshotResponse createSnapshotResponse = client ().admin ().cluster ().prepareCreateSnapshot (fsRepoName , snapshotName )
116
120
.setWaitForCompletion (true ).get ();
117
121
final SnapshotInfo snapshotInfo = createSnapshotResponse .getSnapshotInfo ();
@@ -154,29 +158,11 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception {
154
158
assertTrue (SearchableSnapshots .SNAPSHOT_INDEX_ID_SETTING .exists (settings ));
155
159
156
160
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 );
177
162
178
163
internalCluster ().fullRestart ();
179
164
assertRecovered (restoredIndexName , originalAllHits , originalBarHits );
165
+ assertSearchableSnapshotStats (restoredIndexName , cacheEnabled , nonCachedExtensions );
180
166
181
167
internalCluster ().ensureAtLeastNumDataNodes (2 );
182
168
@@ -192,6 +178,7 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception {
192
178
.setWaitForNoRelocatingShards (true ).setWaitForEvents (Priority .LANGUID ).get ().isTimedOut ());
193
179
194
180
assertRecovered (restoredIndexName , originalAllHits , originalBarHits );
181
+ assertSearchableSnapshotStats (restoredIndexName , cacheEnabled , nonCachedExtensions );
195
182
}
196
183
197
184
private void assertRecovered (String indexName , TotalHits originalAllHits , TotalHits originalBarHits ) throws Exception {
@@ -239,4 +226,41 @@ private void assertRecovered(String indexName, TotalHits originalAllHits, TotalH
239
226
assertThat (barTotalHits , equalTo (originalBarHits ));
240
227
}
241
228
}
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
+ }
242
266
}
0 commit comments