xintongsong commented on a change in pull request #9693: [FLINK-13984] Separate 
on-heap and off-heap managed memory pools
URL: https://github.com/apache/flink/pull/9693#discussion_r327940329
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/memory/KeyedBudgetTest.java
 ##########
 @@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.runtime.memory;
+
+import org.apache.flink.types.Either;
+import org.apache.flink.util.Preconditions;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.LongStream;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * Test suite for {@link KeyedBudget}.
+ */
+@SuppressWarnings("MagicNumber")
+public class KeyedBudgetTest {
+       private static final String[] TEST_KEYS = {"k1", "k2", "k3", "k4"};
+       private static final long[] TEST_BUDGETS = {15, 17, 22, 11};
+
+       @Test
+       public void testSuccessfulAcquisitionForKey() {
+               KeyedBudget<String> keyedBudget = createSimpleKeyedBudget();
+
+               long acquired = keyedBudget.acquireBudgetForKey("k1", 10L);
+
+               assertThat(acquired, is(10L));
+               checkOneKeyBudgetChange(keyedBudget, "k1", 5L);
+       }
+
+       @Test
+       public void testFailedAcquisitionForKey() {
+               KeyedBudget<String> keyedBudget = createSimpleKeyedBudget();
+
+               long acquired = keyedBudget.acquireBudgetForKey("k1", 20L);
+
+               assertThat(acquired, is(15L));
+               checkOneKeyBudgetChange(keyedBudget, "k1", 15L);
+       }
+
+       @Test
+       public void testSuccessfulReleaseForKey() {
+               KeyedBudget<String> keyedBudget = createSimpleKeyedBudget();
+
+               keyedBudget.acquireBudgetForKey("k1", 10L);
+               keyedBudget.releaseBudgetForKey("k1", 5L);
+
+               checkOneKeyBudgetChange(keyedBudget, "k1", 10L);
+       }
+
+       @Test
+       public void testFailedReleaseForKey() {
+               KeyedBudget<String> keyedBudget = createSimpleKeyedBudget();
+
+               keyedBudget.acquireBudgetForKey("k1", 10L);
+               try {
+                       keyedBudget.releaseBudgetForKey("k1", 15L);
+                       fail("IllegalStateException is expected to fail 
over-sized release");
+               } catch (IllegalStateException e) {
+                       // expected
+               }
+
+               checkOneKeyBudgetChange(keyedBudget, "k1", 5L);
+       }
+
+       @Test
+       public void testSuccessfulAcquisitionForKeys() {
+               KeyedBudget<String> keyedBudget = createSimpleKeyedBudget();
+
+               Either<Map<String, Long>, Long> acquired =
+                       
keyedBudget.acquirePagedBudgetForKeys(Arrays.asList("k2", "k3"), 4, 5);
+
+               assertThat(acquired.isLeft(), is(true));
+               assertThat(acquired.left().values().stream().mapToLong(b -> 
b).sum(), is(4L));
+
+               assertThat(keyedBudget.availableBudgetForKey("k1"), is(15L));
+               
assertThat(keyedBudget.availableBudgetForKeys(Arrays.asList("k2", "k3")), 
is(19L));
+               assertThat(keyedBudget.totalAvailableBudget(), is(45L));
+       }
+
+       @Test
+       public void testSuccessfulReleaseForKeys() {
+               KeyedBudget<String> keyedBudget = createSimpleKeyedBudget();
+
+               keyedBudget.acquirePagedBudgetForKeys(Arrays.asList("k2", 
"k3"), 4, 8);
+               keyedBudget.releaseBudgetForKeys(createdBudgetMap(new String[] 
{"k2", "k3"}, new long[] {7, 10}));
+
+               
assertThat(keyedBudget.availableBudgetForKeys(Arrays.asList("k2", "k3")), 
is(24L));
+               
assertThat(keyedBudget.availableBudgetForKeys(Arrays.asList("k1", "k4")), 
is(26L));
+               assertThat(keyedBudget.totalAvailableBudget(), is(50L));
+       }
+
+       @Test
+       public void testSuccessfulReleaseForKeysWithMixedRequests() {
+               KeyedBudget<String> keyedBudget = createSimpleKeyedBudget();
+
+               keyedBudget.acquirePagedBudgetForKeys(Arrays.asList("k2", 
"k3"), 4, 8);
+               keyedBudget.acquirePagedBudgetForKeys(Arrays.asList("k1", 
"k4"), 6, 3);
+               keyedBudget.releaseBudgetForKeys(createdBudgetMap(new String[] 
{"k2", "k3"}, new long[] {7, 10}));
+
+               
assertThat(keyedBudget.availableBudgetForKeys(Arrays.asList("k2", "k3")), 
is(24L));
+               
assertThat(keyedBudget.availableBudgetForKeys(Arrays.asList("k1", "k4")), 
is(8L));
+               assertThat(keyedBudget.totalAvailableBudget(), is(32L));
+       }
+
 
 Review comment:
   It seems to me that the following behaviors are not covered with current 
test cases:
   - acquirePagedBudget / acquireBudgetForKey / acquirePagedBudgetForKeys 
returns max possible budget / pages when failed
   - for each key only integer times of pagesize can be acquired with 
acquirePagedBudget / acquirePagedBudgetForKeys. E.g., when acquiring 2 pages 
with page size 5 from key A (available 7) and key B (available 3), it should 
fail even the total requested budget is no larger than the total available 
budget of A and B.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to