Skip to content

Commit 4cbbe3e

Browse files
committed
Fixes DocStats to not report index size < -1 (#27863)
Previously to this change when DocStats are added together (for example when adding the index size of all primary shards for an index) we naively added the `totalSizeInBytes` together. This worked most of the time but not when the index size on one or multiple shards was reported to be `-1` (no value). This change improves the logic by considering if the current value or the value to be added is `-1`: * If the current and new value are both `-1` the value remains at `-1` * If the current value is `-1` and the new value is not `-1`, current value is changed to be equal to the new value * If the current value is not `-1` and the new value is `-1` the new value is ignored and the current value is not changed * If both the current and new values are not `-1` the current value is changed to be equal to the sum of the current and new values. The change also re-enables the failing rollover YAML test that was failing due to this bug.
1 parent dd0cb11 commit 4cbbe3e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

core/src/main/java/org/elasticsearch/index/shard/DocsStats.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public void add(DocsStats other) {
4949
if (other == null) {
5050
return;
5151
}
52-
this.totalSizeInBytes += other.totalSizeInBytes;
52+
if (this.totalSizeInBytes == -1) {
53+
this.totalSizeInBytes = other.totalSizeInBytes;
54+
} else if (other.totalSizeInBytes != -1) {
55+
this.totalSizeInBytes += other.totalSizeInBytes;
56+
}
5357
this.count += other.count;
5458
this.deleted += other.deleted;
5559
}

core/src/test/java/org/elasticsearch/index/shard/DocsStatsTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ public void testCalculateAverageDocSize() throws Exception {
4141
assertThat(stats.getTotalSizeInBytes(), equalTo(600L));
4242
assertThat(stats.getAverageSizeInBytes(), equalTo(12L));
4343
}
44+
45+
public void testUninitialisedShards() {
46+
DocsStats stats = new DocsStats(0, 0, -1);
47+
assertThat(stats.getTotalSizeInBytes(), equalTo(-1L));
48+
assertThat(stats.getAverageSizeInBytes(), equalTo(0L));
49+
stats.add(new DocsStats(0, 0, -1));
50+
assertThat(stats.getTotalSizeInBytes(), equalTo(-1L));
51+
assertThat(stats.getAverageSizeInBytes(), equalTo(0L));
52+
stats.add(new DocsStats(1, 0, 10));
53+
assertThat(stats.getTotalSizeInBytes(), equalTo(10L));
54+
assertThat(stats.getAverageSizeInBytes(), equalTo(10L));
55+
stats.add(new DocsStats(0, 0, -1));
56+
assertThat(stats.getTotalSizeInBytes(), equalTo(10L));
57+
assertThat(stats.getAverageSizeInBytes(), equalTo(10L));
58+
stats.add(new DocsStats(1, 0, 20));
59+
assertThat(stats.getTotalSizeInBytes(), equalTo(30L));
60+
assertThat(stats.getAverageSizeInBytes(), equalTo(15L));
61+
}
4462

4563
public void testSerialize() throws Exception {
4664
DocsStats originalStats = new DocsStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong());

rest-api-spec/src/main/resources/rest-api-spec/test/indices.rollover/10_basic.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
22
"Rollover index via API":
3+
4+
35
# create index with alias
46
- do:
57
indices.create:

0 commit comments

Comments
 (0)