Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle overflow for MutableOffHeapByteArrayStore buffer starting size #13215

Merged
merged 3 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,20 @@ public void close()
private final int _startSize;

@VisibleForTesting
public int getStartSize() {
return _startSize;
public static int getStartSize(int numArrays, int avgArrayLen) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made static to avoid initializing huge buffers in unit tests

// For each array, we store the array and its startoffset (4 bytes)
long estimatedSize = numArrays * ((long) avgArrayLen + 4);
if (estimatedSize > 0 && estimatedSize <= Integer.MAX_VALUE) {
return (int) estimatedSize;
}
return Integer.MAX_VALUE;
}

public MutableOffHeapByteArrayStore(PinotDataBufferMemoryManager memoryManager, String allocationContext,
int numArrays, int avgArrayLen) {
_memoryManager = memoryManager;
_allocationContext = allocationContext;
_startSize = numArrays * (avgArrayLen + 4); // For each array, we store the array and its startoffset (4 bytes)
_startSize = getStartSize(numArrays, avgArrayLen);
expand(_startSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class MutableOffHeapByteArrayStoreTest {

private PinotDataBufferMemoryManager _memoryManager;
private static final int ONE_GB = 1024 * 1024 * 1024;

@BeforeClass
public void setUp() {
Expand All @@ -44,8 +45,11 @@ public void tearDown()
@Test
public void maxValueTest()
throws Exception {
MutableOffHeapByteArrayStore store = new MutableOffHeapByteArrayStore(_memoryManager, "stringColumn", 1024, 32);
final int arrSize = store.getStartSize();
int numArrays = 1024;
int avgArrayLen = 32;
MutableOffHeapByteArrayStore store =
new MutableOffHeapByteArrayStore(_memoryManager, "stringColumn", numArrays, avgArrayLen);
final int arrSize = MutableOffHeapByteArrayStore.getStartSize(numArrays, avgArrayLen);
byte[] dataIn = new byte[arrSize - 4];
for (int i = 0; i < dataIn.length; i++) {
dataIn[i] = (byte) (i % Byte.MAX_VALUE);
Expand All @@ -56,11 +60,21 @@ public void maxValueTest()
store.close();
}

@Test
public void startSizeTest() {
Assert.assertEquals(MutableOffHeapByteArrayStore.getStartSize(1, ONE_GB), ONE_GB + 4);
Assert.assertEquals(MutableOffHeapByteArrayStore.getStartSize(3, ONE_GB), Integer.MAX_VALUE);
Assert.assertEquals(MutableOffHeapByteArrayStore.getStartSize(5, ONE_GB), Integer.MAX_VALUE);
}

@Test
public void overflowTest()
throws Exception {
MutableOffHeapByteArrayStore store = new MutableOffHeapByteArrayStore(_memoryManager, "stringColumn", 1024, 32);
final int maxSize = store.getStartSize() - 4;
int numArrays = 1024;
int avgArrayLen = 32;
MutableOffHeapByteArrayStore store =
new MutableOffHeapByteArrayStore(_memoryManager, "stringColumn", numArrays, avgArrayLen);
final int maxSize = MutableOffHeapByteArrayStore.getStartSize(numArrays, avgArrayLen) - 4;

byte[] b1 = new byte[3];
for (int i = 0; i < b1.length; i++) {
Expand Down
Loading