-
Notifications
You must be signed in to change notification settings - Fork 13k
/
SlotPoolImplTest.java
840 lines (659 loc) · 30.8 KB
/
SlotPoolImplTest.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
/*
* 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.slotpool;
import org.apache.flink.api.common.JobID;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.core.testutils.OneShotLatch;
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.concurrent.ComponentMainThreadExecutor;
import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutorServiceAdapter;
import org.apache.flink.runtime.concurrent.FutureUtils;
import org.apache.flink.runtime.executiongraph.utils.SimpleAckingTaskManagerGateway;
import org.apache.flink.runtime.jobmanager.scheduler.Locality;
import org.apache.flink.runtime.jobmanager.slots.DummySlotOwner;
import org.apache.flink.runtime.jobmaster.AllocatedSlotInfo;
import org.apache.flink.runtime.jobmaster.AllocatedSlotReport;
import org.apache.flink.runtime.jobmaster.JobMasterId;
import org.apache.flink.runtime.jobmaster.SlotRequestId;
import org.apache.flink.runtime.messages.Acknowledge;
import org.apache.flink.runtime.resourcemanager.SlotRequest;
import org.apache.flink.runtime.taskexecutor.TaskExecutor;
import org.apache.flink.runtime.taskexecutor.slot.SlotOffer;
import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation;
import org.apache.flink.runtime.taskmanager.TaskManagerLocation;
import org.apache.flink.util.ExceptionUtils;
import org.apache.flink.util.FlinkException;
import org.apache.flink.util.clock.ManualClock;
import org.apache.flink.shaded.guava18.com.google.common.collect.ImmutableMap;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.apache.flink.runtime.jobmaster.slotpool.AvailableSlotsTest.DEFAULT_TESTING_PROFILE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Tests for the {@link SlotPoolImpl}.
*/
public class SlotPoolImplTest extends SlotPoolTestBase {
private static final Time TIMEOUT = Time.seconds(10L);
private static final ComponentMainThreadExecutor mainThreadExecutor =
ComponentMainThreadExecutorServiceAdapter.forMainThread();
private TaskManagerLocation taskManagerLocation;
private SimpleAckingTaskManagerGateway taskManagerGateway;
@Before
@Override
public void setup() throws Exception {
taskManagerLocation = new LocalTaskManagerLocation();
taskManagerGateway = new SimpleAckingTaskManagerGateway();
super.setup();
}
@Test
public void testAllocateSimpleSlot() throws Exception {
CompletableFuture<SlotRequest> slotRequestFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(slotRequestFuture::complete);
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
SlotRequestId requestId = new SlotRequestId();
CompletableFuture<PhysicalSlot> future = requestNewAllocatedSlot(slotPool, requestId);
assertFalse(future.isDone());
final SlotRequest slotRequest = slotRequestFuture.get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
final SlotOffer slotOffer = new SlotOffer(
slotRequest.getAllocationId(),
0,
DEFAULT_TESTING_PROFILE);
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer));
PhysicalSlot physicalSlot = future.get(1, TimeUnit.SECONDS);
assertTrue(future.isDone());
assertEquals(taskManagerLocation, physicalSlot.getTaskManagerLocation());
assertEquals(slotRequest.getAllocationId(), physicalSlot.getAllocationId());
}
}
@Test
public void testAllocationFulfilledByReturnedSlot() throws Exception {
final ArrayBlockingQueue<SlotRequest> slotRequestQueue = new ArrayBlockingQueue<>(2);
resourceManagerGateway.setRequestSlotConsumer(slotRequest -> {
while (!slotRequestQueue.offer(slotRequest)) {
// noop
}
});
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
SlotRequestId requestId1 = new SlotRequestId();
CompletableFuture<PhysicalSlot> future1 = requestNewAllocatedSlot(
slotPool,
requestId1
);
SlotRequestId requestId2 = new SlotRequestId();
CompletableFuture<PhysicalSlot> future2 = requestNewAllocatedSlot(
slotPool,
requestId2
);
assertFalse(future1.isDone());
assertFalse(future2.isDone());
final List<SlotRequest> slotRequests = new ArrayList<>(2);
for (int i = 0; i < 2; i++) {
slotRequests.add(slotRequestQueue.poll(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS));
}
final SlotOffer slotOffer = new SlotOffer(
slotRequests.get(0).getAllocationId(),
0,
DEFAULT_TESTING_PROFILE);
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer));
PhysicalSlot slot1 = future1.get(1, TimeUnit.SECONDS);
assertTrue(future1.isDone());
assertFalse(future2.isDone());
// return this slot to pool
slotPool.releaseSlot(requestId1, null);
// second allocation fulfilled by previous slot returning
PhysicalSlot slot2 = future2.get(1, TimeUnit.SECONDS);
assertTrue(future2.isDone());
assertEquals(slot1, slot2);
}
}
@Test
public void testAllocateWithFreeSlot() throws Exception {
final CompletableFuture<SlotRequest> slotRequestFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(slotRequestFuture::complete);
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
SlotRequestId requestId1 = new SlotRequestId();
CompletableFuture<PhysicalSlot> future1 = requestNewAllocatedSlot(
slotPool,
requestId1
);
assertFalse(future1.isDone());
final SlotRequest slotRequest = slotRequestFuture.get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
final SlotOffer slotOffer = new SlotOffer(
slotRequest.getAllocationId(),
0,
DEFAULT_TESTING_PROFILE);
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer));
PhysicalSlot slot1 = future1.get(1, TimeUnit.SECONDS);
assertTrue(future1.isDone());
// return this slot to pool
slotPool.releaseSlot(requestId1, null);
assertEquals(1, slotPool.getAvailableSlots().size());
assertEquals(0, slotPool.getAllocatedSlots().size());
Optional<PhysicalSlot> optional = slotPool.allocateAvailableSlot(
new SlotRequestId(),
slotRequest.getAllocationId()
);
// second allocation fulfilled by previous slot returning
assertTrue(optional.isPresent());
PhysicalSlot slot2 = optional.get();
assertEquals(slot1, slot2);
}
}
@Test
public void testOfferSlot() throws Exception {
final CompletableFuture<SlotRequest> slotRequestFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(slotRequestFuture::complete);
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
SlotRequestId requestId = new SlotRequestId();
CompletableFuture<PhysicalSlot> future = requestNewAllocatedSlot(
slotPool,
requestId
);
assertFalse(future.isDone());
final SlotRequest slotRequest = slotRequestFuture.get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
final SlotOffer slotOffer = new SlotOffer(
slotRequest.getAllocationId(),
0,
DEFAULT_TESTING_PROFILE);
final TaskManagerLocation invalidTaskManagerLocation = new LocalTaskManagerLocation();
// slot from unregistered resource
assertFalse(slotPool.offerSlot(invalidTaskManagerLocation, taskManagerGateway, slotOffer));
final SlotOffer nonRequestedSlotOffer = new SlotOffer(
new AllocationID(),
0,
DEFAULT_TESTING_PROFILE);
// we'll also accept non requested slots
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, nonRequestedSlotOffer));
// accepted slot
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer));
PhysicalSlot slot = future.get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
assertEquals(1, slotPool.getAvailableSlots().size());
assertEquals(1, slotPool.getAllocatedSlots().size());
assertEquals(taskManagerLocation, slot.getTaskManagerLocation());
assertEquals(nonRequestedSlotOffer.getAllocationId(), slot.getAllocationId());
// duplicated offer with using slot
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer));
assertEquals(1, slotPool.getAllocatedSlots().size());
assertEquals(nonRequestedSlotOffer.getAllocationId(), slot.getAllocationId());
final SlotOffer anotherSlotOfferWithSameAllocationId = new SlotOffer(
slotRequest.getAllocationId(),
1,
DEFAULT_TESTING_PROFILE);
assertFalse(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, anotherSlotOfferWithSameAllocationId));
TaskManagerLocation anotherTaskManagerLocation = new LocalTaskManagerLocation();
assertFalse(slotPool.offerSlot(anotherTaskManagerLocation, taskManagerGateway, slotOffer));
// duplicated offer with free slot
slotPool.releaseSlot(requestId, null);
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer));
assertFalse(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, anotherSlotOfferWithSameAllocationId));
assertFalse(slotPool.offerSlot(anotherTaskManagerLocation, taskManagerGateway, slotOffer));
}
}
@Test
public void testReleaseResource() throws Exception {
final CompletableFuture<SlotRequest> slotRequestFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(slotRequestFuture::complete);
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
SlotRequestId requestId1 = new SlotRequestId();
CompletableFuture<PhysicalSlot> future1 = requestNewAllocatedSlot(
slotPool,
requestId1
);
final SlotRequest slotRequest = slotRequestFuture.get(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
CompletableFuture<PhysicalSlot> future2 = requestNewAllocatedSlot(
slotPool,
new SlotRequestId()
);
final SlotOffer slotOffer = new SlotOffer(
slotRequest.getAllocationId(),
0,
DEFAULT_TESTING_PROFILE);
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer));
PhysicalSlot slot1 = future1.get(1, TimeUnit.SECONDS);
assertTrue(future1.isDone());
assertFalse(future2.isDone());
final CompletableFuture<?> releaseFuture = new CompletableFuture<>();
SingleLogicalSlot logicalSlot = SingleLogicalSlot.allocateFromPhysicalSlot(
requestId1,
slot1,
Locality.UNKNOWN,
new DummySlotOwner(),
true
);
logicalSlot.tryAssignPayload(new DummyPayload(releaseFuture));
slotPool.releaseTaskManager(taskManagerLocation.getResourceID(), null);
releaseFuture.get(1, TimeUnit.SECONDS);
assertFalse(logicalSlot.isAlive());
// slot released and not usable, second allocation still not fulfilled
Thread.sleep(10);
assertFalse(future2.isDone());
}
}
/**
* Tests that unused offered slots are directly used to fulfill pending slot
* requests.
*
* <p>Moreover it tests that the old slot request is canceled
*
* <p>See FLINK-8089, FLINK-8934
*/
@Test
public void testFulfillingSlotRequestsWithUnusedOfferedSlots() throws Exception {
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
final ArrayBlockingQueue<AllocationID> allocationIds = new ArrayBlockingQueue<>(2);
resourceManagerGateway.setRequestSlotConsumer(
(SlotRequest slotRequest) -> allocationIds.offer(slotRequest.getAllocationId()));
final ArrayBlockingQueue<AllocationID> canceledAllocations = new ArrayBlockingQueue<>(2);
resourceManagerGateway.setCancelSlotConsumer(canceledAllocations::offer);
final SlotRequestId slotRequestId1 = new SlotRequestId();
final SlotRequestId slotRequestId2 = new SlotRequestId();
CompletableFuture<PhysicalSlot> slotFuture1 = requestNewAllocatedSlot(
slotPool,
slotRequestId1
);
// wait for the first slot request
final AllocationID allocationId1 = allocationIds.take();
CompletableFuture<PhysicalSlot> slotFuture2 = requestNewAllocatedSlot(
slotPool,
slotRequestId2
);
// wait for the second slot request
final AllocationID allocationId2 = allocationIds.take();
slotPool.releaseSlot(slotRequestId1, null);
try {
// this should fail with a CancellationException
slotFuture1.get();
fail("The first slot future should have failed because it was cancelled.");
} catch (ExecutionException ee) {
// expected
assertTrue(ExceptionUtils.stripExecutionException(ee) instanceof FlinkException);
}
assertEquals(allocationId1, canceledAllocations.take());
final SlotOffer slotOffer = new SlotOffer(allocationId1, 0, ResourceProfile.ANY);
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
assertTrue(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer));
// the slot offer should fulfill the second slot request
assertEquals(allocationId1, slotFuture2.get().getAllocationId());
// check that the second slot allocation has been canceled
assertEquals(allocationId2, canceledAllocations.take());
}
}
/**
* Tests that a SlotPoolImpl shutdown releases all registered slots.
*/
@Test
public void testShutdownReleasesAllSlots() throws Exception {
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
final int numSlotOffers = 2;
final Collection<SlotOffer> slotOffers = new ArrayList<>(numSlotOffers);
for (int i = 0; i < numSlotOffers; i++) {
slotOffers.add(
new SlotOffer(
new AllocationID(),
i,
ResourceProfile.ANY));
}
final ArrayBlockingQueue<AllocationID> freedSlotQueue = new ArrayBlockingQueue<>(numSlotOffers);
taskManagerGateway.setFreeSlotFunction(
(AllocationID allocationID, Throwable cause) -> {
try {
freedSlotQueue.put(allocationID);
return CompletableFuture.completedFuture(Acknowledge.get());
} catch (InterruptedException e) {
return FutureUtils.completedExceptionally(e);
}
});
final Collection<SlotOffer> acceptedSlotOffers = slotPool.offerSlots(taskManagerLocation, taskManagerGateway, slotOffers);
assertThat(acceptedSlotOffers, Matchers.equalTo(slotOffers));
// shut down the slot pool
slotPool.close();
// the shut down operation should have freed all registered slots
ArrayList<AllocationID> freedSlots = new ArrayList<>(numSlotOffers);
while (freedSlots.size() < numSlotOffers) {
freedSlotQueue.drainTo(freedSlots);
}
assertThat(freedSlots, Matchers.containsInAnyOrder(slotOffers.stream().map(SlotOffer::getAllocationId).toArray()));
}
}
@Test
public void testCheckIdleSlot() throws Exception {
final ManualClock clock = new ManualClock();
try (TestingSlotPoolImpl slotPool = slotPoolBuilder
.setClock(clock)
.setIdleSlotTimeout(TIMEOUT)
.build()) {
final BlockingQueue<AllocationID> freedSlots = new ArrayBlockingQueue<>(1);
taskManagerGateway.setFreeSlotFunction(
(AllocationID allocationId, Throwable cause) -> {
try {
freedSlots.put(allocationId);
return CompletableFuture.completedFuture(Acknowledge.get());
} catch (InterruptedException e) {
return FutureUtils.completedExceptionally(e);
}
});
final AllocationID expiredSlotID = new AllocationID();
final AllocationID freshSlotID = new AllocationID();
final SlotOffer slotToExpire = new SlotOffer(expiredSlotID, 0, ResourceProfile.ANY);
final SlotOffer slotToNotExpire = new SlotOffer(freshSlotID, 1, ResourceProfile.ANY);
assertThat(slotPool.registerTaskManager(taskManagerLocation.getResourceID()),
Matchers.is(true));
assertThat(
slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotToExpire),
Matchers.is(true));
clock.advanceTime(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
assertThat(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotToNotExpire),
Matchers.is(true));
clock.advanceTime(1L, TimeUnit.MILLISECONDS);
slotPool.triggerCheckIdleSlot();
final AllocationID freedSlot = freedSlots.poll(TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
assertThat(freedSlot, Matchers.is(expiredSlotID));
assertThat(freedSlots.isEmpty(), Matchers.is(true));
}
}
/**
* Tests that idle slots which cannot be released will be discarded. See FLINK-11059.
*/
@Test
public void testDiscardIdleSlotIfReleasingFailed() throws Exception {
final ManualClock clock = new ManualClock();
try (TestingSlotPoolImpl slotPool = slotPoolBuilder
.setClock(clock)
.setIdleSlotTimeout(TIMEOUT)
.build()) {
final AllocationID expiredAllocationId = new AllocationID();
final SlotOffer slotToExpire = new SlotOffer(expiredAllocationId, 0, ResourceProfile.ANY);
OneShotLatch freeSlotLatch = new OneShotLatch();
taskManagerGateway.setFreeSlotFunction((AllocationID allocationId, Throwable cause) -> {
freeSlotLatch.trigger();
return FutureUtils.completedExceptionally(new TimeoutException("Test failure"));
});
assertThat(slotPool.registerTaskManager(taskManagerLocation.getResourceID()), Matchers.is(true));
assertThat(slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotToExpire), Matchers.is(true));
clock.advanceTime(TIMEOUT.toMilliseconds() + 1, TimeUnit.MILLISECONDS);
slotPool.triggerCheckIdleSlot();
freeSlotLatch.await();
CompletableFuture<PhysicalSlot> future = requestNewAllocatedSlot(
slotPool,
new SlotRequestId()
);
try {
// since the slot must have been discarded, we cannot fulfill the slot request
future.get(10L, TimeUnit.MILLISECONDS);
fail("Expected to fail with a timeout.");
} catch (TimeoutException ignored) {
// expected
assertEquals(0, slotPool.getAvailableSlots().size());
}
}
}
/**
* Tests that failed slots are freed on the {@link TaskExecutor}.
*/
@Test
public void testFreeFailedSlots() throws Exception {
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
final int parallelism = 5;
final ArrayBlockingQueue<AllocationID> allocationIds = new ArrayBlockingQueue<>(parallelism);
resourceManagerGateway.setRequestSlotConsumer(
slotRequest -> allocationIds.offer(slotRequest.getAllocationId()));
final Map<SlotRequestId, CompletableFuture<PhysicalSlot>> slotRequestFutures = new HashMap<>(parallelism);
for (int i = 0; i < parallelism; i++) {
final SlotRequestId slotRequestId = new SlotRequestId();
slotRequestFutures.put(slotRequestId, requestNewAllocatedSlot(slotPool, slotRequestId));
}
final List<SlotOffer> slotOffers = new ArrayList<>(parallelism);
for (int i = 0; i < parallelism; i++) {
slotOffers.add(new SlotOffer(allocationIds.take(), i, ResourceProfile.ANY));
}
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
slotPool.offerSlots(taskManagerLocation, taskManagerGateway, slotOffers);
// wait for the completion of both slot futures
FutureUtils.waitForAll(slotRequestFutures.values()).get();
final ArrayBlockingQueue<AllocationID> freedSlots = new ArrayBlockingQueue<>(1);
taskManagerGateway.setFreeSlotFunction(
(allocationID, throwable) -> {
freedSlots.offer(allocationID);
return CompletableFuture.completedFuture(Acknowledge.get());
});
final FlinkException failException = new FlinkException("Test fail exception");
// fail allocations one by one
for (int i = 0; i < parallelism - 1; i++) {
final SlotOffer slotOffer = slotOffers.get(i);
Optional<ResourceID> emptyTaskExecutorFuture =
slotPool.failAllocation(slotOffer.getAllocationId(), failException);
assertThat(emptyTaskExecutorFuture.isPresent(), is(false));
assertThat(freedSlots.take(), is(equalTo(slotOffer.getAllocationId())));
}
final SlotOffer slotOffer = slotOffers.get(parallelism - 1);
final Optional<ResourceID> emptyTaskExecutorFuture = slotPool.failAllocation(
slotOffer.getAllocationId(),
failException);
assertTrue(emptyTaskExecutorFuture.isPresent());
assertThat(emptyTaskExecutorFuture.get(), is(equalTo(taskManagerLocation.getResourceID())));
assertThat(freedSlots.take(), is(equalTo(slotOffer.getAllocationId())));
}
}
/**
* Tests that create report of allocated slots on a {@link TaskExecutor}.
*/
@Test
public void testCreateAllocatedSlotReport() throws Exception {
JobID jobID = new JobID();
try (SlotPoolImpl slotPool = slotPoolBuilder.build(jobID)) {
final ArrayBlockingQueue<AllocationID> allocationIds = new ArrayBlockingQueue<>(1);
resourceManagerGateway.setRequestSlotConsumer(
slotRequest -> allocationIds.offer(slotRequest.getAllocationId()));
final SlotRequestId slotRequestId = new SlotRequestId();
final CompletableFuture<PhysicalSlot> slotRequestFuture = requestNewAllocatedSlot(
slotPool,
slotRequestId
);
final List<AllocatedSlotInfo> allocatedSlotInfos = new ArrayList<>(2);
final List<SlotOffer> slotOffers = new ArrayList<>(2);
final AllocationID allocatedId = allocationIds.take();
slotOffers.add(new SlotOffer(allocatedId, 0, ResourceProfile.ANY));
allocatedSlotInfos.add(new AllocatedSlotInfo(0, allocatedId));
final AllocationID availableId = new AllocationID();
slotOffers.add(new SlotOffer(availableId, 1, ResourceProfile.ANY));
allocatedSlotInfos.add(new AllocatedSlotInfo(1, availableId));
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
slotPool.offerSlots(taskManagerLocation, taskManagerGateway, slotOffers);
// wait for the completion of slot future
slotRequestFuture.get(1, TimeUnit.SECONDS);
final AllocatedSlotReport slotReport = slotPool.createAllocatedSlotReport(taskManagerLocation.getResourceID());
assertThat(jobID, is(slotReport.getJobId()));
assertThat(slotReport.getAllocatedSlotInfos(), containsInAnyOrder(isEachEqual(allocatedSlotInfos)));
}
}
@Test
public void testCalculationOfTaskExecutorUtilization() throws Exception {
try (final SlotPoolImpl slotPool = slotPoolBuilder.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
final TaskManagerLocation firstTaskManagerLocation = new LocalTaskManagerLocation();
final TaskManagerLocation secondTaskManagerLocation = new LocalTaskManagerLocation();
final List<AllocationID> firstTaskManagersSlots = offerSlots(firstTaskManagerLocation, slotPool, 4);
final List<AllocationID> secondTaskManagersSlots = offerSlots(secondTaskManagerLocation, slotPool, 4);
slotPool.allocateAvailableSlot(new SlotRequestId(), firstTaskManagersSlots.get(0));
slotPool.allocateAvailableSlot(new SlotRequestId(), firstTaskManagersSlots.get(1));
slotPool.allocateAvailableSlot(new SlotRequestId(), secondTaskManagersSlots.get(3));
final Collection<SlotInfoWithUtilization> availableSlotsInformation = slotPool.getAvailableSlotsInformation();
final Map<TaskManagerLocation, Double> utilizationPerTaskExecutor = ImmutableMap.of(
firstTaskManagerLocation, 2.0 / 4,
secondTaskManagerLocation, 1.0 / 4);
for (SlotInfoWithUtilization slotInfoWithUtilization : availableSlotsInformation) {
final double expectedTaskExecutorUtilization = utilizationPerTaskExecutor.get(slotInfoWithUtilization.getTaskManagerLocation());
assertThat(slotInfoWithUtilization.getTaskExecutorUtilization(), is(closeTo(expectedTaskExecutorUtilization, 0.1)));
}
}
}
@Test
public void testOrphanedAllocationCanBeRemapped() throws Exception {
final List<AllocationID> allocationIds = new ArrayList<>();
resourceManagerGateway.setRequestSlotConsumer(
slotRequest -> allocationIds.add(slotRequest.getAllocationId()));
final List<AllocationID> canceledAllocations = new ArrayList<>();
resourceManagerGateway.setCancelSlotConsumer(canceledAllocations::add);
try (SlotPoolImpl slotPool = slotPoolBuilder.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
final SlotRequestId slotRequestId1 = new SlotRequestId();
final SlotRequestId slotRequestId2 = new SlotRequestId();
requestNewAllocatedSlots(slotPool, slotRequestId1, slotRequestId2);
final AllocationID allocationId1 = allocationIds.get(0);
final AllocationID allocationId2 = allocationIds.get(1);
offerSlot(slotPool, allocationId2);
// verify that orphaned allocationId2 is remapped to slotRequestId2
assertThat(slotPool.getPendingRequests().values(), hasSize(1));
assertThat(slotPool.getPendingRequests().containsKeyA(slotRequestId2), is(true));
assertThat(slotPool.getPendingRequests().containsKeyB(allocationId1), is(true));
assertThat(canceledAllocations, hasSize(0));
}
}
@Test
public void testOrphanedAllocationIsCanceledIfNotRemapped() throws Exception {
final List<AllocationID> allocationIds = new ArrayList<>();
resourceManagerGateway.setRequestSlotConsumer(
slotRequest -> allocationIds.add(slotRequest.getAllocationId()));
final List<AllocationID> canceledAllocations = new ArrayList<>();
resourceManagerGateway.setCancelSlotConsumer(canceledAllocations::add);
try (SlotPoolImpl slotPool = slotPoolBuilder
.setResourceManagerGateway(resourceManagerGateway)
.build()) {
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
final SlotRequestId slotRequestId1 = new SlotRequestId();
final SlotRequestId slotRequestId2 = new SlotRequestId();
requestNewAllocatedSlots(slotPool, slotRequestId1, slotRequestId2);
final AllocationID allocationId1 = allocationIds.get(0);
final AllocationID allocationId2 = allocationIds.get(1);
// create a random non-existing allocation id
AllocationID randomAllocationId;
do {
randomAllocationId = new AllocationID();
} while (randomAllocationId.equals(allocationId1) || randomAllocationId.equals(allocationId2));
offerSlot(slotPool, randomAllocationId);
assertThat(slotPool.getPendingRequests().values(), hasSize(1));
assertThat(canceledAllocations, contains(allocationId1));
}
}
/**
* In this case a slot is offered to the SlotPoolImpl before the ResourceManager is connected.
* It can happen in production if a TaskExecutor is reconnected to a restarted JobMaster.
*/
@Test
public void testSlotsOfferedWithoutResourceManagerConnected() throws Exception {
try (SlotPoolImpl slotPool = new TestingSlotPoolImpl(new JobID())) {
slotPool.start(JobMasterId.generate(), "mock-address", mainThreadExecutor);
final SlotRequestId slotRequestId = new SlotRequestId();
final CompletableFuture<PhysicalSlot> slotFuture = requestNewAllocatedSlot(slotPool, slotRequestId);
assertThat(slotPool.getWaitingForResourceManager().values(), hasSize(1));
final AllocationID allocationId = new AllocationID();
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
offerSlot(slotPool, allocationId);
assertThat(slotPool.getWaitingForResourceManager().values(), hasSize(0));
assertThat(slotFuture.isDone(), is(true));
assertThat(slotFuture.isCompletedExceptionally(), is(false));
assertThat(slotFuture.getNow(null).getAllocationId(), is(allocationId));
}
}
private void offerSlot(final SlotPoolImpl slotPool, final AllocationID allocationId) {
final SlotOffer slotOffer = new SlotOffer(allocationId, 0, ResourceProfile.ANY);
slotPool.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer);
}
private List<AllocationID> offerSlots(
TaskManagerLocation taskManagerLocation,
SlotPoolImpl slotPool,
int numberOfSlotsToRegister) {
final List<AllocationID> allocationIds = IntStream.range(0, numberOfSlotsToRegister)
.mapToObj(ignored -> new AllocationID())
.collect(Collectors.toList());
Collection<SlotOffer> slotOffers = IntStream.range(0, numberOfSlotsToRegister)
.mapToObj(index -> new SlotOffer(allocationIds.get(index), index, ResourceProfile.ANY))
.collect(Collectors.toList());
slotPool.offerSlots(
taskManagerLocation,
new SimpleAckingTaskManagerGateway(),
slotOffers);
return allocationIds;
}
private static Collection<Matcher<? super AllocatedSlotInfo>> isEachEqual(Collection<AllocatedSlotInfo> allocatedSlotInfos) {
return allocatedSlotInfos
.stream()
.map(SlotPoolImplTest::isEqualAllocatedSlotInfo)
.collect(Collectors.toList());
}
private static Matcher<AllocatedSlotInfo> isEqualAllocatedSlotInfo(AllocatedSlotInfo expectedAllocatedSlotInfo) {
return new TypeSafeDiagnosingMatcher<AllocatedSlotInfo>() {
@Override
public void describeTo(Description description) {
description.appendText(describeAllocatedSlotInformation(expectedAllocatedSlotInfo));
}
private String describeAllocatedSlotInformation(AllocatedSlotInfo expectedAllocatedSlotInformation) {
return expectedAllocatedSlotInformation.toString();
}
@Override
protected boolean matchesSafely(AllocatedSlotInfo item, Description mismatchDescription) {
final boolean matches = item.getAllocationId().equals(expectedAllocatedSlotInfo.getAllocationId()) &&
item.getSlotIndex() == expectedAllocatedSlotInfo.getSlotIndex();
if (!matches) {
mismatchDescription
.appendText("Actual value ")
.appendText(describeAllocatedSlotInformation(item))
.appendText(" differs from expected value ")
.appendText(describeAllocatedSlotInformation(expectedAllocatedSlotInfo));
}
return matches;
}
};
}
}