use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.
the class DefaultDeclarativeSlotPool method getFulfilledRequirements.
private ResourceCounter getFulfilledRequirements(Iterable<? extends AllocatedSlot> allocatedSlots) {
ResourceCounter resourceDecrement = ResourceCounter.empty();
for (AllocatedSlot allocatedSlot : allocatedSlots) {
final ResourceProfile matchingResourceProfile = getMatchingResourceProfile(allocatedSlot.getAllocationId());
resourceDecrement = resourceDecrement.add(matchingResourceProfile, 1);
}
return resourceDecrement;
}
use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.
the class SlotPoolUtils method calculateResourceCounter.
static ResourceCounter calculateResourceCounter(ResourceProfile[] resourceProfiles) {
final Map<ResourceProfile, Integer> resources = Arrays.stream(resourceProfiles).collect(Collectors.groupingBy(Function.identity(), reducing(0, e -> 1, Integer::sum)));
final ResourceCounter increment = ResourceCounter.withResources(resources);
return increment;
}
use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.
the class DefaultDeclarativeSlotPoolTest method testReturnIdleSlotsAfterTimeout.
@Test
public void testReturnIdleSlotsAfterTimeout() {
final Time idleSlotTimeout = Time.seconds(10);
final long offerTime = 0;
final DefaultDeclarativeSlotPool slotPool = DefaultDeclarativeSlotPoolBuilder.builder().setIdleSlotTimeout(idleSlotTimeout).build();
final ResourceCounter resourceRequirements = createResourceRequirements();
final FreeSlotConsumer freeSlotConsumer = new FreeSlotConsumer();
final TestingTaskExecutorGateway testingTaskExecutorGateway = new TestingTaskExecutorGatewayBuilder().setFreeSlotFunction(freeSlotConsumer).createTestingTaskExecutorGateway();
final Collection<SlotOffer> acceptedSlots = increaseRequirementsAndOfferSlotsToSlotPool(slotPool, resourceRequirements, new LocalTaskManagerLocation(), testingTaskExecutorGateway);
// decrease the resource requirements so that slots are no longer needed
slotPool.decreaseResourceRequirementsBy(resourceRequirements);
slotPool.releaseIdleSlots(offerTime + idleSlotTimeout.toMilliseconds());
final Collection<AllocationID> freedSlots = freeSlotConsumer.drainFreedSlots();
assertThat(acceptedSlots, is(not(empty())));
assertThat(freedSlots, containsInAnyOrder(acceptedSlots.stream().map(SlotOffer::getAllocationId).toArray()));
assertNoAvailableAndRequiredResources(slotPool);
}
use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.
the class DefaultDeclarativeSlotPoolTest method testOfferSlots.
@Test
public void testOfferSlots() throws InterruptedException {
final NewSlotsService notifyNewSlots = new NewSlotsService();
final DefaultDeclarativeSlotPool slotPool = createDefaultDeclarativeSlotPoolWithNewSlotsListener(notifyNewSlots);
final ResourceCounter resourceRequirements = createResourceRequirements();
slotPool.increaseResourceRequirementsBy(resourceRequirements);
Collection<SlotOffer> slotOffers = createSlotOffersForResourceRequirements(resourceRequirements);
final Collection<SlotOffer> acceptedSlots = SlotPoolTestUtils.offerSlots(slotPool, slotOffers);
assertThat(acceptedSlots, containsInAnyOrder(slotOffers.toArray()));
final Collection<PhysicalSlot> newSlots = drainNewSlotService(notifyNewSlots);
assertThat(newSlots, containsInAnyOrder(slotOffers.stream().map(DefaultDeclarativeSlotPoolTest::matchesSlotOffer).collect(Collectors.toList())));
assertThat(slotPool.getAllSlotsInformation(), containsInAnyOrder(newSlots.stream().map(DefaultAllocatedSlotPoolTest::matchesPhysicalSlot).collect(Collectors.toList())));
}
use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.
the class DefaultDeclarativeSlotPoolTest method testReleaseSlotsOnlyReturnsFulfilledRequirementsOfReservedSlots.
@Test
public void testReleaseSlotsOnlyReturnsFulfilledRequirementsOfReservedSlots() {
withSlotPoolContainingOneTaskManagerWithTwoSlotsWithUniqueResourceProfiles((slotPool, freeSlot, slotToReserve, taskManagerLocation) -> {
slotPool.reserveFreeSlot(slotToReserve.getAllocationId(), slotToReserve.getResourceProfile()).tryAssignPayload(new TestingPhysicalSlotPayload());
final ResourceCounter fulfilledRequirements = slotPool.releaseSlots(taskManagerLocation.getResourceID(), new FlinkException("Test failure"));
assertThat(fulfilledRequirements.getResourceCount(freeSlot.getResourceProfile()), is(0));
assertThat(fulfilledRequirements.getResourceCount(slotToReserve.getResourceProfile()), is(1));
});
}
Aggregations