36
36
37
37
import org .apache .lucene .tests .util .LuceneTestCase .SuppressCodecs ;
38
38
import org .opensearch .action .DocWriteResponse ;
39
+ import org .opensearch .action .admin .cluster .node .stats .NodeStats ;
40
+ import org .opensearch .action .admin .cluster .node .stats .NodesStatsRequestBuilder ;
39
41
import org .opensearch .action .admin .cluster .node .stats .NodesStatsResponse ;
40
42
import org .opensearch .action .admin .indices .create .CreateIndexRequest ;
41
43
import org .opensearch .action .admin .indices .forcemerge .ForceMergeResponse ;
70
72
import org .opensearch .index .VersionType ;
71
73
import org .opensearch .index .cache .query .QueryCacheStats ;
72
74
import org .opensearch .index .engine .VersionConflictEngineException ;
75
+ import org .opensearch .index .fielddata .FieldDataStats ;
73
76
import org .opensearch .index .query .QueryBuilders ;
74
77
import org .opensearch .index .remote .RemoteSegmentStats ;
75
78
import org .opensearch .index .shard .IndexShard ;
116
119
import static org .hamcrest .Matchers .equalTo ;
117
120
import static org .hamcrest .Matchers .greaterThan ;
118
121
import static org .hamcrest .Matchers .is ;
119
- import static org .hamcrest .Matchers .lessThan ;
120
122
import static org .hamcrest .Matchers .notNullValue ;
121
123
import static org .hamcrest .Matchers .nullValue ;
122
124
@@ -165,7 +167,7 @@ private Settings.Builder settingsBuilder() {
165
167
return Settings .builder ().put (indexSettings ());
166
168
}
167
169
168
- public void testFieldDataStats () throws InterruptedException {
170
+ public void testFieldDataStats () throws Exception {
169
171
assertAcked (
170
172
client ().admin ()
171
173
.indices ()
@@ -175,117 +177,86 @@ public void testFieldDataStats() throws InterruptedException {
175
177
.get ()
176
178
);
177
179
ensureGreen ();
178
- client ().prepareIndex ("test" ).setId ("1" ).setSource ("field" , "value1" , "field2" , "value1" ).execute ().actionGet ();
179
- client ().prepareIndex ("test" ).setId ("2" ).setSource ("field" , "value2" , "field2" , "value2" ).execute ().actionGet ();
180
+ // Index enough docs to be sure neither primary shard is empty
181
+ for (int i = 0 ; i < 100 ; i ++) {
182
+ client ().prepareIndex ("test" )
183
+ .setId (Integer .toString (i ))
184
+ .setSource ("field" , "value" + i , "field2" , "value" + i )
185
+ .execute ()
186
+ .actionGet ();
187
+ }
180
188
refreshAndWaitForReplication ();
181
189
indexRandomForConcurrentSearch ("test" );
190
+ // Force merge to 1 segment so we can predict counts
191
+ client ().admin ().indices ().prepareForceMerge ().setMaxNumSegments (1 ).execute ().actionGet ();
192
+ refreshAndWaitForReplication ();
182
193
183
- NodesStatsResponse nodesStats = client ().admin ().cluster ().prepareNodesStats ("data:true" ).setIndices (true ).execute ().actionGet ();
184
- assertThat (
185
- nodesStats .getNodes ().get (0 ).getIndices ().getFieldData ().getMemorySizeInBytes () + nodesStats .getNodes ()
186
- .get (1 )
187
- .getIndices ()
188
- .getFieldData ()
189
- .getMemorySizeInBytes (),
190
- equalTo (0L )
191
- );
192
- IndicesStatsResponse indicesStats = client ().admin ()
193
- .indices ()
194
- .prepareStats ("test" )
195
- .clear ()
196
- .setFieldData (true )
197
- .execute ()
198
- .actionGet ();
199
- assertThat (indicesStats .getTotal ().getFieldData ().getMemorySizeInBytes (), equalTo (0L ));
194
+ for (FieldDataStats totalStats : List .of (getTotalFieldDataStats (false ), getIndicesFieldDataStats (false ))) {
195
+ assertEquals (0 , totalStats .getMemorySizeInBytes ());
196
+ assertEquals (0 , totalStats .getItemCount ());
197
+ }
200
198
201
199
// sort to load it to field data...
202
200
client ().prepareSearch ().addSort ("field" , SortOrder .ASC ).execute ().actionGet ();
203
- client ().prepareSearch ().addSort ("field" , SortOrder .ASC ).execute ().actionGet ();
204
201
205
- nodesStats = client ().admin ().cluster ().prepareNodesStats ("data:true" ).setIndices (true ).execute ().actionGet ();
206
- assertThat (
207
- nodesStats .getNodes ().get (0 ).getIndices ().getFieldData ().getMemorySizeInBytes () + nodesStats .getNodes ()
208
- .get (1 )
209
- .getIndices ()
210
- .getFieldData ()
211
- .getMemorySizeInBytes (),
212
- greaterThan (0L )
213
- );
214
- indicesStats = client ().admin ().indices ().prepareStats ("test" ).clear ().setFieldData (true ).execute ().actionGet ();
215
- assertThat (indicesStats .getTotal ().getFieldData ().getMemorySizeInBytes (), greaterThan (0L ));
202
+ for (FieldDataStats totalStats : List .of (getTotalFieldDataStats (false ), getIndicesFieldDataStats (false ))) {
203
+ assertTrue (totalStats .getMemorySizeInBytes () > 0 );
204
+ // The search should have hit 2 shards of the total 4 shards, each of which has 1 segment. So we expect 2 entries.
205
+ assertEquals (2 , totalStats .getItemCount ());
206
+ }
216
207
217
208
// sort to load it to field data...
218
209
client ().prepareSearch ().addSort ("field2" , SortOrder .ASC ).execute ().actionGet ();
219
- client ().prepareSearch ().addSort ("field2" , SortOrder .ASC ).execute ().actionGet ();
220
210
221
- // now check the per field stats
222
- nodesStats = client ().admin ()
223
- .cluster ()
224
- .prepareNodesStats ("data:true" )
225
- .setIndices (new CommonStatsFlags ().set (CommonStatsFlags .Flag .FieldData , true ).fieldDataFields ("*" ))
226
- .execute ()
227
- .actionGet ();
228
- assertThat (
229
- nodesStats .getNodes ().get (0 ).getIndices ().getFieldData ().getMemorySizeInBytes () + nodesStats .getNodes ()
230
- .get (1 )
231
- .getIndices ()
232
- .getFieldData ()
233
- .getMemorySizeInBytes (),
234
- greaterThan (0L )
235
- );
236
- assertThat (
237
- nodesStats .getNodes ().get (0 ).getIndices ().getFieldData ().getFieldMemorySizes ().get ("field" ) + nodesStats .getNodes ()
238
- .get (1 )
239
- .getIndices ()
240
- .getFieldData ()
241
- .getFieldMemorySizes ()
242
- .get ("field" ),
243
- greaterThan (0L )
244
- );
245
- assertThat (
246
- nodesStats .getNodes ().get (0 ).getIndices ().getFieldData ().getFieldMemorySizes ().get ("field" ) + nodesStats .getNodes ()
247
- .get (1 )
248
- .getIndices ()
249
- .getFieldData ()
250
- .getFieldMemorySizes ()
251
- .get ("field" ),
252
- lessThan (
253
- nodesStats .getNodes ().get (0 ).getIndices ().getFieldData ().getMemorySizeInBytes () + nodesStats .getNodes ()
254
- .get (1 )
255
- .getIndices ()
256
- .getFieldData ()
257
- .getMemorySizeInBytes ()
258
- )
259
- );
211
+ // Now we expect 4 total entries, one per searched segment per field
212
+ assertEquals (4 , getTotalFieldDataStats (false ).getItemCount ());
260
213
261
- indicesStats = client ().admin ()
262
- .indices ()
263
- .prepareStats ("test" )
264
- .clear ()
265
- .setFieldData (true )
266
- .setFieldDataFields ("*" )
267
- .execute ()
268
- .actionGet ();
269
- assertThat (indicesStats .getTotal ().getFieldData ().getMemorySizeInBytes (), greaterThan (0L ));
270
- assertThat (indicesStats .getTotal ().getFieldData ().getFieldMemorySizes ().get ("field" ), greaterThan (0L ));
271
- assertThat (
272
- indicesStats .getTotal ().getFieldData ().getFieldMemorySizes ().get ("field" ),
273
- lessThan (indicesStats .getTotal ().getFieldData ().getMemorySizeInBytes ())
274
- );
214
+ // now check the per field stats
215
+ for (FieldDataStats totalStats : List .of (getTotalFieldDataStats (true ), getIndicesFieldDataStats (true ))) {
216
+ assertTrue (totalStats .getMemorySizeInBytes () > 0 );
217
+ for (String fieldName : List .of ("field" , "field2" )) {
218
+ assertTrue (totalStats .getFieldMemorySizes ().get (fieldName ) > 0 );
219
+ assertEquals (2 , totalStats .getFieldItemCounts ().get (fieldName ));
220
+ assertTrue (totalStats .getFieldMemorySizes ().get (fieldName ) < totalStats .getMemorySizeInBytes ());
221
+ }
222
+ }
275
223
276
224
client ().admin ().indices ().prepareClearCache ().setFieldDataCache (true ).execute ().actionGet ();
277
- nodesStats = client ().admin ().cluster ().prepareNodesStats ("data:true" ).setIndices (true ).execute ().actionGet ();
278
- assertThat (
279
- nodesStats .getNodes ().get (0 ).getIndices ().getFieldData ().getMemorySizeInBytes () + nodesStats .getNodes ()
280
- .get (1 )
281
- .getIndices ()
282
- .getFieldData ()
283
- .getMemorySizeInBytes (),
284
- equalTo (0L )
285
- );
286
- indicesStats = client ().admin ().indices ().prepareStats ("test" ).clear ().setFieldData (true ).execute ().actionGet ();
287
- assertThat (indicesStats .getTotal ().getFieldData ().getMemorySizeInBytes (), equalTo (0L ));
225
+ assertBusy (() -> {
226
+ for (FieldDataStats postClearStats : List .of (getTotalFieldDataStats (true ), getIndicesFieldDataStats (true ))) {
227
+ assertEquals (0 , postClearStats .getMemorySizeInBytes ());
228
+ assertEquals (0 , postClearStats .getItemCount ());
229
+ for (long fieldMemorySize : postClearStats .getFieldMemorySizes ().getStats ().values ()) {
230
+ assertEquals (0 , fieldMemorySize );
231
+ }
232
+ for (long fieldItemCount : postClearStats .getFieldItemCounts ().getStats ().values ()) {
233
+ assertEquals (0 , fieldItemCount );
234
+ }
235
+ }
236
+ });
237
+ }
288
238
239
+ private FieldDataStats getTotalFieldDataStats (boolean setFieldDataFields ) {
240
+ NodesStatsRequestBuilder builder = client ().admin ().cluster ().prepareNodesStats ("data:true" );
241
+ if (setFieldDataFields ) {
242
+ builder .setIndices (new CommonStatsFlags ().set (CommonStatsFlags .Flag .FieldData , true ).fieldDataFields ("*" ));
243
+ } else {
244
+ builder .setIndices (true );
245
+ }
246
+ NodesStatsResponse nodesStats = builder .execute ().actionGet ();
247
+ FieldDataStats total = new FieldDataStats ();
248
+ for (NodeStats node : nodesStats .getNodes ()) {
249
+ total .add (node .getIndices ().getFieldData ());
250
+ }
251
+ return total ;
252
+ }
253
+
254
+ private FieldDataStats getIndicesFieldDataStats (boolean setFieldDataFields ) {
255
+ IndicesStatsRequestBuilder builder = client ().admin ().indices ().prepareStats ("test" ).clear ().setFieldData (true );
256
+ if (setFieldDataFields ) {
257
+ builder .setFieldDataFields ("*" );
258
+ }
259
+ return builder .execute ().actionGet ().getTotal ().getFieldData ();
289
260
}
290
261
291
262
public void testClearAllCaches () throws Exception {
0 commit comments