Java Code Examples for org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraint#lockLocation()

The following examples show how to use org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraint#lockLocation() . 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: SharedSlotsTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * We allocate and the structure below and release it from the root.
 *
 * <pre>
 *     Shared(0)(root)
 *        |
 *        +-- Simple(2)(sink)
 *        |
 *        +-- Shared(1)(co-location-group)
 *        |      |
 *        |      +-- Simple(0)(tail)
 *        |      +-- Simple(1)(head)
 *        |
 *        +-- Simple(0)(source)
 * </pre>
 */
@Test
public void testReleaseTwoLevelsFromRoot() {
	try {
		JobVertexID sourceId = new JobVertexID();
		JobVertexID headId = new JobVertexID();
		JobVertexID tailId = new JobVertexID();
		JobVertexID sinkId = new JobVertexID();

		JobVertex headVertex = new JobVertex("head", headId);
		JobVertex tailVertex = new JobVertex("tail", tailId);

		SlotSharingGroup sharingGroup = new SlotSharingGroup(sourceId, headId, tailId, sinkId);
		SlotSharingGroupAssignment assignment = sharingGroup.getTaskAssignment();
		assertEquals(0, assignment.getNumberOfSlots());

		CoLocationGroup coLocationGroup = new CoLocationGroup(headVertex, tailVertex);
		CoLocationConstraint constraint = coLocationGroup.getLocationConstraint(0);
		assertFalse(constraint.isAssigned());

		Instance instance = SchedulerTestUtils.getRandomInstance(1);

		// allocate a shared slot
		SharedSlot sharedSlot = instance.allocateSharedSlot(assignment);

		// get the first simple slot
		SimpleSlot sourceSlot = assignment.addSharedSlotAndAllocateSubSlot(sharedSlot, Locality.LOCAL, sourceId);
		
		SimpleSlot headSlot = assignment.getSlotForTask(constraint, NO_LOCATION);
		constraint.lockLocation();
		SimpleSlot tailSlot = assignment.getSlotForTask(constraint, NO_LOCATION);
		
		SimpleSlot sinkSlot = assignment.getSlotForTask(sinkId, NO_LOCATION);
		
		assertEquals(4, sharedSlot.getNumberLeaves());

		// release all
		sourceSlot.releaseSlot();
		headSlot.releaseSlot();
		tailSlot.releaseSlot();
		sinkSlot.releaseSlot();

		assertTrue(sharedSlot.isReleased());
		assertTrue(sourceSlot.isReleased());
		assertTrue(headSlot.isReleased());
		assertTrue(tailSlot.isReleased());
		assertTrue(sinkSlot.isReleased());
		assertTrue(constraint.getSharedSlot().isReleased());

		assertTrue(constraint.isAssigned());
		assertFalse(constraint.isAssignedAndAlive());

		assertEquals(1, instance.getNumberOfAvailableSlots());
		assertEquals(0, instance.getNumberOfAllocatedSlots());
		
		assertEquals(0, assignment.getNumberOfSlots());
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}