diff --git a/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/BlobCacheUtils.java b/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/BlobCacheUtils.java index 940578d3dafb2..ff273d99d3c41 100644 --- a/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/BlobCacheUtils.java +++ b/x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/BlobCacheUtils.java @@ -36,7 +36,7 @@ public static int toIntBytes(long l) { * Rounds the length up so that it is aligned on the next page size (defined by SharedBytes.PAGE_SIZE). For example */ public static long toPageAlignedSize(long length) { - int remainder = (int) length % SharedBytes.PAGE_SIZE; + int remainder = (int) (length % SharedBytes.PAGE_SIZE); if (remainder > 0L) { return length + (SharedBytes.PAGE_SIZE - remainder); } diff --git a/x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/BlobCacheUtilsTests.java b/x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/BlobCacheUtilsTests.java index 2f78797e556ac..13770fadfcc28 100644 --- a/x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/BlobCacheUtilsTests.java +++ b/x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/BlobCacheUtilsTests.java @@ -6,8 +6,10 @@ */ package org.elasticsearch.blobcache; +import org.elasticsearch.blobcache.shared.SharedBytes; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.test.ESTestCase; +import org.hamcrest.Matchers; import java.io.EOFException; import java.nio.ByteBuffer; @@ -19,4 +21,10 @@ public void testReadSafeThrows() { final int remaining = randomIntBetween(1, 1025); expectThrows(EOFException.class, () -> BlobCacheUtils.readSafe(BytesArray.EMPTY.streamInput(), buffer, 0, remaining)); } + + public void testToPageAlignedSize() { + long value = randomLongBetween(0, Long.MAX_VALUE / 2); + long expected = ((value - 1) / SharedBytes.PAGE_SIZE + 1) * SharedBytes.PAGE_SIZE; + assertThat(BlobCacheUtils.toPageAlignedSize(value), Matchers.equalTo(expected)); + } }