Skip to content

Commit

Permalink
[FLINK-8087] Decouple Slot from AllocatedSlot
Browse files Browse the repository at this point in the history
This commit introduces the SlotContext which is an abstraction for the SimpleSlot
to obtain the relevant slot information to do the communication with the
TaskManager without relying on the AllocatedSlot which is now only used by the
SlotPool.

This closes apache#5088.
  • Loading branch information
tillrohrmann committed Dec 14, 2017
1 parent 627bcda commit a569f38
Show file tree
Hide file tree
Showing 36 changed files with 887 additions and 563 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@
* limitations under the License.
*/

package org.apache.flink.runtime.jobmanager.slots;
package org.apache.flink.runtime.instance;

import org.apache.flink.api.common.JobID;
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.jobmanager.scheduler.Locality;
import org.apache.flink.runtime.jobmanager.slots.SlotContext;
import org.apache.flink.runtime.jobmanager.slots.SlotException;
import org.apache.flink.runtime.jobmanager.slots.SlotOwner;
import org.apache.flink.runtime.jobmanager.slots.TaskManagerGateway;
import org.apache.flink.runtime.taskmanager.TaskManagerLocation;

import java.util.concurrent.atomic.AtomicReference;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
Expand All @@ -38,14 +44,11 @@
* an AllocatedSlot was allocated to the JobManager as soon as the TaskManager registered at the
* JobManager. All slots had a default unknown resource profile.
*/
public class AllocatedSlot {
public class AllocatedSlot implements SlotContext {

/** The ID under which the slot is allocated. Uniquely identifies the slot. */
private final AllocationID slotAllocationId;

/** The ID of the job this slot is allocated for */
private final JobID jobID;

/** The location information of the TaskManager to which this slot belongs */
private final TaskManagerLocation taskManagerLocation;

Expand All @@ -56,23 +59,29 @@ public class AllocatedSlot {
private final TaskManagerGateway taskManagerGateway;

/** The number of the slot on the TaskManager to which slot belongs. Purely informational. */
private final int slotNumber;
private final int physicalSlotNumber;

private final SlotOwner slotOwner;

private final AtomicReference<LogicalSlot> logicalSlotReference;

// ------------------------------------------------------------------------

public AllocatedSlot(
AllocationID slotAllocationId,
JobID jobID,
TaskManagerLocation location,
int slotNumber,
int physicalSlotNumber,
ResourceProfile resourceProfile,
TaskManagerGateway taskManagerGateway) {
TaskManagerGateway taskManagerGateway,
SlotOwner slotOwner) {
this.slotAllocationId = checkNotNull(slotAllocationId);
this.jobID = checkNotNull(jobID);
this.taskManagerLocation = checkNotNull(location);
this.slotNumber = slotNumber;
this.physicalSlotNumber = physicalSlotNumber;
this.resourceProfile = checkNotNull(resourceProfile);
this.taskManagerGateway = checkNotNull(taskManagerGateway);
this.slotOwner = checkNotNull(slotOwner);

logicalSlotReference = new AtomicReference<>(null);
}

// ------------------------------------------------------------------------
Expand All @@ -82,7 +91,7 @@ public AllocatedSlot(
*
* @return The ID under which the slot is allocated
*/
public AllocationID getSlotAllocationId() {
public AllocationID getAllocationId() {
return slotAllocationId;
}

Expand All @@ -97,22 +106,13 @@ public ResourceID getTaskManagerId() {
return getTaskManagerLocation().getResourceID();
}

/**
* Returns the ID of the job this allocated slot belongs to.
*
* @return the ID of the job this allocated slot belongs to
*/
public JobID getJobID() {
return jobID;
}

/**
* Gets the number of the slot.
*
* @return The number of the slot on the TaskManager.
*/
public int getSlotNumber() {
return slotNumber;
public int getPhysicalSlotNumber() {
return physicalSlotNumber;
}

/**
Expand Down Expand Up @@ -144,6 +144,78 @@ public TaskManagerGateway getTaskManagerGateway() {
return taskManagerGateway;
}

/**
* Triggers the release of the logical slot.
*/
public void triggerLogicalSlotRelease() {
final LogicalSlot logicalSlot = logicalSlotReference.get();

if (logicalSlot != null) {
logicalSlot.releaseSlot();
}
}

/**
* Releases the logical slot.
*
* @return true if the logical slot could be released, false otherwise.
*/
public boolean releaseLogicalSlot() {
final LogicalSlot logicalSlot = logicalSlotReference.get();

if (logicalSlot != null) {
if (logicalSlot instanceof Slot) {
final Slot slot = (Slot) logicalSlot;
if (slot.markReleased()) {
logicalSlotReference.set(null);
return true;
}
} else {
throw new RuntimeException("Unsupported logical slot type encountered " + logicalSlot.getClass());
}

}

return false;
}

/**
* Allocates a logical {@link SimpleSlot}.
*
* @return an allocated logical simple slot
* @throws SlotException if we could not allocate a simple slot
*/
public SimpleSlot allocateSimpleSlot(Locality locality) throws SlotException {

final SimpleSlot simpleSlot = new SimpleSlot(this, slotOwner, physicalSlotNumber);

if (logicalSlotReference.compareAndSet(null, simpleSlot)) {
simpleSlot.setLocality(locality);
return simpleSlot;
} else {
throw new SlotException("Could not allocate logical simple slot because the allocated slot is already used.");
}
}

/**
* Allocates a logical {@link SharedSlot}.
*
* @param slotSharingGroupAssignment the slot sharing group to which the shared slot shall belong
* @return an allocated logical shared slot
* @throws SlotException if we could not allocate a shared slot
*/
public SharedSlot allocateSharedSlot(SlotSharingGroupAssignment slotSharingGroupAssignment) throws SlotException {
final SharedSlot sharedSlot = new SharedSlot(this, slotOwner, slotSharingGroupAssignment);

if (logicalSlotReference.compareAndSet(null, sharedSlot)) {


return sharedSlot;
} else {
throw new SlotException("Could not allocate logical shared slot because the allocated slot is already used.");
}
}

// ------------------------------------------------------------------------

/**
Expand All @@ -164,6 +236,6 @@ public final boolean equals(Object obj) {

@Override
public String toString() {
return "AllocatedSlot " + slotAllocationId + " @ " + taskManagerLocation + " - " + slotNumber;
return "AllocatedSlot " + slotAllocationId + " @ " + taskManagerLocation + " - " + physicalSlotNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.apache.flink.runtime.instance;

import org.apache.flink.api.common.JobID;
import org.apache.flink.runtime.clusterframework.types.ResourceID;
import org.apache.flink.runtime.jobmanager.scheduler.SlotAvailabilityListener;
import org.apache.flink.runtime.jobmanager.slots.SlotOwner;
Expand Down Expand Up @@ -210,19 +209,13 @@ public boolean isStillAlive(long now, long cleanUpInterval) {
* Allocates a simple slot on this TaskManager instance. This method returns {@code null}, if no slot
* is available at the moment.
*
* @param jobID The ID of the job that the slot is allocated for.
*
* @return A simple slot that represents a task slot on this TaskManager instance, or null, if the
* TaskManager instance has no more slots available.
*
* @throws InstanceDiedException Thrown if the instance is no longer alive by the time the
* slot is allocated.
*/
public SimpleSlot allocateSimpleSlot(JobID jobID) throws InstanceDiedException {
if (jobID == null) {
throw new IllegalArgumentException();
}

public SimpleSlot allocateSimpleSlot() throws InstanceDiedException {
synchronized (instanceLock) {
if (isDead) {
throw new InstanceDiedException(this);
Expand All @@ -233,7 +226,7 @@ public SimpleSlot allocateSimpleSlot(JobID jobID) throws InstanceDiedException {
return null;
}
else {
SimpleSlot slot = new SimpleSlot(jobID, this, location, nextSlot, taskManagerGateway);
SimpleSlot slot = new SimpleSlot(this, location, nextSlot, taskManagerGateway);
allocatedSlots.add(slot);
return slot;
}
Expand All @@ -244,21 +237,15 @@ public SimpleSlot allocateSimpleSlot(JobID jobID) throws InstanceDiedException {
* Allocates a shared slot on this TaskManager instance. This method returns {@code null}, if no slot
* is available at the moment. The shared slot will be managed by the given SlotSharingGroupAssignment.
*
* @param jobID The ID of the job that the slot is allocated for.
* @param sharingGroupAssignment The assignment group that manages this shared slot.
*
* @return A shared slot that represents a task slot on this TaskManager instance and can hold other
* (shared) slots, or null, if the TaskManager instance has no more slots available.
*
* @throws InstanceDiedException Thrown if the instance is no longer alive by the time the slot is allocated.
*/
public SharedSlot allocateSharedSlot(JobID jobID, SlotSharingGroupAssignment sharingGroupAssignment)
throws InstanceDiedException
{
// the slot needs to be in the returned to taskManager state
if (jobID == null) {
throw new IllegalArgumentException();
}
public SharedSlot allocateSharedSlot(SlotSharingGroupAssignment sharingGroupAssignment)
throws InstanceDiedException {

synchronized (instanceLock) {
if (isDead) {
Expand All @@ -271,7 +258,11 @@ public SharedSlot allocateSharedSlot(JobID jobID, SlotSharingGroupAssignment sha
}
else {
SharedSlot slot = new SharedSlot(
jobID, this, location, nextSlot, taskManagerGateway, sharingGroupAssignment);
this,
location,
nextSlot,
taskManagerGateway,
sharingGroupAssignment);
allocatedSlots.add(slot);
return slot;
}
Expand Down
Loading

0 comments on commit a569f38

Please sign in to comment.