Skip to content

Commit 33c7dd8

Browse files
author
Peter Alfonsi
committed
tests
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
1 parent 887e58d commit 33c7dd8

File tree

2 files changed

+75
-100
lines changed

2 files changed

+75
-100
lines changed

server/src/internalClusterTest/java/org/opensearch/indices/stats/IndexStatsIT.java

Lines changed: 69 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
import org.apache.lucene.tests.util.LuceneTestCase.SuppressCodecs;
3838
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;
3941
import org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse;
4042
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
4143
import org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse;
@@ -70,6 +72,7 @@
7072
import org.opensearch.index.VersionType;
7173
import org.opensearch.index.cache.query.QueryCacheStats;
7274
import org.opensearch.index.engine.VersionConflictEngineException;
75+
import org.opensearch.index.fielddata.FieldDataStats;
7376
import org.opensearch.index.query.QueryBuilders;
7477
import org.opensearch.index.remote.RemoteSegmentStats;
7578
import org.opensearch.index.shard.IndexShard;
@@ -116,7 +119,6 @@
116119
import static org.hamcrest.Matchers.equalTo;
117120
import static org.hamcrest.Matchers.greaterThan;
118121
import static org.hamcrest.Matchers.is;
119-
import static org.hamcrest.Matchers.lessThan;
120122
import static org.hamcrest.Matchers.notNullValue;
121123
import static org.hamcrest.Matchers.nullValue;
122124

@@ -165,7 +167,7 @@ private Settings.Builder settingsBuilder() {
165167
return Settings.builder().put(indexSettings());
166168
}
167169

168-
public void testFieldDataStats() throws InterruptedException {
170+
public void testFieldDataStats() throws Exception {
169171
assertAcked(
170172
client().admin()
171173
.indices()
@@ -175,117 +177,86 @@ public void testFieldDataStats() throws InterruptedException {
175177
.get()
176178
);
177179
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+
}
180188
refreshAndWaitForReplication();
181189
indexRandomForConcurrentSearch("test");
190+
// Force merge to 1 segment so we can predict counts
191+
client().admin().indices().prepareForceMerge().setMaxNumSegments(1).execute().actionGet();
192+
refreshAndWaitForReplication();
182193

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+
}
200198

201199
// sort to load it to field data...
202200
client().prepareSearch().addSort("field", SortOrder.ASC).execute().actionGet();
203-
client().prepareSearch().addSort("field", SortOrder.ASC).execute().actionGet();
204201

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+
}
216207

217208
// sort to load it to field data...
218209
client().prepareSearch().addSort("field2", SortOrder.ASC).execute().actionGet();
219-
client().prepareSearch().addSort("field2", SortOrder.ASC).execute().actionGet();
220210

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());
260213

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+
}
275223

276224
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+
}
288238

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();
289260
}
290261

291262
public void testClearAllCaches() throws Exception {

server/src/test/java/org/opensearch/index/fielddata/FieldDataStatsTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@
4242
public class FieldDataStatsTests extends OpenSearchTestCase {
4343

4444
public void testSerialize() throws IOException {
45-
// TODO: Test both ctors here
4645
FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats();
47-
FieldDataStats stats = new FieldDataStats(randomNonNegativeLong(), randomNonNegativeLong(), map);
46+
// test both ctors
47+
FieldDataStats stats = randomBoolean()
48+
? new FieldDataStats(randomNonNegativeLong(), randomNonNegativeLong(), map)
49+
: new FieldDataStats(randomNonNegativeLong(), randomNonNegativeLong(), map, randomNonNegativeLong(), map);
4850
BytesStreamOutput out = new BytesStreamOutput();
4951
stats.writeTo(out);
5052
StreamInput input = out.bytes().streamInput();
@@ -53,5 +55,7 @@ public void testSerialize() throws IOException {
5355
assertEquals(stats.getEvictions(), read.getEvictions());
5456
assertEquals(stats.getMemorySize(), read.getMemorySize());
5557
assertEquals(stats.getFieldMemorySizes(), read.getFieldMemorySizes());
58+
assertEquals(stats.getItemCount(), read.getItemCount());
59+
assertEquals(stats.getFieldItemCounts(), read.getFieldItemCounts());
5660
}
5761
}

0 commit comments

Comments
 (0)