/
SimpleSlot.java
124 lines (103 loc) · 3.72 KB
/
SimpleSlot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.runtime.instance;
import org.apache.flink.util.AbstractID;
import org.apache.flink.runtime.executiongraph.Execution;
import org.apache.flink.api.common.JobID;
import org.apache.flink.runtime.jobmanager.scheduler.Locality;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
/**
* Class which represents a single slot on a machine or within a shared slot. If this slot is part
* of a [[SharedSlot]], then its parent attribute is set to this instance. If not, then the parent
* attribute is null.
*
* IMPORTANT: This class has no synchronization. Thus it has to be synchronized by the calling
* object.
*/
public class SimpleSlot extends Slot {
private static final AtomicReferenceFieldUpdater<SimpleSlot, Execution> VERTEX_UPDATER =
AtomicReferenceFieldUpdater.newUpdater(SimpleSlot.class, Execution.class, "executedTask");
/** Task being executed in the slot. Volatile to force a memory barrier and allow for correct double-checking */
private volatile Execution executedTask;
private Locality locality = Locality.UNCONSTRAINED;
public SimpleSlot(JobID jobID, Instance instance, int slotNumber, SharedSlot parent, AbstractID groupID){
super(jobID, instance, slotNumber, parent, groupID);
}
@Override
public int getNumberLeaves() {
return 1;
}
public Execution getExecution() {
return executedTask;
}
public Locality getLocality() {
return locality;
}
public void setLocality(Locality locality) {
this.locality = locality;
}
public boolean setExecutedVertex(Execution executedVertex) {
if (executedVertex == null) {
throw new NullPointerException();
}
// check that we can actually run in this slot
if (getStatus() != ALLOCATED_AND_ALIVE) {
return false;
}
// atomically assign the vertex
if (!VERTEX_UPDATER.compareAndSet(this, null, executedVertex)) {
return false;
}
// we need to do a double check that we were not cancelled in the meantime
if (getStatus() != ALLOCATED_AND_ALIVE) {
this.executedTask = null;
return false;
}
return true;
}
@Override
public void cancel() {
if (markCancelled()) {
// kill all tasks currently running in this slot
Execution exec = this.executedTask;
if (exec != null && !exec.isFinished()) {
exec.fail(new Exception("The slot in which the task was scheduled has been killed (probably loss of TaskManager). Instance:"+getInstance()));
}
}
}
@Override
public void releaseSlot() {
// cancel everything, if there is something. since this is atomically status based,
// it will not happen twice if another attempt happened before or concurrently
try {
cancel();
} finally {
if (getParent() != null) {
// we have to ask our parent to dispose us
getParent().disposeChild(this);
} else {
// we have to give back the slot to the owning instance
getInstance().returnAllocatedSlot(this);
}
}
}
@Override
public String toString() {
return "SimpleSlot " + super.toString();
}
}