forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AllocatedSlot.java
287 lines (239 loc) · 8.97 KB
/
AllocatedSlot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* 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.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 org.apache.flink.util.Preconditions;
import java.util.concurrent.atomic.AtomicReference;
import static org.apache.flink.util.Preconditions.checkNotNull;
/**
* The {@code AllocatedSlot} represents a slot that the JobManager allocated from a TaskManager.
* It represents a slice of allocated resources from the TaskManager.
*
* <p>To allocate an {@code AllocatedSlot}, the requests a slot from the ResourceManager. The
* ResourceManager picks (or starts) a TaskManager that will then allocate the slot to the
* JobManager and notify the JobManager.
*
* <p>Note: Prior to the resource management changes introduced in (Flink Improvement Proposal 6),
* 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 {
/** The ID under which the slot is allocated. Uniquely identifies the slot. */
private final AllocationID allocationId;
/** The location information of the TaskManager to which this slot belongs */
private final TaskManagerLocation taskManagerLocation;
/** The resource profile of the slot provides */
private final ResourceProfile resourceProfile;
/** RPC gateway to call the TaskManager that holds this slot */
private final TaskManagerGateway taskManagerGateway;
/** The number of the slot on the TaskManager to which slot belongs. Purely informational. */
private final int physicalSlotNumber;
private final SlotOwner slotOwner;
private final AtomicReference<LogicalSlot> logicalSlotReference;
// ------------------------------------------------------------------------
public AllocatedSlot(
AllocationID allocationId,
TaskManagerLocation location,
int physicalSlotNumber,
ResourceProfile resourceProfile,
TaskManagerGateway taskManagerGateway,
SlotOwner slotOwner) {
this.allocationId = checkNotNull(allocationId);
this.taskManagerLocation = checkNotNull(location);
this.physicalSlotNumber = physicalSlotNumber;
this.resourceProfile = checkNotNull(resourceProfile);
this.taskManagerGateway = checkNotNull(taskManagerGateway);
this.slotOwner = checkNotNull(slotOwner);
logicalSlotReference = new AtomicReference<>(null);
}
// ------------------------------------------------------------------------
/**
* Gets the ID under which the slot is allocated, which uniquely identifies the slot.
*
* @return The ID under which the slot is allocated
*/
public AllocationID getAllocationId() {
return allocationId;
}
/**
* Gets the ID of the TaskManager on which this slot was allocated.
*
* <p>This is equivalent to {@link #getTaskManagerLocation()#getTaskManagerId()}.
*
* @return This slot's TaskManager's ID.
*/
public ResourceID getTaskManagerId() {
return getTaskManagerLocation().getResourceID();
}
/**
* Gets the resource profile of the slot.
*
* @return The resource profile of the slot.
*/
public ResourceProfile getResourceProfile() {
return resourceProfile;
}
/**
* Gets the location info of the TaskManager that offers this slot.
*
* @return The location info of the TaskManager that offers this slot
*/
public TaskManagerLocation getTaskManagerLocation() {
return taskManagerLocation;
}
/**
* Gets the actor gateway that can be used to send messages to the TaskManager.
* <p>
* This method should be removed once the new interface-based RPC abstraction is in place
*
* @return The actor gateway that can be used to send messages to the TaskManager.
*/
public TaskManagerGateway getTaskManagerGateway() {
return taskManagerGateway;
}
/**
* Returns true if this slot is not being used (e.g. a logical slot is allocated from this slot).
*
* @return true if a logical slot is allocated from this slot, otherwise false
*/
public boolean isUsed() {
return logicalSlotReference.get() != null;
}
/**
* 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}.
*
* @param slotRequestId identifying the corresponding slot request
* @param locality specifying the locality of the allocated slot
* @return an allocated logical simple slot
* @throws SlotException if we could not allocate a simple slot
*/
public SimpleSlot allocateSimpleSlot(SlotRequestID slotRequestId, Locality locality) throws SlotException {
final AllocatedSlotContext allocatedSlotContext = new AllocatedSlotContext(
slotRequestId);
final SimpleSlot simpleSlot = new SimpleSlot(allocatedSlotContext, 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 slotRequestId identifying the corresponding slot request
* @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(SlotRequestID slotRequestId, SlotSharingGroupAssignment slotSharingGroupAssignment) throws SlotException {
final AllocatedSlotContext allocatedSlotContext = new AllocatedSlotContext(
slotRequestId);
final SharedSlot sharedSlot = new SharedSlot(allocatedSlotContext, 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.");
}
}
// ------------------------------------------------------------------------
/**
* This always returns a reference hash code.
*/
@Override
public final int hashCode() {
return super.hashCode();
}
/**
* This always checks based on reference equality.
*/
@Override
public final boolean equals(Object obj) {
return this == obj;
}
@Override
public String toString() {
return "AllocatedSlot " + allocationId + " @ " + taskManagerLocation + " - " + physicalSlotNumber;
}
/**
* Slot context for {@link AllocatedSlot}.
*/
private final class AllocatedSlotContext implements SlotContext {
private final SlotRequestID slotRequestId;
private AllocatedSlotContext(SlotRequestID slotRequestId) {
this.slotRequestId = Preconditions.checkNotNull(slotRequestId);
}
@Override
public SlotRequestID getSlotRequestId() {
return slotRequestId;
}
@Override
public AllocationID getAllocationId() {
return allocationId;
}
@Override
public TaskManagerLocation getTaskManagerLocation() {
return taskManagerLocation;
}
@Override
public int getPhysicalSlotNumber() {
return physicalSlotNumber;
}
@Override
public TaskManagerGateway getTaskManagerGateway() {
return taskManagerGateway;
}
}
}