You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/10/13 08:34:26 UTC

[GitHub] [flink] zentol commented on a change in pull request #13561: [FLINK-19315][coordination] Add AllocatedSlotPool

zentol commented on a change in pull request #13561:
URL: https://github.com/apache/flink/pull/13561#discussion_r503767564



##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/slotpool/DefaultAllocatedSlotPoolTest.java
##########
@@ -0,0 +1,315 @@
+/*
+ * 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.jobmaster.slotpool;
+
+import org.apache.flink.runtime.clusterframework.types.AllocationID;
+import org.apache.flink.runtime.clusterframework.types.ResourceID;
+import org.apache.flink.runtime.clusterframework.types.ResourceProfile;
+import org.apache.flink.runtime.jobmaster.JobMasterId;
+import org.apache.flink.runtime.jobmaster.RpcTaskManagerGateway;
+import org.apache.flink.runtime.jobmaster.SlotInfo;
+import org.apache.flink.runtime.taskexecutor.TestingTaskExecutorGatewayBuilder;
+import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation;
+import org.apache.flink.runtime.taskmanager.TaskManagerLocation;
+import org.apache.flink.util.TestLogger;
+
+import org.apache.flink.shaded.guava18.com.google.common.collect.Iterables;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+import org.junit.Test;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.closeTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.sameInstance;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for the {@link DefaultAllocatedSlotPool}.
+ */
+public class DefaultAllocatedSlotPoolTest extends TestLogger {
+
+	@Test
+	public void testAddSlots() {
+		final DefaultAllocatedSlotPool slotPool = new DefaultAllocatedSlotPool();
+
+		final Collection<AllocatedSlot> slots = createAllocatedSlots();
+
+		slotPool.addSlots(slots, 0);
+
+		assertSlotPoolContainsSlots(slotPool, slots);
+		assertSlotPoolContainsFreeSlots(slotPool, slots);
+	}
+
+	@Test
+	public void testRemoveSlot() {
+		final DefaultAllocatedSlotPool slotPool = new DefaultAllocatedSlotPool();
+
+		final Collection<AllocatedSlot> slots = createAllocatedSlots();
+
+		slotPool.addSlots(slots, 0);
+
+		final Iterator<AllocatedSlot> iterator = slots.iterator();
+		final AllocatedSlot removedSlot = iterator.next();
+		iterator.remove();
+
+		slotPool.removeSlot(removedSlot.getAllocationId());
+
+		assertSlotPoolContainsSlots(slotPool, slots);
+	}
+
+	@Test
+	public void testRemoveSlots() {
+		final DefaultAllocatedSlotPool slotPool = new DefaultAllocatedSlotPool();
+
+		final ResourceID owner = ResourceID.generate();
+		final Collection<AllocatedSlot> slots = createAllocatedSlotsWithOwner(owner);
+		final AllocatedSlot otherSlot = createAllocatedSlot(ResourceID.generate());
+		slots.add(otherSlot);
+
+		slotPool.addSlots(slots, 0);
+
+		slotPool.removeSlots(owner);
+
+		assertSlotPoolContainsSlots(slotPool, Collections.singleton(otherSlot));
+	}
+
+	@Test
+	public void testContainsSlots() {
+		final DefaultAllocatedSlotPool slotPool = new DefaultAllocatedSlotPool();
+		final ResourceID owner = ResourceID.generate();
+		final AllocatedSlot allocatedSlot = createAllocatedSlot(owner);
+
+		slotPool.addSlots(Collections.singleton(allocatedSlot), 0);
+
+		assertTrue(slotPool.containsSlots(owner));
+		assertFalse(slotPool.containsSlots(ResourceID.generate()));
+	}
+
+	@Test
+	public void testContainsSlot() {
+		final DefaultAllocatedSlotPool slotPool = new DefaultAllocatedSlotPool();
+		final AllocatedSlot allocatedSlot = createAllocatedSlot(null);
+
+		slotPool.addSlots(Collections.singleton(allocatedSlot), 0);
+
+		assertTrue(slotPool.containsSlot(allocatedSlot.getAllocationId()));
+		assertFalse(slotPool.containsSlot(new AllocationID()));
+	}
+
+	@Test
+	public void testReserveFreeSlot() {
+		final DefaultAllocatedSlotPool slotPool = new DefaultAllocatedSlotPool();
+		final Collection<AllocatedSlot> allSlots = createAllocatedSlots();
+		final Collection<AllocatedSlot> freeSlots = new ArrayList<>(allSlots);
+		final Iterator<AllocatedSlot> iterator = freeSlots.iterator();
+		final AllocatedSlot allocatedSlot = iterator.next();
+		iterator.remove();
+
+		slotPool.addSlots(allSlots, 0);
+
+		assertThat(slotPool.reserveFreeSlot(allocatedSlot.getAllocationId()), sameInstance(allocatedSlot));
+
+		assertSlotPoolContainsFreeSlots(slotPool, freeSlots);
+		assertSlotPoolContainsSlots(slotPool, allSlots);
+	}
+
+	@Test(expected = IllegalStateException.class)
+	public void testReserveNonFreeSlotFails() {
+		final DefaultAllocatedSlotPool slotPool = new DefaultAllocatedSlotPool();
+		final AllocatedSlot slot = createAllocatedSlot(null);
+
+		slotPool.addSlots(Collections.singleton(slot), 0);
+
+		slotPool.reserveFreeSlot(slot.getAllocationId());
+		slotPool.reserveFreeSlot(slot.getAllocationId());
+	}
+
+	@Test
+	public void testFreeReservedSlot() {
+		final DefaultAllocatedSlotPool slotPool = new DefaultAllocatedSlotPool();
+		final Collection<AllocatedSlot> slots = createAllocatedSlots();
+
+		final int initialTime = 0;
+		slotPool.addSlots(slots, initialTime);
+
+		final AllocatedSlot slot = slots.iterator().next();
+
+		slotPool.reserveFreeSlot(slot.getAllocationId());
+
+		final int releaseTime = 1;
+		assertTrue(slotPool.freeReservedSlot(slot.getAllocationId(), releaseTime).isPresent());
+		assertSlotPoolContainsFreeSlots(slotPool, slots);
+
+		for (AllocatedSlotPool.FreeSlotInfo freeSlotInfo : slotPool.getFreeSlotsInformation()) {
+			final long time;
+			if (freeSlotInfo.getAllocationId().equals(slot.getAllocationId())) {
+				time = releaseTime;
+			} else {
+				time = initialTime;
+			}
+
+			assertThat(freeSlotInfo.getFreeSince(), is(time));
+		}
+	}
+
+	@Test
+	public void testFreeFreeSlotIsIgnored() {

Review comment:
       This test checks that freeing a free slot does not cause any side-effects, so the proposed name would be wrong. The method name is indeed a bit awkward; an alternative might be `testFreeingFreeSlotIsIgnored`.




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