Search in sources :

Example 1 with ResourceCounter

use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.

the class BiDirectionalResourceToRequirementMapping method internalDecrementCount.

private static void internalDecrementCount(Map<ResourceProfile, ResourceCounter> primaryMap, ResourceProfile primaryKey, ResourceProfile secondaryKey, int decrement) {
    primaryMap.compute(primaryKey, (resourceProfile, resourceCounter) -> {
        Preconditions.checkState(resourceCounter != null, "Attempting to decrement count of %s->%s, but primary key was unknown.", resourceProfile, secondaryKey);
        final ResourceCounter newCounter = resourceCounter.subtract(secondaryKey, decrement);
        return newCounter.isEmpty() ? null : newCounter;
    });
}
Also used : ResourceCounter(org.apache.flink.runtime.util.ResourceCounter)

Example 2 with ResourceCounter

use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.

the class JobScopedResourceTracker method tryAssigningExcessSlots.

private void tryAssigningExcessSlots() {
    if (LOG.isTraceEnabled()) {
        LOG.trace("There are {} excess resources for job {} before re-assignment.", excessResources.getTotalResourceCount(), jobId);
    }
    ResourceCounter assignedResources = ResourceCounter.empty();
    for (Map.Entry<ResourceProfile, Integer> excessResource : excessResources.getResourcesWithCount()) {
        for (int i = 0; i < excessResource.getValue(); i++) {
            final ResourceProfile resourceProfile = excessResource.getKey();
            final Optional<ResourceProfile> matchingRequirement = findMatchingRequirement(resourceProfile);
            if (matchingRequirement.isPresent()) {
                resourceToRequirementMapping.incrementCount(matchingRequirement.get(), resourceProfile, 1);
                assignedResources = assignedResources.add(resourceProfile, 1);
            } else {
                break;
            }
        }
    }
    for (Map.Entry<ResourceProfile, Integer> assignedResource : assignedResources.getResourcesWithCount()) {
        excessResources = excessResources.subtract(assignedResource.getKey(), assignedResource.getValue());
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("There are {} excess resources for job {} after re-assignment.", excessResources.getTotalResourceCount(), jobId);
    }
}
Also used : ResourceProfile(org.apache.flink.runtime.clusterframework.types.ResourceProfile) ResourceCounter(org.apache.flink.runtime.util.ResourceCounter) Map(java.util.Map)

Example 3 with ResourceCounter

use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.

the class DefaultDeclarativeSlotPool method freeAndReleaseSlots.

private ResourceCounter freeAndReleaseSlots(Collection<AllocatedSlot> currentlyReservedSlots, Collection<AllocatedSlot> slots, Exception cause) {
    ResourceCounter previouslyFulfilledRequirements = getFulfilledRequirements(currentlyReservedSlots);
    releasePayload(currentlyReservedSlots, cause);
    releaseSlots(slots, cause);
    return previouslyFulfilledRequirements;
}
Also used : ResourceCounter(org.apache.flink.runtime.util.ResourceCounter)

Example 4 with ResourceCounter

use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.

the class DefaultDeclarativeSlotPool method releaseIdleSlots.

@Override
public void releaseIdleSlots(long currentTimeMillis) {
    final Collection<AllocatedSlotPool.FreeSlotInfo> freeSlotsInformation = slotPool.getFreeSlotsInformation();
    ResourceCounter excessResources = fulfilledResourceRequirements.subtract(totalResourceRequirements);
    final Iterator<AllocatedSlotPool.FreeSlotInfo> freeSlotIterator = freeSlotsInformation.iterator();
    final Collection<AllocatedSlot> slotsToReturnToOwner = new ArrayList<>();
    while (!excessResources.isEmpty() && freeSlotIterator.hasNext()) {
        final AllocatedSlotPool.FreeSlotInfo idleSlot = freeSlotIterator.next();
        if (currentTimeMillis >= idleSlot.getFreeSince() + idleSlotTimeout.toMilliseconds()) {
            final ResourceProfile matchingProfile = getMatchingResourceProfile(idleSlot.getAllocationId());
            if (excessResources.containsResource(matchingProfile)) {
                excessResources = excessResources.subtract(matchingProfile, 1);
                final Optional<AllocatedSlot> removedSlot = slotPool.removeSlot(idleSlot.getAllocationId());
                final AllocatedSlot allocatedSlot = removedSlot.orElseThrow(() -> new IllegalStateException(String.format("Could not find slot for allocation id %s.", idleSlot.getAllocationId())));
                slotsToReturnToOwner.add(allocatedSlot);
            }
        }
    }
    releaseSlots(slotsToReturnToOwner, new FlinkException("Returning idle slots to their owners."));
    LOG.debug("Idle slots have been returned; new total acquired resources: {}", fulfilledResourceRequirements);
}
Also used : ResourceProfile(org.apache.flink.runtime.clusterframework.types.ResourceProfile) ArrayList(java.util.ArrayList) ResourceCounter(org.apache.flink.runtime.util.ResourceCounter) FlinkException(org.apache.flink.util.FlinkException)

Example 5 with ResourceCounter

use of org.apache.flink.runtime.util.ResourceCounter in project flink by apache.

the class DefaultDeclarativeSlotPoolTest method testReleaseSlotOnlyReturnsFulfilledRequirementsOfReservedSlots.

@Test
public void testReleaseSlotOnlyReturnsFulfilledRequirementsOfReservedSlots() {
    withSlotPoolContainingOneTaskManagerWithTwoSlotsWithUniqueResourceProfiles((slotPool, freeSlot, slotToReserve, ignored) -> {
        slotPool.reserveFreeSlot(slotToReserve.getAllocationId(), slotToReserve.getResourceProfile()).tryAssignPayload(new TestingPhysicalSlotPayload());
        final ResourceCounter fulfilledRequirementsOfFreeSlot = slotPool.releaseSlot(freeSlot.getAllocationId(), new FlinkException("Test failure"));
        final ResourceCounter fulfilledRequirementsOfReservedSlot = slotPool.releaseSlot(slotToReserve.getAllocationId(), new FlinkException("Test failure"));
        assertThat(fulfilledRequirementsOfFreeSlot.getResources(), is(empty()));
        assertThat(fulfilledRequirementsOfReservedSlot.getResourceCount(slotToReserve.getResourceProfile()), is(1));
    });
}
Also used : ResourceCounter(org.apache.flink.runtime.util.ResourceCounter) FlinkException(org.apache.flink.util.FlinkException) Test(org.junit.Test)

Aggregations

ResourceCounter (org.apache.flink.runtime.util.ResourceCounter)39 Test (org.junit.Test)24 ResourceProfile (org.apache.flink.runtime.clusterframework.types.ResourceProfile)10 SlotOffer (org.apache.flink.runtime.taskexecutor.slot.SlotOffer)8 FlinkException (org.apache.flink.util.FlinkException)7 Map (java.util.Map)5 JobID (org.apache.flink.api.common.JobID)5 AllocationID (org.apache.flink.runtime.clusterframework.types.AllocationID)5 LocalTaskManagerLocation (org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation)5 ArrayList (java.util.ArrayList)4 TestingTaskExecutorGateway (org.apache.flink.runtime.taskexecutor.TestingTaskExecutorGateway)4 TestingTaskExecutorGatewayBuilder (org.apache.flink.runtime.taskexecutor.TestingTaskExecutorGatewayBuilder)4 HashMap (java.util.HashMap)3 Time (org.apache.flink.api.common.time.Time)3 ArchivedExecutionGraphTest (org.apache.flink.runtime.executiongraph.ArchivedExecutionGraphTest)3 DefaultSchedulerTest (org.apache.flink.runtime.scheduler.DefaultSchedulerTest)3 ResourceRequirement (org.apache.flink.runtime.slots.ResourceRequirement)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 InstanceID (org.apache.flink.runtime.instance.InstanceID)2 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)2