|
88 | 88 | import org.elasticsearch.index.seqno.SequenceNumbers;
|
89 | 89 | import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus;
|
90 | 90 | import org.elasticsearch.index.store.Store;
|
| 91 | +import org.elasticsearch.index.store.StoreStats; |
91 | 92 | import org.elasticsearch.index.translog.Translog;
|
92 | 93 | import org.elasticsearch.index.translog.TranslogTests;
|
93 | 94 | import org.elasticsearch.indices.IndicesQueryCache;
|
@@ -2273,54 +2274,57 @@ public void testDocStats() throws IOException {
|
2273 | 2274 | }
|
2274 | 2275 | }
|
2275 | 2276 |
|
2276 |
| - public void testEstimateAverageDocSize() throws Exception { |
| 2277 | + public void testEstimateTotalDocSize() throws Exception { |
2277 | 2278 | IndexShard indexShard = null;
|
2278 | 2279 | try {
|
2279 | 2280 | indexShard = newStartedShard(true);
|
2280 |
| - int smallDocNum = randomIntBetween(5, 100); |
2281 |
| - for (int i = 0; i < smallDocNum; i++) { |
2282 |
| - indexDoc(indexShard, "test", "small-" + i); |
2283 |
| - } |
2284 |
| - // Average document size is estimated by sampling segments, thus it should be zero without flushing. |
2285 |
| - DocsStats withoutFlush = indexShard.docStats(); |
2286 |
| - assertThat(withoutFlush.averageSizeInBytes, equalTo(0L)); |
2287 | 2281 |
|
2288 |
| - indexShard.flush(new FlushRequest()); |
2289 |
| - indexShard.refresh("test"); |
2290 |
| - DocsStats smallStats = indexShard.docStats(); |
2291 |
| - assertThat(smallStats.averageSizeInBytes, greaterThan(10L)); |
2292 |
| - |
2293 |
| - long storedAvgSize = indexShard.storeStats().sizeInBytes() / smallDocNum; |
2294 |
| - assertThat("Estimated average document size is too small compared with the average stored size", |
2295 |
| - smallStats.averageSizeInBytes, greaterThanOrEqualTo(storedAvgSize * 80/100)); |
2296 |
| - assertThat("Estimated average document size is too large compared with the average stored size", |
2297 |
| - smallStats.averageSizeInBytes, lessThanOrEqualTo(storedAvgSize * 120/100)); |
2298 |
| - |
2299 |
| - // Indexing large documents should increase the average document size. |
2300 |
| - int largeDocNum = randomIntBetween(100, 200); |
2301 |
| - for (int i = 0; i < largeDocNum; i++) { |
| 2282 | + int numDoc = randomIntBetween(100, 200); |
| 2283 | + for (int i = 0; i < numDoc; i++) { |
2302 | 2284 | String doc = XContentFactory.jsonBuilder()
|
2303 | 2285 | .startObject()
|
2304 | 2286 | .field("count", randomInt())
|
2305 | 2287 | .field("point", randomFloat())
|
2306 | 2288 | .field("description", randomUnicodeOfCodepointLength(100))
|
2307 | 2289 | .endObject().string();
|
2308 |
| - indexDoc(indexShard, "test", "large-" + i, doc); |
| 2290 | + indexDoc(indexShard, "doc", Integer.toString(i), doc); |
2309 | 2291 | }
|
| 2292 | + |
| 2293 | + assertThat("Without flushing, segment sizes should be zero", |
| 2294 | + indexShard.docStats().getTotalSizeInBytes(), equalTo(0L)); |
| 2295 | + |
2310 | 2296 | indexShard.flush(new FlushRequest());
|
2311 | 2297 | indexShard.refresh("test");
|
2312 |
| - DocsStats largeStats = indexShard.docStats(); |
2313 |
| - assertThat(largeStats.averageSizeInBytes, greaterThan(100L)); |
2314 |
| - assertThat(largeStats.averageSizeInBytes, greaterThan(smallStats.averageSizeInBytes)); |
| 2298 | + { |
| 2299 | + final DocsStats docsStats = indexShard.docStats(); |
| 2300 | + final StoreStats storeStats = indexShard.storeStats(); |
| 2301 | + assertThat(storeStats.sizeInBytes(), greaterThan(numDoc * 100L)); // A doc should be more than 100 bytes. |
2315 | 2302 |
|
2316 |
| - int deleteDocs = randomIntBetween(1, smallDocNum / 2); |
2317 |
| - for (int i = 0; i < deleteDocs; i++) { |
2318 |
| - deleteDoc(indexShard, "test", "small-" + i); |
| 2303 | + assertThat("Estimated total document size is too small compared with the stored size", |
| 2304 | + docsStats.getTotalSizeInBytes(), greaterThanOrEqualTo(storeStats.sizeInBytes() * 80/100)); |
| 2305 | + assertThat("Estimated total document size is too large compared with the stored size", |
| 2306 | + docsStats.getTotalSizeInBytes(), lessThanOrEqualTo(storeStats.sizeInBytes() * 120/100)); |
2319 | 2307 | }
|
| 2308 | + |
| 2309 | + // Do some updates and deletes, then recheck the correlation again. |
| 2310 | + for (int i = 0; i < numDoc / 2; i++) { |
| 2311 | + if (randomBoolean()) { |
| 2312 | + deleteDoc(indexShard, "doc", Integer.toString(i)); |
| 2313 | + } else { |
| 2314 | + indexDoc(indexShard, "doc", Integer.toString(i), "{\"foo\": \"bar\"}"); |
| 2315 | + } |
| 2316 | + } |
| 2317 | + |
2320 | 2318 | indexShard.flush(new FlushRequest());
|
2321 | 2319 | indexShard.refresh("test");
|
2322 |
| - DocsStats withDeletedStats = indexShard.docStats(); |
2323 |
| - assertThat(withDeletedStats.averageSizeInBytes, greaterThan(largeStats.averageSizeInBytes)); |
| 2320 | + { |
| 2321 | + final DocsStats docsStats = indexShard.docStats(); |
| 2322 | + final StoreStats storeStats = indexShard.storeStats(); |
| 2323 | + assertThat("Estimated total document size is too small compared with the stored size", |
| 2324 | + docsStats.getTotalSizeInBytes(), greaterThanOrEqualTo(storeStats.sizeInBytes() * 80/100)); |
| 2325 | + assertThat("Estimated total document size is too large compared with the stored size", |
| 2326 | + docsStats.getTotalSizeInBytes(), lessThanOrEqualTo(storeStats.sizeInBytes() * 120/100)); |
| 2327 | + } |
2324 | 2328 |
|
2325 | 2329 | } finally {
|
2326 | 2330 | closeShards(indexShard);
|
|
0 commit comments