19
19
20
20
package org .elasticsearch .indices ;
21
21
22
+ import org .apache .lucene .document .LongPoint ;
23
+ import org .apache .lucene .search .Query ;
22
24
import org .elasticsearch .cluster .ClusterName ;
23
25
import org .elasticsearch .cluster .routing .allocation .DiskThresholdSettings ;
26
+ import org .elasticsearch .common .bytes .BytesArray ;
27
+ import org .elasticsearch .common .bytes .BytesReference ;
28
+ import org .elasticsearch .common .cache .RemovalNotification ;
24
29
import org .elasticsearch .common .settings .Settings ;
25
30
import org .elasticsearch .common .util .concurrent .EsExecutors ;
26
31
import org .elasticsearch .env .Environment ;
27
32
import org .elasticsearch .env .NodeEnvironment ;
33
+ import org .elasticsearch .index .IndexModule ;
28
34
import org .elasticsearch .index .IndexService ;
35
+ import org .elasticsearch .index .engine .Engine .Searcher ;
29
36
import org .elasticsearch .index .shard .IndexShard ;
37
+ import org .elasticsearch .indices .IndicesRequestCache .Key ;
30
38
import org .elasticsearch .indices .breaker .HierarchyCircuitBreakerService ;
31
39
import org .elasticsearch .node .MockNode ;
32
40
import org .elasticsearch .node .Node ;
33
41
import org .elasticsearch .node .NodeValidationException ;
34
42
import org .elasticsearch .script .ScriptService ;
35
43
import org .elasticsearch .test .ESTestCase ;
44
+ import org .elasticsearch .test .InternalSettingsPlugin ;
36
45
import org .elasticsearch .test .InternalTestCluster ;
37
46
import org .elasticsearch .test .MockHttpTransport ;
47
+ import org .elasticsearch .test .hamcrest .ElasticsearchAssertions ;
38
48
import org .elasticsearch .transport .nio .MockNioTransportPlugin ;
39
49
40
50
import java .nio .file .Path ;
41
51
import java .util .Arrays ;
52
+ import java .util .Collections ;
42
53
43
54
import static org .elasticsearch .cluster .coordination .ClusterBootstrapService .INITIAL_MASTER_NODES_SETTING ;
44
55
import static org .elasticsearch .cluster .metadata .IndexMetaData .SETTING_NUMBER_OF_REPLICAS ;
@@ -71,9 +82,11 @@ private Node startNode() throws NodeValidationException {
71
82
.put (HierarchyCircuitBreakerService .USE_REAL_MEMORY_USAGE_SETTING .getKey (), false )
72
83
.putList (DISCOVERY_SEED_HOSTS_SETTING .getKey ()) // empty list disables a port scan for other nodes
73
84
.putList (INITIAL_MASTER_NODES_SETTING .getKey (), nodeName )
85
+ .put (IndicesQueryCache .INDICES_QUERIES_CACHE_ALL_SEGMENTS_SETTING .getKey (), true )
74
86
.build ();
75
87
76
- Node node = new MockNode (settings , Arrays .asList (MockNioTransportPlugin .class , MockHttpTransport .TestPlugin .class ), true );
88
+ Node node = new MockNode (settings ,
89
+ Arrays .asList (MockNioTransportPlugin .class , MockHttpTransport .TestPlugin .class , InternalSettingsPlugin .class ), true );
77
90
node .start ();
78
91
return node ;
79
92
}
@@ -100,7 +113,7 @@ public void testCloseNonEmptyIndicesService() throws Exception {
100
113
assertEquals (0 , indicesService .indicesRefCount .refCount ());
101
114
}
102
115
103
- public void testCloseWhileOngoingRequest () throws Exception {
116
+ public void testCloseWithIncedRefStore () throws Exception {
104
117
Node node = startNode ();
105
118
IndicesService indicesService = node .injector ().getInstance (IndicesService .class );
106
119
assertEquals (1 , indicesService .indicesRefCount .refCount ());
@@ -121,4 +134,157 @@ public void testCloseWhileOngoingRequest() throws Exception {
121
134
assertEquals (0 , indicesService .indicesRefCount .refCount ());
122
135
}
123
136
137
+ public void testCloseWhileOngoingRequest () throws Exception {
138
+ Node node = startNode ();
139
+ IndicesService indicesService = node .injector ().getInstance (IndicesService .class );
140
+ assertEquals (1 , indicesService .indicesRefCount .refCount ());
141
+
142
+ assertAcked (node .client ().admin ().indices ().prepareCreate ("test" )
143
+ .setSettings (Settings .builder ().put (SETTING_NUMBER_OF_SHARDS , 1 ).put (SETTING_NUMBER_OF_REPLICAS , 0 )));
144
+ node .client ().prepareIndex ("test" , "_doc" , "1" ).setSource (Collections .emptyMap ()).get ();
145
+ ElasticsearchAssertions .assertAllSuccessful (node .client ().admin ().indices ().prepareRefresh ("test" ).get ());
146
+
147
+ assertEquals (2 , indicesService .indicesRefCount .refCount ());
148
+
149
+ IndexService indexService = indicesService .iterator ().next ();
150
+ IndexShard shard = indexService .getShard (0 );
151
+ Searcher searcher = shard .acquireSearcher ("test" );
152
+ assertEquals (1 , searcher .reader ().maxDoc ());
153
+
154
+ node .close ();
155
+ assertEquals (1 , indicesService .indicesRefCount .refCount ());
156
+
157
+ searcher .close ();
158
+ assertEquals (0 , indicesService .indicesRefCount .refCount ());
159
+ }
160
+
161
+ public void testCloseAfterRequestHasUsedQueryCache () throws Exception {
162
+ Node node = startNode ();
163
+ IndicesService indicesService = node .injector ().getInstance (IndicesService .class );
164
+ assertEquals (1 , indicesService .indicesRefCount .refCount ());
165
+
166
+ assertAcked (node .client ().admin ().indices ().prepareCreate ("test" )
167
+ .setSettings (Settings .builder ().put (SETTING_NUMBER_OF_SHARDS , 1 )
168
+ .put (SETTING_NUMBER_OF_REPLICAS , 0 )
169
+ .put (IndexModule .INDEX_QUERY_CACHE_EVERYTHING_SETTING .getKey (), true )));
170
+ node .client ().prepareIndex ("test" , "_doc" , "1" ).setSource (Collections .singletonMap ("foo" , 3L )).get ();
171
+ ElasticsearchAssertions .assertAllSuccessful (node .client ().admin ().indices ().prepareRefresh ("test" ).get ());
172
+
173
+ assertEquals (2 , indicesService .indicesRefCount .refCount ());
174
+
175
+ IndicesQueryCache cache = indicesService .getIndicesQueryCache ();
176
+
177
+ IndexService indexService = indicesService .iterator ().next ();
178
+ IndexShard shard = indexService .getShard (0 );
179
+ Searcher searcher = shard .acquireSearcher ("test" );
180
+ assertEquals (1 , searcher .reader ().maxDoc ());
181
+
182
+ Query query = LongPoint .newRangeQuery ("foo" , 0 , 5 );
183
+ assertEquals (0L , cache .getStats (shard .shardId ()).getCacheSize ());
184
+ searcher .searcher ().count (query );
185
+ assertEquals (1L , cache .getStats (shard .shardId ()).getCacheSize ());
186
+
187
+ searcher .close ();
188
+ assertEquals (2 , indicesService .indicesRefCount .refCount ());
189
+ assertEquals (1L , cache .getStats (shard .shardId ()).getCacheSize ());
190
+
191
+ node .close ();
192
+ assertEquals (0 , indicesService .indicesRefCount .refCount ());
193
+ assertEquals (0L , cache .getStats (shard .shardId ()).getCacheSize ());
194
+ }
195
+
196
+ public void testCloseWhileOngoingRequestUsesQueryCache () throws Exception {
197
+ Node node = startNode ();
198
+ IndicesService indicesService = node .injector ().getInstance (IndicesService .class );
199
+ assertEquals (1 , indicesService .indicesRefCount .refCount ());
200
+
201
+ assertAcked (node .client ().admin ().indices ().prepareCreate ("test" )
202
+ .setSettings (Settings .builder ().put (SETTING_NUMBER_OF_SHARDS , 1 )
203
+ .put (SETTING_NUMBER_OF_REPLICAS , 0 )
204
+ .put (IndexModule .INDEX_QUERY_CACHE_EVERYTHING_SETTING .getKey (), true )));
205
+ node .client ().prepareIndex ("test" , "_doc" , "1" ).setSource (Collections .singletonMap ("foo" , 3L )).get ();
206
+ ElasticsearchAssertions .assertAllSuccessful (node .client ().admin ().indices ().prepareRefresh ("test" ).get ());
207
+
208
+ assertEquals (2 , indicesService .indicesRefCount .refCount ());
209
+
210
+ IndicesQueryCache cache = indicesService .getIndicesQueryCache ();
211
+
212
+ IndexService indexService = indicesService .iterator ().next ();
213
+ IndexShard shard = indexService .getShard (0 );
214
+ Searcher searcher = shard .acquireSearcher ("test" );
215
+ assertEquals (1 , searcher .reader ().maxDoc ());
216
+
217
+ node .close ();
218
+ assertEquals (1 , indicesService .indicesRefCount .refCount ());
219
+
220
+ Query query = LongPoint .newRangeQuery ("foo" , 0 , 5 );
221
+ assertEquals (0L , cache .getStats (shard .shardId ()).getCacheSize ());
222
+ searcher .searcher ().count (query );
223
+ assertEquals (1L , cache .getStats (shard .shardId ()).getCacheSize ());
224
+
225
+ searcher .close ();
226
+ assertEquals (0 , indicesService .indicesRefCount .refCount ());
227
+ assertEquals (0L , cache .getStats (shard .shardId ()).getCacheSize ());
228
+ }
229
+
230
+ public void testCloseWhileOngoingRequestUsesRequestCache () throws Exception {
231
+ Node node = startNode ();
232
+ IndicesService indicesService = node .injector ().getInstance (IndicesService .class );
233
+ assertEquals (1 , indicesService .indicesRefCount .refCount ());
234
+
235
+ assertAcked (node .client ().admin ().indices ().prepareCreate ("test" )
236
+ .setSettings (Settings .builder ().put (SETTING_NUMBER_OF_SHARDS , 1 )
237
+ .put (SETTING_NUMBER_OF_REPLICAS , 0 )
238
+ .put (IndexModule .INDEX_QUERY_CACHE_EVERYTHING_SETTING .getKey (), true )));
239
+ node .client ().prepareIndex ("test" , "_doc" , "1" ).setSource (Collections .singletonMap ("foo" , 3L )).get ();
240
+ ElasticsearchAssertions .assertAllSuccessful (node .client ().admin ().indices ().prepareRefresh ("test" ).get ());
241
+
242
+ assertEquals (2 , indicesService .indicesRefCount .refCount ());
243
+
244
+ IndicesRequestCache cache = indicesService .indicesRequestCache ;
245
+
246
+ IndexService indexService = indicesService .iterator ().next ();
247
+ IndexShard shard = indexService .getShard (0 );
248
+ Searcher searcher = shard .acquireSearcher ("test" );
249
+ assertEquals (1 , searcher .reader ().maxDoc ());
250
+
251
+ node .close ();
252
+ assertEquals (1 , indicesService .indicesRefCount .refCount ());
253
+
254
+ assertEquals (0L , cache .count ());
255
+ IndicesRequestCache .CacheEntity cacheEntity = new IndicesRequestCache .CacheEntity () {
256
+ @ Override
257
+ public long ramBytesUsed () {
258
+ return 42 ;
259
+ }
260
+
261
+ @ Override
262
+ public void onCached (Key key , BytesReference value ) {}
263
+
264
+ @ Override
265
+ public boolean isOpen () {
266
+ return true ;
267
+ }
268
+
269
+ @ Override
270
+ public Object getCacheIdentity () {
271
+ return this ;
272
+ }
273
+
274
+ @ Override
275
+ public void onHit () {}
276
+
277
+ @ Override
278
+ public void onMiss () {}
279
+
280
+ @ Override
281
+ public void onRemoval (RemovalNotification <Key , BytesReference > notification ) {}
282
+ };
283
+ cache .getOrCompute (cacheEntity , () -> new BytesArray ("bar" ), searcher .getDirectoryReader (), new BytesArray ("foo" ), () -> "foo" );
284
+ assertEquals (1L , cache .count ());
285
+
286
+ searcher .close ();
287
+ assertEquals (0 , indicesService .indicesRefCount .refCount ());
288
+ assertEquals (0L , cache .count ());
289
+ }
124
290
}
0 commit comments