|
67 | 67 | import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
|
68 | 68 | import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
69 | 69 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
| 70 | +import org.elasticsearch.common.xcontent.XContentFactory; |
70 | 71 | import org.elasticsearch.common.xcontent.XContentType;
|
71 | 72 | import org.elasticsearch.env.NodeEnvironment;
|
72 | 73 | import org.elasticsearch.index.VersionType;
|
|
87 | 88 | import org.elasticsearch.index.seqno.SequenceNumbers;
|
88 | 89 | import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus;
|
89 | 90 | import org.elasticsearch.index.store.Store;
|
| 91 | +import org.elasticsearch.index.store.StoreStats; |
90 | 92 | import org.elasticsearch.index.translog.Translog;
|
91 | 93 | import org.elasticsearch.index.translog.TranslogTests;
|
92 | 94 | import org.elasticsearch.indices.IndicesQueryCache;
|
|
150 | 152 | import static org.hamcrest.Matchers.hasSize;
|
151 | 153 | import static org.hamcrest.Matchers.hasToString;
|
152 | 154 | import static org.hamcrest.Matchers.instanceOf;
|
| 155 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
153 | 156 | import static org.hamcrest.Matchers.notNullValue;
|
154 | 157 | import static org.hamcrest.Matchers.nullValue;
|
155 | 158 |
|
@@ -2227,6 +2230,7 @@ public void testDocStats() throws IOException {
|
2227 | 2230 | final DocsStats docsStats = indexShard.docStats();
|
2228 | 2231 | assertThat(docsStats.getCount(), equalTo(numDocs));
|
2229 | 2232 | assertThat(docsStats.getDeleted(), equalTo(0L));
|
| 2233 | + assertThat(docsStats.getAverageSizeInBytes(), greaterThan(0L)); |
2230 | 2234 | }
|
2231 | 2235 |
|
2232 | 2236 | final List<Integer> ids = randomSubsetOf(
|
@@ -2263,12 +2267,70 @@ public void testDocStats() throws IOException {
|
2263 | 2267 | final DocsStats docStats = indexShard.docStats();
|
2264 | 2268 | assertThat(docStats.getCount(), equalTo(numDocs));
|
2265 | 2269 | assertThat(docStats.getDeleted(), equalTo(0L));
|
| 2270 | + assertThat(docStats.getAverageSizeInBytes(), greaterThan(0L)); |
2266 | 2271 | }
|
2267 | 2272 | } finally {
|
2268 | 2273 | closeShards(indexShard);
|
2269 | 2274 | }
|
2270 | 2275 | }
|
2271 | 2276 |
|
| 2277 | + public void testEstimateTotalDocSize() throws Exception { |
| 2278 | + IndexShard indexShard = null; |
| 2279 | + try { |
| 2280 | + indexShard = newStartedShard(true); |
| 2281 | + |
| 2282 | + int numDoc = randomIntBetween(100, 200); |
| 2283 | + for (int i = 0; i < numDoc; i++) { |
| 2284 | + String doc = XContentFactory.jsonBuilder() |
| 2285 | + .startObject() |
| 2286 | + .field("count", randomInt()) |
| 2287 | + .field("point", randomFloat()) |
| 2288 | + .field("description", randomUnicodeOfCodepointLength(100)) |
| 2289 | + .endObject().string(); |
| 2290 | + indexDoc(indexShard, "doc", Integer.toString(i), doc); |
| 2291 | + } |
| 2292 | + |
| 2293 | + assertThat("Without flushing, segment sizes should be zero", |
| 2294 | + indexShard.docStats().getTotalSizeInBytes(), equalTo(0L)); |
| 2295 | + |
| 2296 | + indexShard.flush(new FlushRequest()); |
| 2297 | + indexShard.refresh("test"); |
| 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. |
| 2302 | + |
| 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)); |
| 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 | + |
| 2318 | + indexShard.flush(new FlushRequest()); |
| 2319 | + indexShard.refresh("test"); |
| 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 | + } |
| 2328 | + |
| 2329 | + } finally { |
| 2330 | + closeShards(indexShard); |
| 2331 | + } |
| 2332 | + } |
| 2333 | + |
2272 | 2334 | /**
|
2273 | 2335 | * here we are simulating the scenario that happens when we do async shard fetching from GatewaySerivce while we are finishing
|
2274 | 2336 | * a recovery and concurrently clean files. This should always be possible without any exception. Yet there was a bug where IndexShard
|
|
0 commit comments