zhijiangW commented on a change in pull request #9993:
[FLINK-14498][runtime]Introduce NetworkBufferPool#isAvailable() for interacting
with LocalBufferPool.
URL: https://github.com/apache/flink/pull/9993#discussion_r339397642
##########
File path:
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/buffer/NetworkBufferPoolTest.java
##########
@@ -518,4 +528,176 @@ public void go() throws Exception {
globalPool.destroy();
}
}
+
+ /**
+ * Tests {@link NetworkBufferPool#isAvailable()}, verifying that the
buffer availability is
+ * correctly maintained and the future callback is correctly processed.
+ */
+ @Test
+ public void testBufferAvailabilityAndFutureCallback() throws Exception {
+ final int numBuffers = 10;
+ final int numberOfSegmentsToRequest = 5;
+ final Duration requestSegmentsTimeout = Duration.ofSeconds(10L);
+
+ final NetworkBufferPool globalPool = new NetworkBufferPool(
+ numBuffers,
+ 128,
+ numberOfSegmentsToRequest,
+ requestSegmentsTimeout);
+
+ try {
+ assertTrue(globalPool.isAvailable().isDone());
+
+ List<MemorySegment> segments = new
ArrayList<>(numberOfSegmentsToRequest);
+ for (int i = 0; i < numberOfSegmentsToRequest; ++i) {
+ MemorySegment segment =
globalPool.requestMemorySegment();
+ assertNotNull(segment);
+ segments.add(segment);
+ assertTrue(globalPool.isAvailable().isDone());
+ }
+
+ final List<MemorySegment> exclusiveSegments =
globalPool.requestMemorySegments();
+ assertEquals(numberOfSegmentsToRequest,
exclusiveSegments.size());
+
+ assertFalse(globalPool.isAvailable().isDone());
+ assertEquals(0,
globalPool.getNumberOfAvailableMemorySegments());
+
+ final AtomicBoolean completeFlag = new
AtomicBoolean(false);
+ CompletableFuture<?> availableFuture =
globalPool.isAvailable();
+ availableFuture.whenComplete((ignored, throwable) ->
completeFlag.set(true));
+
+ // recycle one buffer
+ globalPool.recycle(segments.get(0));
+ assertTrue(completeFlag.get());
+ assertTrue(availableFuture.isDone());
+ assertTrue(globalPool.isAvailable().isDone());
Review comment:
Even we could further remove below:
```
final AtomicBoolean completeFlag = new AtomicBoolean(false);
availableFuture.whenComplete((ignored, throwable) -> completeFlag.set(true));
assertTrue(completeFlag.get());
```
Before recycling, the global pool is not available, after recycling it
becomes available via `assertTrue(globalPool.isAvailable().isDone())`. It is
already enough to verify our logic changes. It is useless to verify the logic
action (`completeFlag.set(true))`) when future is completed, because in real
code path the action is actually different.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services