-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
GridJobWorker.java
891 lines (725 loc) · 28.2 KB
/
GridJobWorker.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
/*
* 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.ignite.internal.processors.job;
import org.apache.ignite.*;
import org.apache.ignite.cluster.*;
import org.apache.ignite.compute.*;
import org.apache.ignite.events.*;
import org.apache.ignite.fs.*;
import org.apache.ignite.internal.*;
import org.apache.ignite.lang.*;
import org.apache.ignite.marshaller.*;
import org.apache.ignite.internal.managers.deployment.*;
import org.apache.ignite.internal.processors.task.*;
import org.apache.ignite.internal.processors.timeout.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.internal.util.worker.*;
import org.jetbrains.annotations.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import static org.apache.ignite.events.IgniteEventType.*;
import static org.apache.ignite.internal.GridTopic.*;
import static org.apache.ignite.internal.managers.communication.GridIoPolicy.*;
/**
* Job worker.
*/
public class GridJobWorker extends GridWorker implements GridTimeoutObject {
/** Per-thread held flag. */
private static final ThreadLocal<Boolean> HOLD = new ThreadLocal<Boolean>() {
@Override protected Boolean initialValue() {
return false;
}
};
/** Static logger to avoid re-creation. */
private static final AtomicReference<IgniteLogger> logRef = new AtomicReference<>();
/** */
private final long createTime;
/** */
private volatile long startTime;
/** */
private volatile long finishTime;
/** */
private final GridKernalContext ctx;
/** */
private final Object jobTopic;
/** */
private final Object taskTopic;
/** */
private byte[] jobBytes;
/** Task originating node. */
private final ClusterNode taskNode;
/** Flag set when visor or internal task is running. */
private final boolean internal;
/** */
private final IgniteLogger log;
/** */
private final IgniteMarshaller marsh;
/** */
private final GridJobSessionImpl ses;
/** */
private final GridJobContextImpl jobCtx;
/** */
private final GridJobEventListener evtLsnr;
/** Deployment. */
private final GridDeployment dep;
/** */
private final AtomicBoolean finishing = new AtomicBoolean();
/** Guard ensuring that master-leave callback is not execute more than once. */
private final AtomicBoolean masterLeaveGuard = new AtomicBoolean();
/** */
private volatile boolean timedOut;
/** */
private volatile boolean sysCancelled;
/** */
private volatile boolean sysStopping;
/** */
private volatile boolean isStarted;
/** Deployed job. */
private ComputeJob job;
/** Halted flag (if greater than 0, job is halted). */
private final AtomicInteger held = new AtomicInteger();
/** Hold/unhold listener to notify job processor. */
private final GridJobHoldListener holdLsnr;
/**
* @param ctx Kernal context.
* @param dep Grid deployment.
* @param createTime Create time.
* @param ses Grid task session.
* @param jobCtx Job context.
* @param jobBytes Grid job bytes.
* @param job Job.
* @param taskNode Grid task node.
* @param internal Whether or not task was marked with {@link GridInternal}
* @param evtLsnr Job event listener.
* @param holdLsnr Hold listener.
*/
GridJobWorker(
GridKernalContext ctx,
GridDeployment dep,
long createTime,
GridJobSessionImpl ses,
GridJobContextImpl jobCtx,
byte[] jobBytes,
ComputeJob job,
ClusterNode taskNode,
boolean internal,
GridJobEventListener evtLsnr,
GridJobHoldListener holdLsnr) {
super(ctx.gridName(), "grid-job-worker", ctx.log());
assert ctx != null;
assert ses != null;
assert jobCtx != null;
assert taskNode != null;
assert evtLsnr != null;
assert dep != null;
assert holdLsnr != null;
this.ctx = ctx;
this.createTime = createTime;
this.evtLsnr = evtLsnr;
this.dep = dep;
this.ses = ses;
this.jobCtx = jobCtx;
this.jobBytes = jobBytes;
this.taskNode = taskNode;
this.internal = internal;
this.holdLsnr = holdLsnr;
if (job != null)
this.job = job;
log = U.logger(ctx, logRef, this);
marsh = ctx.config().getMarshaller();
UUID locNodeId = ctx.discovery().localNode().id();
jobTopic = TOPIC_JOB.topic(ses.getJobId(), locNodeId);
taskTopic = TOPIC_TASK.topic(ses.getJobId(), locNodeId);
}
/**
* Gets deployed job or {@code null} of job could not be deployed.
*
* @return Deployed job.
*/
@Nullable public ComputeJob getJob() {
return job;
}
/**
* @return Deployed task.
*/
public GridDeployment getDeployment() {
return dep;
}
/**
* Returns {@code True} if job was cancelled by the system.
*
* @return {@code True} if job was cancelled by the system.
*/
boolean isSystemCanceled() {
return sysCancelled;
}
/**
* @return Create time.
*/
long getCreateTime() {
return createTime;
}
/**
* @return Unique job ID.
*/
public IgniteUuid getJobId() {
IgniteUuid jobId = ses.getJobId();
assert jobId != null;
return jobId;
}
/**
* @return Job context.
*/
public ComputeJobContext getJobContext() {
return jobCtx;
}
/**
* @return Job communication topic.
*/
Object getJobTopic() {
return jobTopic;
}
/**
* @return Task communication topic.
*/
Object getTaskTopic() {
return taskTopic;
}
/**
* @return Session.
*/
public GridJobSessionImpl getSession() {
return ses;
}
/**
* Gets job finishing state.
*
* @return {@code true} if job is being finished after execution
* and {@code false} otherwise.
*/
boolean isFinishing() {
return finishing.get();
}
/**
* @return Parent task node ID.
*/
ClusterNode getTaskNode() {
return taskNode;
}
/**
* @return Job execution time.
*/
long getExecuteTime() {
long startTime0 = startTime;
long finishTime0 = finishTime;
return startTime0 == 0 ? 0 : finishTime0 == 0 ?
U.currentTimeMillis() - startTime0 : finishTime0 - startTime0;
}
/**
* @return Time job spent on waiting queue.
*/
long getQueuedTime() {
long startTime0 = startTime;
return startTime0 == 0 ? U.currentTimeMillis() - createTime : startTime0 - createTime;
}
/** {@inheritDoc} */
@Override public long endTime() {
return ses.getEndTime();
}
/** {@inheritDoc} */
@Override public IgniteUuid timeoutId() {
IgniteUuid jobId = ses.getJobId();
assert jobId != null;
return jobId;
}
/**
* @return {@code True} if job is timed out.
*/
boolean isTimedOut() {
return timedOut;
}
/**
* @return {@code True} if parent task is internal or Visor-related.
*/
public boolean isInternal() {
return internal;
}
/** {@inheritDoc} */
@Override public void onTimeout() {
if (finishing.get())
return;
timedOut = true;
U.warn(log, "Job has timed out: " + ses);
cancel();
if (!internal && ctx.event().isRecordable(EVT_JOB_TIMEDOUT))
recordEvent(EVT_JOB_TIMEDOUT, "Job has timed out: " + job);
}
/**
* Callback for whenever grid is stopping.
*/
public void onStopping() {
sysStopping = true;
}
/**
* @return {@code True} if job was halted.
*/
public boolean held() {
return held.get() > 0;
}
/**
* Sets halt flags.
*/
public void hold() {
held.incrementAndGet();
HOLD.set(true);
holdLsnr.onHold(this);
}
/**
* Initializes job. Handles deployments and event recording.
*
* @param dep Job deployed task.
* @param taskCls Task class.
* @return {@code True} if job was successfully initialized.
*/
boolean initialize(GridDeployment dep, Class<?> taskCls) {
assert dep != null;
IgniteException ex = null;
try {
if (job == null) {
job = marsh.unmarshal(jobBytes, dep.classLoader());
// No need to hold reference any more.
jobBytes = null;
}
// Inject resources.
ctx.resource().inject(dep, taskCls, job, ses, jobCtx);
if (!internal && ctx.event().isRecordable(EVT_JOB_QUEUED))
recordEvent(EVT_JOB_QUEUED, "Job got queued for computation.");
}
catch (IgniteCheckedException e) {
U.error(log, "Failed to initialize job [jobId=" + ses.getJobId() + ", ses=" + ses + ']', e);
ex = new IgniteException(e);
}
catch (Throwable e) {
ex = handleThrowable(e);
assert ex != null;
}
finally {
if (ex != null)
finishJob(null, ex, true);
}
return ex == null;
}
/** {@inheritDoc} */
@Override protected void body() {
assert job != null;
startTime = U.currentTimeMillis();
isStarted = true;
// Event notification.
evtLsnr.onJobStarted(this);
if (!internal && ctx.event().isRecordable(EVT_JOB_STARTED))
recordEvent(EVT_JOB_STARTED, /*no message for success*/null);
execute0(true);
}
/**
* Executes the job.
*/
public void execute() {
execute0(false);
}
/**
* @param skipNtf {@code True} to skip job processor {@code onUnhold()}
* notification (only from {@link #body()}).
*/
private void execute0(boolean skipNtf) {
// Make sure flag is not set for current thread.
HOLD.set(false);
if (isCancelled())
// If job was cancelled prior to assigning runner to it?
super.cancel();
if (!skipNtf) {
holdLsnr.onUnhold(this);
int c = held.decrementAndGet();
if (c > 0) {
if (log.isDebugEnabled())
log.debug("Ignoring job execution (job was held several times) [c=" + c + ']');
return;
}
}
boolean sndRes = true;
Object res = null;
IgniteException ex = null;
try {
ctx.job().currentTaskSession(ses);
// If job has timed out, then
// avoid computation altogether.
if (isTimedOut())
sndRes = false;
else {
res = U.wrapThreadLoader(dep.classLoader(), new Callable<Object>() {
@Nullable @Override public Object call() throws IgniteCheckedException {
try {
if (internal && ctx.config().isPeerClassLoadingEnabled())
ctx.job().internal(true);
return job.execute();
}
finally {
if (internal && ctx.config().isPeerClassLoadingEnabled())
ctx.job().internal(false);
}
}
});
if (log.isDebugEnabled())
log.debug("Job execution has successfully finished [job=" + job + ", res=" + res + ']');
}
}
catch (IgniteException e) {
if (sysStopping && e.hasCause(IgniteInterruptedCheckedException.class, InterruptedException.class)) {
ex = handleThrowable(e);
assert ex != null;
}
else {
if (X.hasCause(e, GridInternalException.class) || X.hasCause(e, IgniteFsOutOfSpaceException.class)) {
// Print exception for internal errors only if debug is enabled.
if (log.isDebugEnabled())
U.error(log, "Failed to execute job [jobId=" + ses.getJobId() + ", ses=" + ses + ']', e);
}
else if (X.hasCause(e, InterruptedException.class)) {
String msg = "Job was cancelled [jobId=" + ses.getJobId() + ", ses=" + ses + ']';
if (log.isDebugEnabled())
U.error(log, msg, e);
else
U.warn(log, msg);
}
else
U.error(log, "Failed to execute job [jobId=" + ses.getJobId() + ", ses=" + ses + ']', e);
ex = e;
}
}
// Catch Throwable to protect against bad user code except
// InterruptedException if job is being cancelled.
catch (Throwable e) {
ex = handleThrowable(e);
assert ex != null;
}
finally {
// Finish here only if not held by this thread.
if (!HOLD.get())
finishJob(res, ex, sndRes);
ctx.job().currentTaskSession(null);
}
}
/**
* Handles {@link Throwable} generic exception for task
* deployment and execution.
*
* @param e Exception.
* @return Wrapped exception.
*/
private IgniteException handleThrowable(Throwable e) {
String msg = null;
IgniteException ex = null;
// Special handling for weird interrupted exception which
// happens due to JDk 1.5 bug.
if (e instanceof InterruptedException && !sysStopping) {
msg = "Failed to execute job due to interrupted exception.";
// Turn interrupted exception into checked exception.
ex = new IgniteException(msg, e);
}
// Special 'NoClassDefFoundError' handling if P2P is on. We had many questions
// about this exception and decided to change error message.
else if ((e instanceof NoClassDefFoundError || e instanceof ClassNotFoundException)
&& ctx.config().isPeerClassLoadingEnabled()) {
msg = "Failed to execute job due to class or resource loading exception (make sure that task " +
"originating node is still in grid and requested class is in the task class path) [jobId=" +
ses.getJobId() + ", ses=" + ses + ']';
ex = new ComputeUserUndeclaredException(msg, e);
}
else if (sysStopping && X.hasCause(e, InterruptedException.class, IgniteInterruptedCheckedException.class)) {
msg = "Job got interrupted due to system stop (will attempt failover).";
ex = new ComputeExecutionRejectedException(e);
}
if (msg == null) {
msg = "Failed to execute job due to unexpected runtime exception [jobId=" + ses.getJobId() +
", ses=" + ses + ']';
ex = new ComputeUserUndeclaredException(msg, e);
}
assert msg != null;
assert ex != null;
U.error(log, msg, e);
return ex;
}
/** {@inheritDoc} */
@Override public void cancel() {
cancel(false);
}
/**
* @param sys System flag.
*/
public void cancel(boolean sys) {
try {
super.cancel();
final ComputeJob job0 = job;
if (sys)
sysCancelled = true;
if (job0 != null) {
if (log.isDebugEnabled())
log.debug("Cancelling job: " + ses);
U.wrapThreadLoader(dep.classLoader(), new IgniteRunnable() {
@Override public void run() {
job0.cancel();
}
});
}
if (!internal && ctx.event().isRecordable(EVT_JOB_CANCELLED))
recordEvent(EVT_JOB_CANCELLED, "Job was cancelled: " + job0);
}
// Catch throwable to protect against bad user code.
catch (Throwable e) {
U.error(log, "Failed to cancel job due to undeclared user exception [jobId=" + ses.getJobId() +
", ses=" + ses + ']', e);
}
}
/**
* @param evtType Event type.
* @param msg Message.
*/
private void recordEvent(int evtType, @Nullable String msg) {
assert ctx.event().isRecordable(evtType);
assert !internal;
IgniteJobEvent evt = new IgniteJobEvent();
evt.jobId(ses.getJobId());
evt.message(msg);
evt.node(ctx.discovery().localNode());
evt.taskName(ses.getTaskName());
evt.taskClassName(ses.getTaskClassName());
evt.taskSessionId(ses.getId());
evt.type(evtType);
evt.taskNode(taskNode);
evt.taskSubjectId(ses.subjectId());
ctx.event().record(evt);
}
/**
* @param res Result.
* @param ex Error.
* @param sndReply If {@code true}, reply will be sent.
*/
void finishJob(@Nullable Object res,
@Nullable IgniteException ex,
boolean sndReply)
{
// Avoid finishing a job more than once from different threads.
if (!finishing.compareAndSet(false, true))
return;
// Do not send reply if job has been cancelled from system.
if (sndReply)
sndReply = !sysCancelled;
// We should save message ID here since listener callback will reset sequence.
ClusterNode sndNode = ctx.discovery().node(taskNode.id());
finishTime = U.currentTimeMillis();
Collection<IgniteBiTuple<Integer, String>> evts = null;
try {
if (ses.isFullSupport())
evtLsnr.onBeforeJobResponseSent(this);
// Send response back only if job has not timed out.
if (!isTimedOut()) {
if (sndReply) {
if (sndNode == null) {
onMasterNodeLeft();
U.warn(log, "Failed to reply to sender node because it left grid [nodeId=" + taskNode.id() +
", ses=" + ses + ", jobId=" + ses.getJobId() + ", job=" + job + ']');
// Record job reply failure.
if (!internal && ctx.event().isRecordable(EVT_JOB_FAILED))
evts = addEvent(evts, EVT_JOB_FAILED, "Job reply failed (task node left grid): " + job);
}
else {
try {
if (ex != null) {
if (isStarted) {
// Job failed.
if (!internal && ctx.event().isRecordable(EVT_JOB_FAILED))
evts = addEvent(evts, EVT_JOB_FAILED, "Job failed due to exception [ex=" +
ex + ", job=" + job + ']');
}
else if (!internal && ctx.event().isRecordable(EVT_JOB_REJECTED))
evts = addEvent(evts, EVT_JOB_REJECTED, "Job has not been started " +
"[ex=" + ex + ", job=" + job + ']');
}
else if (!internal && ctx.event().isRecordable(EVT_JOB_FINISHED))
evts = addEvent(evts, EVT_JOB_FINISHED, /*no message for success. */null);
boolean loc = ctx.localNodeId().equals(sndNode.id()) && !ctx.config().isMarshalLocalJobs();
Map<Object, Object> attrs = jobCtx.getAttributes();
GridJobExecuteResponse jobRes = new GridJobExecuteResponse(
ctx.localNodeId(),
ses.getId(),
ses.getJobId(),
loc ? null : marsh.marshal(ex),
loc ? ex : null,
loc ? null: marsh.marshal(res),
loc ? res : null,
loc ? null : marsh.marshal(attrs),
loc ? attrs : null,
isCancelled());
long timeout = ses.getEndTime() - U.currentTimeMillis();
if (timeout <= 0)
// Ignore the actual timeout and send response anyway.
timeout = 1;
if (ses.isFullSupport()) {
// Send response to designated job topic.
// Always go through communication to preserve order,
// if attributes are enabled.
ctx.io().sendOrderedMessage(
sndNode,
taskTopic,
jobRes,
internal ? MANAGEMENT_POOL : SYSTEM_POOL,
timeout,
false);
}
else if (ctx.localNodeId().equals(sndNode.id()))
ctx.task().processJobExecuteResponse(ctx.localNodeId(), jobRes);
else
// Send response to common topic as unordered message.
ctx.io().send(sndNode, TOPIC_TASK, jobRes, internal ? MANAGEMENT_POOL : SYSTEM_POOL);
}
catch (IgniteCheckedException e) {
// Log and invoke the master-leave callback.
if (isDeadNode(taskNode.id())) {
onMasterNodeLeft();
// Avoid stack trace for left nodes.
U.warn(log, "Failed to reply to sender node because it left grid " +
"[nodeId=" + taskNode.id() + ", jobId=" + ses.getJobId() +
", ses=" + ses + ", job=" + job + ']');
}
else
U.error(log, "Error sending reply for job [nodeId=" + sndNode.id() + ", jobId=" +
ses.getJobId() + ", ses=" + ses + ", job=" + job + ']', e);
if (!internal && ctx.event().isRecordable(EVT_JOB_FAILED))
evts = addEvent(evts, EVT_JOB_FAILED, "Failed to send reply for job [nodeId=" +
taskNode.id() + ", job=" + job + ']');
}
// Catching interrupted exception because
// it gets thrown for some reason.
catch (Exception e) {
String msg = "Failed to send reply for job [nodeId=" + taskNode.id() + ", job=" + job + ']';
U.error(log, msg, e);
if (!internal && ctx.event().isRecordable(EVT_JOB_FAILED))
evts = addEvent(evts, EVT_JOB_FAILED, msg);
}
}
}
else {
if (ex != null) {
if (isStarted) {
if (!internal && ctx.event().isRecordable(EVT_JOB_FAILED))
evts = addEvent(evts, EVT_JOB_FAILED, "Job failed due to exception [ex=" + ex +
", job=" + job + ']');
}
else if (!internal && ctx.event().isRecordable(EVT_JOB_REJECTED))
evts = addEvent(evts, EVT_JOB_REJECTED, "Job has not been started [ex=" + ex +
", job=" + job + ']');
}
else if (!internal && ctx.event().isRecordable(EVT_JOB_FINISHED))
evts = addEvent(evts, EVT_JOB_FINISHED, /*no message for success. */null);
}
}
// Job timed out.
else if (!internal && ctx.event().isRecordable(EVT_JOB_FAILED))
evts = addEvent(evts, EVT_JOB_FAILED, "Job failed due to timeout: " + job);
}
finally {
if (evts != null) {
for (IgniteBiTuple<Integer, String> t : evts)
recordEvent(t.get1(), t.get2());
}
// Listener callback.
evtLsnr.onJobFinished(this);
}
}
/**
* If the job implements {@link org.apache.ignite.compute.ComputeJobMasterLeaveAware#onMasterNodeLeft} interface then invoke
* {@link org.apache.ignite.compute.ComputeJobMasterLeaveAware#onMasterNodeLeft(org.apache.ignite.compute.ComputeTaskSession)} method.
*
* @return {@code True} if master leave has been handled (either by this call or before).
*/
boolean onMasterNodeLeft() {
if (job instanceof ComputeJobMasterLeaveAware) {
if (masterLeaveGuard.compareAndSet(false, true)) {
try {
((ComputeJobMasterLeaveAware)job).onMasterNodeLeft(ses.session());
if (log.isDebugEnabled())
log.debug("Successfully executed GridComputeJobMasterLeaveAware.onMasterNodeLeft() callback " +
"[nodeId=" + taskNode.id() + ", jobId=" + ses.getJobId() + ", job=" + job + ']');
}
catch (Exception e) {
U.error(log, "Failed to execute GridComputeJobMasterLeaveAware.onMasterNodeLeft() callback " +
"[nodeId=" + taskNode.id() + ", jobId=" + ses.getJobId() + ", job=" + job + ']', e);
}
}
return true;
}
return false;
}
/**
* @param evts Collection (created if {@code null}).
* @param evt Event.
* @param msg Message (optional).
* @return Collection with event added.
*/
Collection<IgniteBiTuple<Integer, String>> addEvent(@Nullable Collection<IgniteBiTuple<Integer, String>> evts,
Integer evt, @Nullable String msg) {
assert ctx.event().isRecordable(evt);
assert !internal;
if (evts == null)
evts = new ArrayList<>();
evts.add(F.t(evt, msg));
return evts;
}
/**
* Checks whether node is alive or dead.
*
* @param uid UID of node to check.
* @return {@code true} if node is dead, {@code false} is node is alive.
*/
private boolean isDeadNode(UUID uid) {
return ctx.discovery().node(uid) == null || !ctx.discovery().pingNode(uid);
}
/** {@inheritDoc} */
@Override public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
assert obj instanceof GridJobWorker;
IgniteUuid jobId1 = ses.getJobId();
IgniteUuid jobId2 = ((GridJobWorker)obj).ses.getJobId();
assert jobId1 != null;
assert jobId2 != null;
return jobId1.equals(jobId2);
}
/** {@inheritDoc} */
@Override public int hashCode() {
IgniteUuid jobId = ses.getJobId();
assert jobId != null;
return jobId.hashCode();
}
/** {@inheritDoc} */
@Override public String toString() {
return S.toString(GridJobWorker.class, this);
}
}