/
JobMasterServiceLeadershipRunner.java
518 lines (447 loc) · 20.4 KB
/
JobMasterServiceLeadershipRunner.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* 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.jobmaster;
import org.apache.flink.api.common.JobID;
import org.apache.flink.api.common.JobStatus;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.runtime.execution.librarycache.LibraryCacheManager;
import org.apache.flink.runtime.highavailability.JobResultStore;
import org.apache.flink.runtime.jobmaster.factories.JobMasterServiceProcessFactory;
import org.apache.flink.runtime.leaderelection.LeaderContender;
import org.apache.flink.runtime.leaderelection.LeaderElectionService;
import org.apache.flink.runtime.messages.Acknowledge;
import org.apache.flink.runtime.messages.webmonitor.JobDetails;
import org.apache.flink.runtime.rpc.FatalErrorHandler;
import org.apache.flink.runtime.scheduler.ExecutionGraphInfo;
import org.apache.flink.util.ExceptionUtils;
import org.apache.flink.util.FlinkException;
import org.apache.flink.util.Preconditions;
import org.apache.flink.util.concurrent.FutureUtils;
import org.apache.flink.util.function.ThrowingRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.concurrent.GuardedBy;
import java.io.IOException;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Supplier;
/**
* Leadership runner for the {@link JobMasterServiceProcess}.
*
* <p>The responsibility of this component is to manage the leadership of the {@link
* JobMasterServiceProcess}. This means that the runner will create an instance of the process when
* it obtains the leadership. The process is stopped once the leadership is revoked.
*
* <p>This component only accepts signals (job result completion, initialization failure) as long as
* it is running and as long as the signals are coming from the current leader process. This ensures
* that only the current leader can affect this component.
*
* <p>All leadership operations are serialized. This means that granting the leadership has to
* complete before the leadership can be revoked and vice versa.
*
* <p>The {@link #resultFuture} can be completed with the following values: * *
*
* <ul>
* <li>{@link JobManagerRunnerResult} to signal an initialization failure of the {@link
* JobMasterService} or the completion of a job
* <li>{@link Exception} to signal an unexpected failure
* </ul>
*/
public class JobMasterServiceLeadershipRunner implements JobManagerRunner, LeaderContender {
private static final Logger LOG =
LoggerFactory.getLogger(JobMasterServiceLeadershipRunner.class);
private final Object lock = new Object();
private final JobMasterServiceProcessFactory jobMasterServiceProcessFactory;
private final LeaderElectionService leaderElectionService;
private final JobResultStore jobResultStore;
private final LibraryCacheManager.ClassLoaderLease classLoaderLease;
private final FatalErrorHandler fatalErrorHandler;
private final CompletableFuture<Void> terminationFuture = new CompletableFuture<>();
private final CompletableFuture<JobManagerRunnerResult> resultFuture =
new CompletableFuture<>();
@GuardedBy("lock")
private State state = State.RUNNING;
@GuardedBy("lock")
private CompletableFuture<Void> sequentialOperation = FutureUtils.completedVoidFuture();
@GuardedBy("lock")
private JobMasterServiceProcess jobMasterServiceProcess =
JobMasterServiceProcess.waitingForLeadership();
@GuardedBy("lock")
private CompletableFuture<JobMasterGateway> jobMasterGatewayFuture = new CompletableFuture<>();
@GuardedBy("lock")
private boolean hasCurrentLeaderBeenCancelled = false;
public JobMasterServiceLeadershipRunner(
JobMasterServiceProcessFactory jobMasterServiceProcessFactory,
LeaderElectionService leaderElectionService,
JobResultStore jobResultStore,
LibraryCacheManager.ClassLoaderLease classLoaderLease,
FatalErrorHandler fatalErrorHandler) {
this.jobMasterServiceProcessFactory = jobMasterServiceProcessFactory;
this.leaderElectionService = leaderElectionService;
this.jobResultStore = jobResultStore;
this.classLoaderLease = classLoaderLease;
this.fatalErrorHandler = fatalErrorHandler;
}
@Override
public CompletableFuture<Void> closeAsync() {
synchronized (lock) {
if (state != State.STOPPED) {
state = State.STOPPED;
LOG.debug("Terminating the leadership runner for job {}.", getJobID());
jobMasterGatewayFuture.completeExceptionally(
new FlinkException(
"JobMasterServiceLeadershipRunner is closed. Therefore, the corresponding JobMaster will never acquire the leadership."));
resultFuture.complete(
JobManagerRunnerResult.forSuccess(
createExecutionGraphInfoWithJobStatus(JobStatus.SUSPENDED)));
final CompletableFuture<Void> processTerminationFuture =
jobMasterServiceProcess.closeAsync();
final CompletableFuture<Void> serviceTerminationFuture =
FutureUtils.runAfterwards(
processTerminationFuture,
() -> {
classLoaderLease.release();
leaderElectionService.stop();
});
FutureUtils.forward(serviceTerminationFuture, terminationFuture);
terminationFuture.whenComplete(
(unused, throwable) ->
LOG.debug(
"Leadership runner for job {} has been terminated.",
getJobID()));
}
}
return terminationFuture;
}
@Override
public void start() throws Exception {
LOG.debug("Start leadership runner for job {}.", getJobID());
leaderElectionService.start(this);
}
@Override
public CompletableFuture<JobMasterGateway> getJobMasterGateway() {
synchronized (lock) {
return jobMasterGatewayFuture;
}
}
@Override
public CompletableFuture<JobManagerRunnerResult> getResultFuture() {
return resultFuture;
}
@Override
public JobID getJobID() {
return jobMasterServiceProcessFactory.getJobId();
}
@Override
public CompletableFuture<Acknowledge> cancel(Time timeout) {
synchronized (lock) {
hasCurrentLeaderBeenCancelled = true;
return getJobMasterGateway()
.thenCompose(jobMasterGateway -> jobMasterGateway.cancel(timeout))
.exceptionally(
e -> {
throw new CompletionException(
new FlinkException(
"Cancellation failed.",
ExceptionUtils.stripCompletionException(e)));
});
}
}
@Override
public CompletableFuture<JobStatus> requestJobStatus(Time timeout) {
return requestJob(timeout)
.thenApply(
executionGraphInfo ->
executionGraphInfo.getArchivedExecutionGraph().getState());
}
@Override
public CompletableFuture<JobDetails> requestJobDetails(Time timeout) {
return requestJob(timeout)
.thenApply(
executionGraphInfo ->
JobDetails.createDetailsForJob(
executionGraphInfo.getArchivedExecutionGraph()));
}
@Override
public CompletableFuture<ExecutionGraphInfo> requestJob(Time timeout) {
synchronized (lock) {
if (state == State.RUNNING) {
if (jobMasterServiceProcess.isInitializedAndRunning()) {
return getJobMasterGateway()
.thenCompose(jobMasterGateway -> jobMasterGateway.requestJob(timeout));
} else {
return CompletableFuture.completedFuture(
createExecutionGraphInfoWithJobStatus(
hasCurrentLeaderBeenCancelled
? JobStatus.CANCELLING
: JobStatus.INITIALIZING));
}
} else {
return resultFuture.thenApply(JobManagerRunnerResult::getExecutionGraphInfo);
}
}
}
@Override
public boolean isInitialized() {
synchronized (lock) {
return jobMasterServiceProcess.isInitializedAndRunning();
}
}
@Override
public void grantLeadership(UUID leaderSessionID) {
runIfStateRunning(
() -> startJobMasterServiceProcessAsync(leaderSessionID),
"starting a new JobMasterServiceProcess");
}
@GuardedBy("lock")
private void startJobMasterServiceProcessAsync(UUID leaderSessionId) {
sequentialOperation =
sequentialOperation.thenRun(
() ->
runIfValidLeader(
leaderSessionId,
ThrowingRunnable.unchecked(
() ->
verifyJobSchedulingStatusAndCreateJobMasterServiceProcess(
leaderSessionId)),
"verify job scheduling status and create JobMasterServiceProcess"));
handleAsyncOperationError(sequentialOperation, "Could not start the job manager.");
}
@GuardedBy("lock")
private void verifyJobSchedulingStatusAndCreateJobMasterServiceProcess(UUID leaderSessionId)
throws FlinkException {
try {
if (jobResultStore.hasJobResultEntry(getJobID())) {
jobAlreadyDone();
} else {
createNewJobMasterServiceProcess(leaderSessionId);
}
} catch (IOException e) {
throw new FlinkException(
String.format(
"Could not retrieve the job scheduling status for job %s.", getJobID()),
e);
}
}
private ExecutionGraphInfo createExecutionGraphInfoWithJobStatus(JobStatus jobStatus) {
return new ExecutionGraphInfo(
jobMasterServiceProcessFactory.createArchivedExecutionGraph(jobStatus, null));
}
private void jobAlreadyDone() {
resultFuture.complete(
JobManagerRunnerResult.forSuccess(
new ExecutionGraphInfo(
jobMasterServiceProcessFactory.createArchivedExecutionGraph(
JobStatus.FAILED,
new JobAlreadyDoneException(getJobID())))));
}
@GuardedBy("lock")
private void createNewJobMasterServiceProcess(UUID leaderSessionId) throws FlinkException {
Preconditions.checkState(jobMasterServiceProcess.closeAsync().isDone());
LOG.debug(
"Create new JobMasterServiceProcess because we were granted leadership under {}.",
leaderSessionId);
jobMasterServiceProcess = jobMasterServiceProcessFactory.create(leaderSessionId);
forwardIfValidLeader(
leaderSessionId,
jobMasterServiceProcess.getJobMasterGatewayFuture(),
jobMasterGatewayFuture,
"JobMasterGatewayFuture from JobMasterServiceProcess");
forwardResultFuture(leaderSessionId, jobMasterServiceProcess.getResultFuture());
confirmLeadership(leaderSessionId, jobMasterServiceProcess.getLeaderAddressFuture());
}
private void confirmLeadership(
UUID leaderSessionId, CompletableFuture<String> leaderAddressFuture) {
FutureUtils.assertNoException(
leaderAddressFuture.thenAccept(
address -> {
synchronized (lock) {
if (isValidLeader(leaderSessionId)) {
LOG.debug("Confirm leadership {}.", leaderSessionId);
leaderElectionService.confirmLeadership(
leaderSessionId, address);
} else {
LOG.trace(
"Ignore confirming leadership because the leader {} is no longer valid.",
leaderSessionId);
}
}
}));
}
private void forwardResultFuture(
UUID leaderSessionId, CompletableFuture<JobManagerRunnerResult> resultFuture) {
resultFuture.whenComplete(
(jobManagerRunnerResult, throwable) -> {
synchronized (lock) {
if (isValidLeader(leaderSessionId)) {
onJobCompletion(jobManagerRunnerResult, throwable);
} else {
LOG.trace(
"Ignore result future forwarding because the leader {} is no longer valid.",
leaderSessionId);
}
}
});
}
@GuardedBy("lock")
private void onJobCompletion(
JobManagerRunnerResult jobManagerRunnerResult, Throwable throwable) {
state = State.JOB_COMPLETED;
LOG.debug("Completing the result for job {}.", getJobID());
if (throwable != null) {
resultFuture.completeExceptionally(throwable);
jobMasterGatewayFuture.completeExceptionally(
new FlinkException(
"Could not retrieve JobMasterGateway because the JobMaster failed.",
throwable));
} else {
if (!jobManagerRunnerResult.isSuccess()) {
jobMasterGatewayFuture.completeExceptionally(
new FlinkException(
"Could not retrieve JobMasterGateway because the JobMaster initialization failed.",
jobManagerRunnerResult.getInitializationFailure()));
}
resultFuture.complete(jobManagerRunnerResult);
}
}
@Override
public void revokeLeadership() {
runIfStateRunning(
this::stopJobMasterServiceProcessAsync,
"revoke leadership from JobMasterServiceProcess");
}
@GuardedBy("lock")
private void stopJobMasterServiceProcessAsync() {
sequentialOperation =
sequentialOperation.thenCompose(
ignored ->
callIfRunning(
this::stopJobMasterServiceProcess,
"stop leading JobMasterServiceProcess")
.orElse(FutureUtils.completedVoidFuture()));
handleAsyncOperationError(sequentialOperation, "Could not suspend the job manager.");
}
@GuardedBy("lock")
private CompletableFuture<Void> stopJobMasterServiceProcess() {
LOG.debug("Stop current JobMasterServiceProcess because the leadership has been revoked.");
jobMasterGatewayFuture.completeExceptionally(
new FlinkException(
"Cannot obtain JobMasterGateway because the JobMaster lost leadership."));
jobMasterGatewayFuture = new CompletableFuture<>();
hasCurrentLeaderBeenCancelled = false;
return jobMasterServiceProcess.closeAsync();
}
@Override
public void handleError(Exception exception) {
fatalErrorHandler.onFatalError(exception);
}
private void handleAsyncOperationError(CompletableFuture<Void> operation, String message) {
operation.whenComplete(
(unused, throwable) -> {
if (throwable != null) {
runIfStateRunning(
() ->
handleJobMasterServiceLeadershipRunnerError(
new FlinkException(message, throwable)),
"handle JobMasterServiceLeadershipRunner error");
}
});
}
private void handleJobMasterServiceLeadershipRunnerError(Throwable cause) {
if (ExceptionUtils.isJvmFatalError(cause)) {
fatalErrorHandler.onFatalError(cause);
} else {
resultFuture.completeExceptionally(cause);
}
}
private void runIfStateRunning(Runnable action, String actionDescription) {
synchronized (lock) {
if (isRunning()) {
action.run();
} else {
LOG.trace(
"Ignore '{}' because the leadership runner is no longer running.",
actionDescription);
}
}
}
private <T> Optional<T> callIfRunning(
Supplier<? extends T> supplier, String supplierDescription) {
synchronized (lock) {
if (isRunning()) {
return Optional.of(supplier.get());
} else {
LOG.trace(
"Ignore '{}' because the leadership runner is no longer running.",
supplierDescription);
return Optional.empty();
}
}
}
@GuardedBy("lock")
private boolean isRunning() {
return state == State.RUNNING;
}
private void runIfValidLeader(
UUID expectedLeaderId, Runnable action, String actionDescription) {
synchronized (lock) {
if (isValidLeader(expectedLeaderId)) {
action.run();
} else {
LOG.trace(
"Ignore leader action '{}' because the leadership runner is no longer the valid leader for {}.",
actionDescription,
expectedLeaderId);
}
}
}
@GuardedBy("lock")
private boolean isValidLeader(UUID expectedLeaderId) {
return isRunning() && leaderElectionService.hasLeadership(expectedLeaderId);
}
private <T> void forwardIfValidLeader(
UUID expectedLeaderId,
CompletableFuture<? extends T> source,
CompletableFuture<T> target,
String forwardDescription) {
source.whenComplete(
(t, throwable) -> {
synchronized (lock) {
if (isValidLeader(expectedLeaderId)) {
if (throwable != null) {
target.completeExceptionally(throwable);
} else {
target.complete(t);
}
} else {
LOG.trace(
"Ignore forwarding '{}' because the leadership runner is no longer the valid leader for {}.",
forwardDescription,
expectedLeaderId);
}
}
});
}
enum State {
RUNNING,
STOPPED,
JOB_COMPLETED,
}
}