-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-3648; maxTimeToBlock in BufferPool.allocate should be enforced #1304
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
Changes from all commits
c63bcf5
f53018d
23a344d
7753f75
9d8c251
e1fc43f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| import org.apache.kafka.common.errors.TimeoutException; | ||
| import org.apache.kafka.common.metrics.Metrics; | ||
| import org.apache.kafka.common.utils.MockTime; | ||
| import org.apache.kafka.common.utils.SystemTime; | ||
| import org.apache.kafka.test.TestUtils; | ||
| import org.junit.After; | ||
| import org.junit.Test; | ||
|
|
@@ -27,17 +28,19 @@ | |
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.fail; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class BufferPoolTest { | ||
| private MockTime time = new MockTime(); | ||
| private Metrics metrics = new Metrics(time); | ||
| private final long maxBlockTimeMs = 2000; | ||
| String metricGroup = "TestMetrics"; | ||
| private final MockTime time = new MockTime(); | ||
| private final SystemTime systemTime = new SystemTime(); | ||
| private final Metrics metrics = new Metrics(time); | ||
| private final long maxBlockTimeMs = 2000; | ||
| private final String metricGroup = "TestMetrics"; | ||
|
|
||
| @After | ||
| public void teardown() { | ||
|
|
@@ -96,7 +99,7 @@ public void testDelayedAllocation() throws Exception { | |
| CountDownLatch allocation = asyncAllocate(pool, 5 * 1024); | ||
| assertEquals("Allocation shouldn't have happened yet, waiting on memory.", 1L, allocation.getCount()); | ||
| doDealloc.countDown(); // return the memory | ||
| allocation.await(); | ||
| assertTrue("Allocation should succeed soon after de-allocation", allocation.await(1, TimeUnit.SECONDS)); | ||
| } | ||
|
|
||
| private CountDownLatch asyncDeallocate(final BufferPool pool, final ByteBuffer buffer) { | ||
|
|
@@ -115,6 +118,16 @@ public void run() { | |
| return latch; | ||
| } | ||
|
|
||
| private void delayedDeallocate(final BufferPool pool, final ByteBuffer buffer, final long delayMs) { | ||
| Thread thread = new Thread() { | ||
| public void run() { | ||
| systemTime.sleep(delayMs); | ||
| pool.deallocate(buffer); | ||
| } | ||
| }; | ||
| thread.start(); | ||
| } | ||
|
|
||
| private CountDownLatch asyncAllocate(final BufferPool pool, final int size) { | ||
| final CountDownLatch completed = new CountDownLatch(1); | ||
| Thread thread = new Thread() { | ||
|
|
@@ -133,20 +146,32 @@ public void run() { | |
| } | ||
|
|
||
| /** | ||
| * Test if Timeout exception is thrown when there is not enough memory to allocate and the elapsed time is greater than the max specified block time | ||
| * Test if Timeout exception is thrown when there is not enough memory to allocate and the elapsed time is greater than the max specified block time. | ||
| * And verify that the allocation should finish soon after the maxBlockTimeMs. | ||
| * | ||
| * @throws Exception | ||
| */ | ||
| @Test | ||
| public void testBlockTimeout() throws Exception { | ||
| BufferPool pool = new BufferPool(2, 1, metrics, time, metricGroup); | ||
| pool.allocate(1, maxBlockTimeMs); | ||
| BufferPool pool = new BufferPool(10, 1, metrics, systemTime, metricGroup); | ||
| ByteBuffer buffer1 = pool.allocate(1, maxBlockTimeMs); | ||
| ByteBuffer buffer2 = pool.allocate(1, maxBlockTimeMs); | ||
| ByteBuffer buffer3 = pool.allocate(1, maxBlockTimeMs); | ||
| // First two buffers will be de-allocated within maxBlockTimeMs since the most recent de-allocation | ||
| delayedDeallocate(pool, buffer1, maxBlockTimeMs / 2); | ||
| delayedDeallocate(pool, buffer2, maxBlockTimeMs); | ||
| // The third buffer will be de-allocated after maxBlockTimeMs since the most recent de-allocation | ||
| delayedDeallocate(pool, buffer3, maxBlockTimeMs / 2 * 5); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding the comments. I would simplify the expressions in the third parameter, e.g.:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ijuma I can not do
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zhuchen1018 I guess you meant
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ijuma Yes I mean
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you going to update the other two to remove unnecessary operations? That's all that's left and then I can merge the PR. Thanks!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ijuma Oh, I thought you mean we can leave the all three as is so that the code style looks consistent. I will update it. |
||
|
|
||
| long beginTimeMs = systemTime.milliseconds(); | ||
| try { | ||
| pool.allocate(2, maxBlockTimeMs); | ||
| fail("The buffer allocated more memory than its maximum value 2"); | ||
| pool.allocate(10, maxBlockTimeMs); | ||
| fail("The buffer allocated more memory than its maximum value 10"); | ||
| } catch (TimeoutException e) { | ||
| // this is good | ||
| } | ||
| long endTimeMs = systemTime.milliseconds(); | ||
| assertTrue("Allocation should finish not much later than maxBlockTimeMs", endTimeMs - beginTimeMs < maxBlockTimeMs + 1000); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a comment explaining the reasoning for the third parameter values in the
delayedDeallocatecalls?