org.apache.flink.runtime.clusterframework.types.SlotID Java Examples

The following examples show how to use org.apache.flink.runtime.clusterframework.types.SlotID. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaskExecutorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private void requestSlots(TaskExecutorGateway tmGateway, Iterable<? extends AllocationID> allocationIds, ResourceManagerId resourceManagerId, String jobMasterGatewayAddress) {
	int slotIndex = 0;
	for (AllocationID allocationId : allocationIds) {
		final SlotID slotId1 = new SlotID(unresolvedTaskManagerLocation.getResourceID(), slotIndex);
		tmGateway.requestSlot(
			slotId1,
			jobId,
			allocationId,
			ResourceProfile.UNKNOWN,
			jobMasterGatewayAddress,
			resourceManagerId,
			Time.seconds(10L)).join();

		slotIndex++;
	}
}
 
Example #2
Source File: SlotManagerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that if we have received a slot report with some allocated slots, then we don't accept
 * slot requests with allocated allocation ids.
 */
@Test
public void testDuplicatePendingSlotRequestAfterSlotReport() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceActions resourceManagerActions = mock(ResourceActions.class);
	final JobID jobId = new JobID();
	final AllocationID allocationId = new AllocationID();
	final ResourceProfile resourceProfile = new ResourceProfile(1.0, 1);
	final ResourceID resourceID = ResourceID.generate();
	final SlotID slotId = new SlotID(resourceID, 0);

	final TaskExecutorGateway taskExecutorGateway = mock(TaskExecutorGateway.class);
	final TaskExecutorConnection taskManagerConnection = new TaskExecutorConnection(resourceID, taskExecutorGateway);

	final SlotStatus slotStatus = new SlotStatus(slotId, resourceProfile, jobId, allocationId);
	final SlotReport slotReport = new SlotReport(slotStatus);

	final SlotRequest slotRequest = new SlotRequest(jobId, allocationId, resourceProfile, "foobar");

	try (SlotManager slotManager = createSlotManager(resourceManagerId, resourceManagerActions)) {
		slotManager.registerTaskManager(taskManagerConnection, slotReport);

		assertFalse(slotManager.registerSlotRequest(slotRequest));
	}
}
 
Example #3
Source File: SlotManagerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the slot manager ignores slot reports of unknown origin (not registered
 * task managers).
 */
@Test
public void testReceivingUnknownSlotReport() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceActions resourceManagerActions = mock(ResourceActions.class);

	final InstanceID unknownInstanceID = new InstanceID();
	final SlotID unknownSlotId = new SlotID(ResourceID.generate(), 0);
	final ResourceProfile unknownResourceProfile = new ResourceProfile(1.0, 1);
	final SlotStatus unknownSlotStatus = new SlotStatus(unknownSlotId, unknownResourceProfile);
	final SlotReport unknownSlotReport = new SlotReport(unknownSlotStatus);

	try (SlotManager slotManager = createSlotManager(resourceManagerId, resourceManagerActions)) {
		// check that we don't have any slots registered
		assertTrue(0 == slotManager.getNumberRegisteredSlots());

		// this should not update anything since the instance id is not known to the slot manager
		assertFalse(slotManager.reportSlotStatus(unknownInstanceID, unknownSlotReport));

		assertTrue(0 == slotManager.getNumberRegisteredSlots());
	}
}
 
Example #4
Source File: SlotManagerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the slot manager ignores slot reports of unknown origin (not registered
 * task managers).
 */
@Test
public void testReceivingUnknownSlotReport() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceActions resourceManagerActions = mock(ResourceActions.class);

	final InstanceID unknownInstanceID = new InstanceID();
	final SlotID unknownSlotId = new SlotID(ResourceID.generate(), 0);
	final ResourceProfile unknownResourceProfile = new ResourceProfile(1.0, 1);
	final SlotStatus unknownSlotStatus = new SlotStatus(unknownSlotId, unknownResourceProfile);
	final SlotReport unknownSlotReport = new SlotReport(unknownSlotStatus);

	try (SlotManager slotManager = createSlotManager(resourceManagerId, resourceManagerActions)) {
		// check that we don't have any slots registered
		assertTrue(0 == slotManager.getNumberRegisteredSlots());

		// this should not update anything since the instance id is not known to the slot manager
		assertFalse(slotManager.reportSlotStatus(unknownInstanceID, unknownSlotReport));

		assertTrue(0 == slotManager.getNumberRegisteredSlots());
	}
}
 
Example #5
Source File: SlotManager.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a failed slot request. The slot manager tries to find a new slot fulfilling
 * the resource requirements for the failed slot request.
 *
 * @param slotId identifying the slot which was assigned to the slot request before
 * @param allocationId identifying the failed slot request
 * @param cause of the failure
 */
private void handleFailedSlotRequest(SlotID slotId, AllocationID allocationId, Throwable cause) {
	PendingSlotRequest pendingSlotRequest = pendingSlotRequests.get(allocationId);

	LOG.debug("Slot request with allocation id {} failed for slot {}.", allocationId, slotId, cause);

	if (null != pendingSlotRequest) {
		pendingSlotRequest.setRequestFuture(null);

		try {
			internalRequestSlot(pendingSlotRequest);
		} catch (ResourceManagerException e) {
			pendingSlotRequests.remove(allocationId);

			resourceActions.notifyAllocationFailure(
				pendingSlotRequest.getJobId(),
				allocationId,
				e);
		}
	} else {
		LOG.debug("There was not pending slot request with allocation id {}. Probably the request has been fulfilled or cancelled.", allocationId);
	}
}
 
Example #6
Source File: SlotManager.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a pending slot request identified by the given allocation id from a slot identified
 * by the given slot id.
 *
 * @param slotId identifying the slot
 * @param allocationId identifying the presumable assigned pending slot request
 */
private void removeSlotRequestFromSlot(SlotID slotId, AllocationID allocationId) {
	TaskManagerSlot taskManagerSlot = slots.get(slotId);

	if (null != taskManagerSlot) {
		if (taskManagerSlot.getState() == TaskManagerSlot.State.PENDING && Objects.equals(allocationId, taskManagerSlot.getAssignedSlotRequest().getAllocationId())) {

			TaskManagerRegistration taskManagerRegistration = taskManagerRegistrations.get(taskManagerSlot.getInstanceId());

			if (taskManagerRegistration == null) {
				throw new IllegalStateException("Trying to remove slot request from slot for which there is no TaskManager " + taskManagerSlot.getInstanceId() + " is registered.");
			}

			// clear the pending slot request
			taskManagerSlot.clearPendingSlotRequest();

			updateSlotState(taskManagerSlot, taskManagerRegistration, null, null);
		} else {
			LOG.debug("Ignore slot request removal for slot {}.", slotId);
		}
	} else {
		LOG.debug("There was no slot with {} registered. Probably this slot has been already freed.", slotId);
	}
}
 
Example #7
Source File: SlotManagerImplTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that only free slots can fulfill/complete a pending task manager slot.
 */
@Test
public void testOnlyFreeSlotsCanFulfillPendingTaskManagerSlot() throws Exception {
	final int numberSlots = 1;
	final TestingResourceActions resourceActions = new TestingResourceActionsBuilder().build();

	try (final SlotManagerImpl slotManager = createSlotManager(ResourceManagerId.generate(), resourceActions, numberSlots)) {
		final JobID jobId = new JobID();
		assertThat(slotManager.registerSlotRequest(createSlotRequest(jobId)), is(true));

		final TaskExecutorConnection taskExecutorConnection = createTaskExecutorConnection();
		final SlotID slotId = new SlotID(taskExecutorConnection.getResourceID(), 0);
		final SlotStatus slotStatus = new SlotStatus(slotId, ResourceProfile.ANY, jobId, new AllocationID());
		final SlotReport slotReport = new SlotReport(slotStatus);

		slotManager.registerTaskManager(taskExecutorConnection, slotReport);

		assertThat(slotManager.getNumberRegisteredSlots(), is(1));
		assertThat(slotManager.getNumberPendingTaskManagerSlots(), is(numberSlots));
		assertThat(slotManager.getNumberAssignedPendingTaskManagerSlots(), is(1));
	}
}
 
Example #8
Source File: SlotManagerImplTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that if we have received a slot report with some allocated slots, then we don't accept
 * slot requests with allocated allocation ids.
 */
@Test
public void testDuplicatePendingSlotRequestAfterSlotReport() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceActions resourceManagerActions = new TestingResourceActionsBuilder().build();

	final TaskExecutorConnection taskManagerConnection = createTaskExecutorConnection();
	final ResourceID resourceID = taskManagerConnection.getResourceID();

	final JobID jobId = new JobID();
	final AllocationID allocationId = new AllocationID();
	final ResourceProfile resourceProfile = ResourceProfile.fromResources(1.0, 1);
	final SlotID slotId = new SlotID(resourceID, 0);
	final SlotStatus slotStatus = new SlotStatus(slotId, resourceProfile, jobId, allocationId);
	final SlotReport slotReport = new SlotReport(slotStatus);

	final SlotRequest slotRequest = new SlotRequest(jobId, allocationId, resourceProfile, "foobar");

	try (SlotManager slotManager = createSlotManager(resourceManagerId, resourceManagerActions)) {
		slotManager.registerTaskManager(taskManagerConnection, slotReport);

		assertFalse(slotManager.registerSlotRequest(slotRequest));
	}
}
 
Example #9
Source File: TaskExecutor.java    From flink with Apache License 2.0 6 votes vote down vote up
private void allocateSlot(
		SlotID slotId,
		JobID jobId,
		AllocationID allocationId,
		ResourceProfile resourceProfile) throws SlotAllocationException {
	if (taskSlotTable.isSlotFree(slotId.getSlotNumber())) {
		if (taskSlotTable.allocateSlot(slotId.getSlotNumber(), jobId, allocationId, resourceProfile, taskManagerConfiguration.getTimeout())) {
			log.info("Allocated slot for {}.", allocationId);
		} else {
			log.info("Could not allocate slot for {}.", allocationId);
			throw new SlotAllocationException("Could not allocate slot.");
		}
	} else if (!taskSlotTable.isAllocated(slotId.getSlotNumber(), jobId, allocationId)) {
		final String message = "The slot " + slotId + " has already been allocated for a different job.";

		log.info(message);

		final AllocationID allocationID = taskSlotTable.getCurrentAllocation(slotId.getSlotNumber());
		throw new SlotOccupiedException(message, allocationID, taskSlotTable.getOwningJob(allocationID));
	}
}
 
Example #10
Source File: SlotManagerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that only free slots can fulfill/complete a pending task manager slot.
 */
@Test
public void testOnlyFreeSlotsCanFulfillPendingTaskManagerSlot() throws Exception {
	final int numberSlots = 1;
	final TestingResourceActions resourceActions = new TestingResourceActionsBuilder()
		.setAllocateResourceFunction(convert(value -> numberSlots))
		.build();

	try (final SlotManager slotManager = createSlotManager(ResourceManagerId.generate(), resourceActions)) {
		final JobID jobId = new JobID();
		assertThat(slotManager.registerSlotRequest(createSlotRequest(jobId)), is(true));

		final TaskExecutorConnection taskExecutorConnection = createTaskExecutorConnection();
		final SlotID slotId = new SlotID(taskExecutorConnection.getResourceID(), 0);
		final SlotStatus slotStatus = new SlotStatus(slotId, ResourceProfile.UNKNOWN, jobId, new AllocationID());
		final SlotReport slotReport = new SlotReport(slotStatus);

		slotManager.registerTaskManager(taskExecutorConnection, slotReport);

		assertThat(slotManager.getNumberRegisteredSlots(), is(1));
		assertThat(slotManager.getNumberPendingTaskManagerSlots(), is(numberSlots));
		assertThat(slotManager.getNumberAssignedPendingTaskManagerSlots(), is(1));
	}
}
 
Example #11
Source File: SlotManagerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that only free slots can fulfill/complete a pending task manager slot.
 */
@Test
public void testOnlyFreeSlotsCanFulfillPendingTaskManagerSlot() throws Exception {
	final int numberSlots = 1;
	final TestingResourceActions resourceActions = new TestingResourceActionsBuilder()
		.setAllocateResourceFunction(convert(value -> numberSlots))
		.build();

	try (final SlotManagerImpl slotManager = createSlotManager(ResourceManagerId.generate(), resourceActions)) {
		final JobID jobId = new JobID();
		assertThat(slotManager.registerSlotRequest(createSlotRequest(jobId)), is(true));

		final TaskExecutorConnection taskExecutorConnection = createTaskExecutorConnection();
		final SlotID slotId = new SlotID(taskExecutorConnection.getResourceID(), 0);
		final SlotStatus slotStatus = new SlotStatus(slotId, ResourceProfile.UNKNOWN, jobId, new AllocationID());
		final SlotReport slotReport = new SlotReport(slotStatus);

		slotManager.registerTaskManager(taskExecutorConnection, slotReport);

		assertThat(slotManager.getNumberRegisteredSlots(), is(1));
		assertThat(slotManager.getNumberPendingTaskManagerSlots(), is(numberSlots));
		assertThat(slotManager.getNumberAssignedPendingTaskManagerSlots(), is(1));
	}
}
 
Example #12
Source File: SlotManagerImplTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can register task manager and their slots at the slot manager.
 */
@Test
public void testTaskManagerRegistration() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceActions resourceManagerActions = new TestingResourceActionsBuilder().build();

	final TaskExecutorGateway taskExecutorGateway = new TestingTaskExecutorGatewayBuilder().createTestingTaskExecutorGateway();
	final ResourceID resourceId = ResourceID.generate();
	final TaskExecutorConnection taskManagerConnection = new TaskExecutorConnection(resourceId, taskExecutorGateway);

	final SlotID slotId1 = new SlotID(resourceId, 0);
	final SlotID slotId2 = new SlotID(resourceId, 1);
	final ResourceProfile resourceProfile = ResourceProfile.fromResources(42.0, 1337);
	final SlotStatus slotStatus1 = new SlotStatus(slotId1, resourceProfile);
	final SlotStatus slotStatus2 = new SlotStatus(slotId2, resourceProfile);
	final SlotReport slotReport = new SlotReport(Arrays.asList(slotStatus1, slotStatus2));

	try (SlotManagerImpl slotManager = createSlotManager(resourceManagerId, resourceManagerActions)) {
		slotManager.registerTaskManager(taskManagerConnection, slotReport);

		assertTrue("The number registered slots does not equal the expected number.", 2 == slotManager.getNumberRegisteredSlots());

		assertNotNull(slotManager.getSlot(slotId1));
		assertNotNull(slotManager.getSlot(slotId2));
	}
}
 
Example #13
Source File: SlotManagerImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a failed slot request. The slot manager tries to find a new slot fulfilling
 * the resource requirements for the failed slot request.
 *
 * @param slotId identifying the slot which was assigned to the slot request before
 * @param allocationId identifying the failed slot request
 * @param cause of the failure
 */
private void handleFailedSlotRequest(SlotID slotId, AllocationID allocationId, Throwable cause) {
	PendingSlotRequest pendingSlotRequest = pendingSlotRequests.get(allocationId);

	LOG.debug("Slot request with allocation id {} failed for slot {}.", allocationId, slotId, cause);

	if (null != pendingSlotRequest) {
		pendingSlotRequest.setRequestFuture(null);

		try {
			internalRequestSlot(pendingSlotRequest);
		} catch (ResourceManagerException e) {
			pendingSlotRequests.remove(allocationId);

			resourceActions.notifyAllocationFailure(
				pendingSlotRequest.getJobId(),
				allocationId,
				e);
		}
	} else {
		LOG.debug("There was not pending slot request with allocation id {}. Probably the request has been fulfilled or cancelled.", allocationId);
	}
}
 
Example #14
Source File: SlotManagerImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Updates a slot with the given allocation id.
 *
 * @param slotId to update
 * @param allocationId specifying the current allocation of the slot
 * @param jobId specifying the job to which the slot is allocated
 * @return True if the slot could be updated; otherwise false
 */
private boolean updateSlot(SlotID slotId, AllocationID allocationId, JobID jobId) {
	final TaskManagerSlot slot = slots.get(slotId);

	if (slot != null) {
		final TaskManagerRegistration taskManagerRegistration = taskManagerRegistrations.get(slot.getInstanceId());

		if (taskManagerRegistration != null) {
			updateSlotState(slot, taskManagerRegistration, allocationId, jobId);

			return true;
		} else {
			throw new IllegalStateException("Trying to update a slot from a TaskManager " +
				slot.getInstanceId() + " which has not been registered.");
		}
	} else {
		LOG.debug("Trying to update unknown slot with slot id {}.", slotId);

		return false;
	}
}
 
Example #15
Source File: ResourceManager.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void notifySlotAvailable(
		final InstanceID instanceID,
		final SlotID slotId,
		final AllocationID allocationId) {

	final ResourceID resourceId = slotId.getResourceID();
	WorkerRegistration<WorkerType> registration = taskExecutors.get(resourceId);

	if (registration != null) {
		InstanceID registrationId = registration.getInstanceID();

		if (Objects.equals(registrationId, instanceID)) {
			slotManager.freeSlot(slotId, allocationId);
		} else {
			log.debug("Invalid registration id for slot available message. This indicates an" +
				" outdated request.");
		}
	} else {
		log.debug("Could not find registration for resource id {}. Discarding the slot available" +
			"message {}.", resourceId, slotId);
	}
}
 
Example #16
Source File: SlotManagerImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a pending slot request identified by the given allocation id from a slot identified
 * by the given slot id.
 *
 * @param slotId identifying the slot
 * @param allocationId identifying the presumable assigned pending slot request
 */
private void removeSlotRequestFromSlot(SlotID slotId, AllocationID allocationId) {
	TaskManagerSlot taskManagerSlot = slots.get(slotId);

	if (null != taskManagerSlot) {
		if (taskManagerSlot.getState() == TaskManagerSlot.State.PENDING && Objects.equals(allocationId, taskManagerSlot.getAssignedSlotRequest().getAllocationId())) {

			TaskManagerRegistration taskManagerRegistration = taskManagerRegistrations.get(taskManagerSlot.getInstanceId());

			if (taskManagerRegistration == null) {
				throw new IllegalStateException("Trying to remove slot request from slot for which there is no TaskManager " + taskManagerSlot.getInstanceId() + " is registered.");
			}

			// clear the pending slot request
			taskManagerSlot.clearPendingSlotRequest();

			updateSlotState(taskManagerSlot, taskManagerRegistration, null, null);
		} else {
			LOG.debug("Ignore slot request removal for slot {}.", slotId);
		}
	} else {
		LOG.debug("There was no slot with {} registered. Probably this slot has been already freed.", slotId);
	}
}
 
Example #17
Source File: SlotManagerImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a failed slot request. The slot manager tries to find a new slot fulfilling
 * the resource requirements for the failed slot request.
 *
 * @param slotId identifying the slot which was assigned to the slot request before
 * @param allocationId identifying the failed slot request
 * @param cause of the failure
 */
private void handleFailedSlotRequest(SlotID slotId, AllocationID allocationId, Throwable cause) {
	PendingSlotRequest pendingSlotRequest = pendingSlotRequests.get(allocationId);

	LOG.debug("Slot request with allocation id {} failed for slot {}.", allocationId, slotId, cause);

	if (null != pendingSlotRequest) {
		pendingSlotRequest.setRequestFuture(null);

		try {
			internalRequestSlot(pendingSlotRequest);
		} catch (ResourceManagerException e) {
			pendingSlotRequests.remove(allocationId);

			resourceActions.notifyAllocationFailure(
				pendingSlotRequest.getJobId(),
				allocationId,
				e);
		}
	} else {
		LOG.debug("There was not pending slot request with allocation id {}. Probably the request has been fulfilled or cancelled.", allocationId);
	}
}
 
Example #18
Source File: SlotManagerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a task manager timeout does not remove the slots from the SlotManager.
 * A timeout should only trigger the {@link ResourceActions#releaseResource(InstanceID, Exception)}
 * callback. The receiver of the callback can then decide what to do with the TaskManager.
 *
 * <p>See FLINK-7793
 */
@Test
public void testTaskManagerTimeoutDoesNotRemoveSlots() throws Exception {
	final Time taskManagerTimeout = Time.milliseconds(10L);
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceID resourceID = ResourceID.generate();
	final ResourceActions resourceActions = mock(ResourceActions.class);
	final TaskExecutorGateway taskExecutorGateway = mock(TaskExecutorGateway.class);

	when(taskExecutorGateway.canBeReleased()).thenReturn(CompletableFuture.completedFuture(true));

	final TaskExecutorConnection taskExecutorConnection = new TaskExecutorConnection(resourceID, taskExecutorGateway);
	final SlotStatus slotStatus = new SlotStatus(
		new SlotID(resourceID, 0),
		new ResourceProfile(1.0, 1));
	final SlotReport initialSlotReport = new SlotReport(slotStatus);

	try (final SlotManager slotManager = SlotManagerBuilder.newBuilder()
		.setTaskManagerTimeout(taskManagerTimeout)
		.build()) {

		slotManager.start(resourceManagerId, Executors.directExecutor(), resourceActions);

		slotManager.registerTaskManager(taskExecutorConnection, initialSlotReport);

		assertEquals(1, slotManager.getNumberRegisteredSlots());

		// wait for the timeout call to happen
		verify(resourceActions, timeout(taskManagerTimeout.toMilliseconds() * 20L).atLeast(1)).releaseResource(eq(taskExecutorConnection.getInstanceID()), any(Exception.class));

		assertEquals(1, slotManager.getNumberRegisteredSlots());

		slotManager.unregisterTaskManager(taskExecutorConnection.getInstanceID());

		assertEquals(0, slotManager.getNumberRegisteredSlots());
	}
}
 
Example #19
Source File: SlotManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a task manager timeout does not remove the slots from the SlotManager.
 * A timeout should only trigger the {@link ResourceActions#releaseResource(InstanceID, Exception)}
 * callback. The receiver of the callback can then decide what to do with the TaskManager.
 *
 * <p>See FLINK-7793
 */
@Test
public void testTaskManagerTimeoutDoesNotRemoveSlots() throws Exception {
	final Time taskManagerTimeout = Time.milliseconds(10L);
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceID resourceID = ResourceID.generate();
	final ResourceActions resourceActions = mock(ResourceActions.class);
	final TaskExecutorGateway taskExecutorGateway = mock(TaskExecutorGateway.class);

	when(taskExecutorGateway.canBeReleased()).thenReturn(CompletableFuture.completedFuture(true));

	final TaskExecutorConnection taskExecutorConnection = new TaskExecutorConnection(resourceID, taskExecutorGateway);
	final SlotStatus slotStatus = new SlotStatus(
		new SlotID(resourceID, 0),
		new ResourceProfile(1.0, 1));
	final SlotReport initialSlotReport = new SlotReport(slotStatus);

	try (final SlotManager slotManager = SlotManagerBuilder.newBuilder()
		.setTaskManagerTimeout(taskManagerTimeout)
		.build()) {

		slotManager.start(resourceManagerId, Executors.directExecutor(), resourceActions);

		slotManager.registerTaskManager(taskExecutorConnection, initialSlotReport);

		assertEquals(1, slotManager.getNumberRegisteredSlots());

		// wait for the timeout call to happen
		verify(resourceActions, timeout(taskManagerTimeout.toMilliseconds() * 20L).atLeast(1)).releaseResource(eq(taskExecutorConnection.getInstanceID()), any(Exception.class));

		assertEquals(1, slotManager.getNumberRegisteredSlots());

		slotManager.unregisterTaskManager(taskExecutorConnection.getInstanceID());

		assertEquals(0, slotManager.getNumberRegisteredSlots());
	}
}
 
Example #20
Source File: SlotManager.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Registers a slot for the given task manager at the slot manager. The slot is identified by
 * the given slot id. The given resource profile defines the available resources for the slot.
 * The task manager connection can be used to communicate with the task manager.
 *
 * @param slotId identifying the slot on the task manager
 * @param allocationId which is currently deployed in the slot
 * @param resourceProfile of the slot
 * @param taskManagerConnection to communicate with the remote task manager
 */
private void registerSlot(
		SlotID slotId,
		AllocationID allocationId,
		JobID jobId,
		ResourceProfile resourceProfile,
		TaskExecutorConnection taskManagerConnection) {

	if (slots.containsKey(slotId)) {
		// remove the old slot first
		removeSlot(slotId);
	}

	final TaskManagerSlot slot = createAndRegisterTaskManagerSlot(slotId, resourceProfile, taskManagerConnection);

	final PendingTaskManagerSlot pendingTaskManagerSlot;

	if (allocationId == null) {
		pendingTaskManagerSlot = findExactlyMatchingPendingTaskManagerSlot(resourceProfile);
	} else {
		pendingTaskManagerSlot = null;
	}

	if (pendingTaskManagerSlot == null) {
		updateSlot(slotId, allocationId, jobId);
	} else {
		pendingSlots.remove(pendingTaskManagerSlot.getTaskManagerSlotId());
		final PendingSlotRequest assignedPendingSlotRequest = pendingTaskManagerSlot.getAssignedPendingSlotRequest();

		if (assignedPendingSlotRequest == null) {
			handleFreeSlot(slot);
		} else {
			assignedPendingSlotRequest.unassignPendingTaskManagerSlot();
			allocateSlot(slot, assignedPendingSlotRequest);
		}
	}
}
 
Example #21
Source File: SlotStatus.java    From flink with Apache License 2.0 5 votes vote down vote up
public SlotStatus(
	SlotID slotID,
	ResourceProfile resourceProfile,
	JobID jobID,
	AllocationID allocationID) {
	this.slotID = checkNotNull(slotID, "slotID cannot be null");
	this.resourceProfile = checkNotNull(resourceProfile, "profile cannot be null");
	this.allocationID = allocationID;
	this.jobID = jobID;
}
 
Example #22
Source File: SlotManager.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the given slot from the slot manager.
 *
 * @param slotId identifying the slot to remove
 */
private void removeSlot(SlotID slotId) {
	TaskManagerSlot slot = slots.remove(slotId);

	if (null != slot) {
		freeSlots.remove(slotId);

		if (slot.getState() == TaskManagerSlot.State.PENDING) {
			// reject the pending slot request --> triggering a new allocation attempt
			rejectPendingSlotRequest(
				slot.getAssignedSlotRequest(),
				new Exception("The assigned slot " + slot.getSlotId() + " was removed."));
		}

		AllocationID oldAllocationId = slot.getAllocationId();

		if (oldAllocationId != null) {
			fulfilledSlotRequests.remove(oldAllocationId);

			resourceActions.notifyAllocationFailure(
				slot.getJobId(),
				oldAllocationId,
				new FlinkException("The assigned slot " + slot.getSlotId() + " was removed."));
		}
	} else {
		LOG.debug("There was no slot registered with slot id {}.", slotId);
	}
}
 
Example #23
Source File: SlotManagerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that duplicate slot requests (requests with an already registered allocation id) are
 * also detected after a pending slot request has been fulfilled but not yet freed.
 */
@Test
public void testDuplicatePendingSlotRequestAfterSuccessfulAllocation() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final AtomicInteger allocateResourceCalls = new AtomicInteger(0);
	final ResourceActions resourceManagerActions = new TestingResourceActionsBuilder()
		.setAllocateResourceConsumer(resourceProfile -> allocateResourceCalls.incrementAndGet())
		.build();
	final AllocationID allocationId = new AllocationID();
	final ResourceProfile resourceProfile1 = new ResourceProfile(1.0, 2);
	final ResourceProfile resourceProfile2 = new ResourceProfile(2.0, 1);
	final SlotRequest slotRequest1 = new SlotRequest(new JobID(), allocationId, resourceProfile1, "foobar");
	final SlotRequest slotRequest2 = new SlotRequest(new JobID(), allocationId, resourceProfile2, "barfoo");

	final TaskExecutorGateway taskExecutorGateway = new TestingTaskExecutorGatewayBuilder().createTestingTaskExecutorGateway();

	final ResourceID resourceID = ResourceID.generate();

	final TaskExecutorConnection taskManagerConnection = new TaskExecutorConnection(resourceID, taskExecutorGateway);

	final SlotID slotId = new SlotID(resourceID, 0);
	final SlotStatus slotStatus = new SlotStatus(slotId, resourceProfile1);
	final SlotReport slotReport = new SlotReport(slotStatus);

	try (SlotManager slotManager = createSlotManager(resourceManagerId, resourceManagerActions)) {
		slotManager.registerTaskManager(taskManagerConnection, slotReport);
		assertTrue(slotManager.registerSlotRequest(slotRequest1));

		TaskManagerSlot slot = slotManager.getSlot(slotId);

		assertEquals("The slot has not been allocated to the expected allocation id.", allocationId, slot.getAllocationId());

		assertFalse(slotManager.registerSlotRequest(slotRequest2));
	}

	// check that we have only called the resource allocation only for the first slot request,
	// since the second request is a duplicate
	assertThat(allocateResourceCalls.get(), is(0));
}
 
Example #24
Source File: SlotManagerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that un-registering a pending slot request will cancel it, removing it from all
 * assigned task manager slots and then remove it from the slot manager.
 */
@Test
public void testUnregisterPendingSlotRequest() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceActions resourceManagerActions = mock(ResourceActions.class);
	final ResourceID resourceID = ResourceID.generate();
	final SlotID slotId = new SlotID(resourceID, 0);
	final AllocationID allocationId = new AllocationID();

	final TaskExecutorGateway taskExecutorGateway = new TestingTaskExecutorGatewayBuilder()
		.setRequestSlotFunction(slotIDJobIDAllocationIDStringResourceManagerIdTuple5 -> new CompletableFuture<>())
		.createTestingTaskExecutorGateway();

	final ResourceProfile resourceProfile = new ResourceProfile(1.0, 1);
	final SlotStatus slotStatus = new SlotStatus(slotId, resourceProfile);
	final SlotReport slotReport = new SlotReport(slotStatus);

	final SlotRequest slotRequest = new SlotRequest(new JobID(), allocationId, resourceProfile, "foobar");

	final TaskExecutorConnection taskManagerConnection = new TaskExecutorConnection(resourceID, taskExecutorGateway);

	try (SlotManager slotManager = createSlotManager(resourceManagerId, resourceManagerActions)) {
		slotManager.registerTaskManager(taskManagerConnection, slotReport);

		TaskManagerSlot slot = slotManager.getSlot(slotId);

		slotManager.registerSlotRequest(slotRequest);

		assertNotNull(slotManager.getSlotRequest(allocationId));

		assertTrue(slot.getState() == TaskManagerSlot.State.PENDING);

		slotManager.unregisterSlotRequest(allocationId);

		assertNull(slotManager.getSlotRequest(allocationId));

		slot = slotManager.getSlot(slotId);
		assertTrue(slot.getState() == TaskManagerSlot.State.FREE);
	}
}
 
Example #25
Source File: SlotStatus.java    From flink with Apache License 2.0 5 votes vote down vote up
public SlotStatus(
	SlotID slotID,
	ResourceProfile resourceProfile,
	JobID jobID,
	AllocationID allocationID) {
	this.slotID = checkNotNull(slotID, "slotID cannot be null");
	this.resourceProfile = checkNotNull(resourceProfile, "profile cannot be null");
	this.allocationID = allocationID;
	this.jobID = jobID;
}
 
Example #26
Source File: SlotManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that duplicate slot requests (requests with an already registered allocation id) are
 * also detected after a pending slot request has been fulfilled but not yet freed.
 */
@Test
public void testDuplicatePendingSlotRequestAfterSuccessfulAllocation() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final AtomicInteger allocateResourceCalls = new AtomicInteger(0);
	final ResourceActions resourceManagerActions = new TestingResourceActionsBuilder()
		.setAllocateResourceConsumer(resourceProfile -> allocateResourceCalls.incrementAndGet())
		.build();
	final AllocationID allocationId = new AllocationID();
	final ResourceProfile resourceProfile1 = new ResourceProfile(1.0, 2);
	final ResourceProfile resourceProfile2 = new ResourceProfile(2.0, 1);
	final SlotRequest slotRequest1 = new SlotRequest(new JobID(), allocationId, resourceProfile1, "foobar");
	final SlotRequest slotRequest2 = new SlotRequest(new JobID(), allocationId, resourceProfile2, "barfoo");

	final TaskExecutorGateway taskExecutorGateway = new TestingTaskExecutorGatewayBuilder().createTestingTaskExecutorGateway();

	final ResourceID resourceID = ResourceID.generate();

	final TaskExecutorConnection taskManagerConnection = new TaskExecutorConnection(resourceID, taskExecutorGateway);

	final SlotID slotId = new SlotID(resourceID, 0);
	final SlotStatus slotStatus = new SlotStatus(slotId, resourceProfile1);
	final SlotReport slotReport = new SlotReport(slotStatus);

	try (SlotManagerImpl slotManager = createSlotManager(resourceManagerId, resourceManagerActions)) {
		slotManager.registerTaskManager(taskManagerConnection, slotReport);
		assertTrue(slotManager.registerSlotRequest(slotRequest1));

		TaskManagerSlot slot = slotManager.getSlot(slotId);

		assertEquals("The slot has not been allocated to the expected allocation id.", allocationId, slot.getAllocationId());

		assertFalse(slotManager.registerSlotRequest(slotRequest2));
	}

	// check that we have only called the resource allocation only for the first slot request,
	// since the second request is a duplicate
	assertThat(allocateResourceCalls.get(), is(0));
}
 
Example #27
Source File: SlotManagerImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Free the given slot from the given allocation. If the slot is still allocated by the given
 * allocation id, then the slot will be marked as free and will be subject to new slot requests.
 *
 * @param slotId identifying the slot to free
 * @param allocationId with which the slot is presumably allocated
 */
@Override
public void freeSlot(SlotID slotId, AllocationID allocationId) {
	checkInit();

	TaskManagerSlot slot = slots.get(slotId);

	if (null != slot) {
		if (slot.getState() == TaskManagerSlot.State.ALLOCATED) {
			if (Objects.equals(allocationId, slot.getAllocationId())) {

				TaskManagerRegistration taskManagerRegistration = taskManagerRegistrations.get(slot.getInstanceId());

				if (taskManagerRegistration == null) {
					throw new IllegalStateException("Trying to free a slot from a TaskManager " +
						slot.getInstanceId() + " which has not been registered.");
				}

				updateSlotState(slot, taskManagerRegistration, null, null);
			} else {
				LOG.debug("Received request to free slot {} with expected allocation id {}, " +
					"but actual allocation id {} differs. Ignoring the request.", slotId, allocationId, slot.getAllocationId());
			}
		} else {
			LOG.debug("Slot {} has not been allocated.", allocationId);
		}
	} else {
		LOG.debug("Trying to free a slot {} which has not been registered. Ignoring this message.", slotId);
	}
}
 
Example #28
Source File: SlotManagerImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Registers a slot for the given task manager at the slot manager. The slot is identified by
 * the given slot id. The given resource profile defines the available resources for the slot.
 * The task manager connection can be used to communicate with the task manager.
 *
 * @param slotId identifying the slot on the task manager
 * @param allocationId which is currently deployed in the slot
 * @param resourceProfile of the slot
 * @param taskManagerConnection to communicate with the remote task manager
 */
private void registerSlot(
		SlotID slotId,
		AllocationID allocationId,
		JobID jobId,
		ResourceProfile resourceProfile,
		TaskExecutorConnection taskManagerConnection) {

	if (slots.containsKey(slotId)) {
		// remove the old slot first
		removeSlot(slotId);
	}

	final TaskManagerSlot slot = createAndRegisterTaskManagerSlot(slotId, resourceProfile, taskManagerConnection);

	final PendingTaskManagerSlot pendingTaskManagerSlot;

	if (allocationId == null) {
		pendingTaskManagerSlot = findExactlyMatchingPendingTaskManagerSlot(resourceProfile);
	} else {
		pendingTaskManagerSlot = null;
	}

	if (pendingTaskManagerSlot == null) {
		updateSlot(slotId, allocationId, jobId);
	} else {
		pendingSlots.remove(pendingTaskManagerSlot.getTaskManagerSlotId());
		final PendingSlotRequest assignedPendingSlotRequest = pendingTaskManagerSlot.getAssignedPendingSlotRequest();

		if (assignedPendingSlotRequest == null) {
			handleFreeSlot(slot);
		} else {
			assignedPendingSlotRequest.unassignPendingTaskManagerSlot();
			allocateSlot(slot, assignedPendingSlotRequest);
		}
	}
}
 
Example #29
Source File: SlotManagerImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Nonnull
private TaskManagerSlot createAndRegisterTaskManagerSlot(SlotID slotId, ResourceProfile resourceProfile, TaskExecutorConnection taskManagerConnection) {
	final TaskManagerSlot slot = new TaskManagerSlot(
		slotId,
		resourceProfile,
		taskManagerConnection);
	slots.put(slotId, slot);
	return slot;
}
 
Example #30
Source File: SlotManagerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that free slots which are reported as allocated won't be considered for fulfilling
 * other pending slot requests.
 *
 * <p>See: FLINK-8505
 */
@Test
public void testReportAllocatedSlot() throws Exception {
	final ResourceID taskManagerId = ResourceID.generate();
	final ResourceActions resourceActions = new TestingResourceActionsBuilder().build();
	final TestingTaskExecutorGateway taskExecutorGateway = new TestingTaskExecutorGatewayBuilder().createTestingTaskExecutorGateway();
	final TaskExecutorConnection taskExecutorConnection = new TaskExecutorConnection(taskManagerId, taskExecutorGateway);

	try (final SlotManager slotManager = SlotManagerBuilder.newBuilder().build()) {

		slotManager.start(ResourceManagerId.generate(), Executors.directExecutor(), resourceActions);

		// initially report a single slot as free
		final SlotID slotId = new SlotID(taskManagerId, 0);
		final SlotStatus initialSlotStatus = new SlotStatus(
			slotId,
			ResourceProfile.UNKNOWN);
		final SlotReport initialSlotReport = new SlotReport(initialSlotStatus);

		slotManager.registerTaskManager(taskExecutorConnection, initialSlotReport);

		assertThat(slotManager.getNumberRegisteredSlots(), is(equalTo(1)));

		// Now report this slot as allocated
		final SlotStatus slotStatus = new SlotStatus(
			slotId,
			ResourceProfile.UNKNOWN,
			new JobID(),
			new AllocationID());
		final SlotReport slotReport = new SlotReport(
			slotStatus);

		slotManager.reportSlotStatus(
			taskExecutorConnection.getInstanceID(),
			slotReport);

		// this slot request should not be fulfilled
		final AllocationID allocationId = new AllocationID();
		final SlotRequest slotRequest = new SlotRequest(
			new JobID(),
			allocationId,
			ResourceProfile.UNKNOWN,
			"foobar");

		// This triggered an IllegalStateException before
		slotManager.registerSlotRequest(slotRequest);

		assertThat(slotManager.getSlotRequest(allocationId).isAssigned(), is(false));
	}
}