Skip to content

Commit 50a9551

Browse files
committed
Additionally constrain requests >= operations
1 parent 0143979 commit 50a9551

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

server/src/main/java/org/elasticsearch/common/blobstore/BlobStoreActionStats.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public BlobStoreActionStats(StreamInput in) throws IOException {
3333

3434
public BlobStoreActionStats {
3535
assert operations >= 0 && requests >= 0;
36+
assert requests >= operations;
3637
}
3738

3839
@Override

server/src/test/java/org/elasticsearch/common/blobstore/BlobStoreActionStatsTests.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,37 @@
1414
public class BlobStoreActionStatsTests extends ESTestCase {
1515

1616
public void testAdd() {
17-
final BlobStoreActionStats lhs = randomEndpointStats(1 << 30);
18-
final BlobStoreActionStats rhs = randomEndpointStats(1 << 30);
17+
final BlobStoreActionStats lhs = randomBlobStoreActionStats(1 << 30);
18+
final BlobStoreActionStats rhs = randomBlobStoreActionStats(1 << 30);
1919
final BlobStoreActionStats result = lhs.add(rhs);
2020
assertEquals(lhs.operations() + rhs.operations(), result.operations());
2121
assertEquals(lhs.requests() + rhs.requests(), result.requests());
2222
}
2323

2424
public void testAddOverflow() {
25-
final BlobStoreActionStats lhs = new BlobStoreActionStats(randomLongBetween(50, 1 << 30), randomLongBetween(50, 1 << 30));
26-
final int fieldToOverflow = randomIntBetween(0, 1);
27-
final BlobStoreActionStats rhs = new BlobStoreActionStats(
28-
fieldToOverflow == 0 ? (Long.MAX_VALUE - lhs.operations()) + 1 : 1,
29-
fieldToOverflow == 1 ? (Long.MAX_VALUE - lhs.requests()) + 1 : 1
30-
);
25+
final BlobStoreActionStats lhs = randomBlobStoreActionStats(50, 1 << 30);
26+
// We can only overflow requests, or both values (we can't just overflow operations because requests >= operations
27+
final boolean overflowRequestsOnly = randomBoolean();
28+
final long valueToCauseOverflow = (Long.MAX_VALUE - lhs.operations()) + 1;
29+
final long operationsValue = overflowRequestsOnly ? 1 : valueToCauseOverflow;
30+
final BlobStoreActionStats rhs = new BlobStoreActionStats(operationsValue, valueToCauseOverflow);
3131
assertThrows(ArithmeticException.class, () -> lhs.add(rhs));
3232
}
3333

3434
public void testIsZero() {
3535
assertTrue(new BlobStoreActionStats(0, 0).isZero());
36-
assertFalse(new BlobStoreActionStats(randomLongBetween(1, Long.MAX_VALUE), 0).isZero());
3736
assertFalse(new BlobStoreActionStats(0, randomLongBetween(1, Long.MAX_VALUE)).isZero());
37+
assertFalse(randomBlobStoreActionStats(1, Long.MAX_VALUE).isZero());
3838
}
3939

40-
private BlobStoreActionStats randomEndpointStats() {
41-
return randomEndpointStats(Long.MAX_VALUE);
40+
private BlobStoreActionStats randomBlobStoreActionStats(long upperBound) {
41+
return randomBlobStoreActionStats(0, upperBound);
4242
}
4343

44-
private BlobStoreActionStats randomEndpointStats(long upperBound) {
45-
return new BlobStoreActionStats(randomLongBetween(0, upperBound), randomLongBetween(0, upperBound));
44+
private BlobStoreActionStats randomBlobStoreActionStats(long lowerBound, long upperBound) {
45+
assert upperBound >= lowerBound;
46+
long operations = randomLongBetween(lowerBound, upperBound);
47+
long requests = randomLongBetween(operations, upperBound);
48+
return new BlobStoreActionStats(operations, requests);
4649
}
4750
}

0 commit comments

Comments
 (0)