/
SlotPoolTest.java
682 lines (538 loc) · 22.5 KB
/
SlotPoolTest.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
/*
* 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.runtime.clusterframework.types.AllocationID;
import org.apache.flink.runtime.clusterframework.types.ResourceProfile;
import org.apache.flink.runtime.executiongraph.utils.SimpleAckingTaskManagerGateway;
import org.apache.flink.runtime.instance.SlotSharingGroupId;
import org.apache.flink.runtime.jobgraph.JobVertexID;
import org.apache.flink.runtime.jobmanager.scheduler.DummyScheduledUnit;
import org.apache.flink.runtime.jobmanager.scheduler.ScheduledUnit;
import org.apache.flink.runtime.jobmaster.JobMasterId;
import org.apache.flink.runtime.jobmaster.LogicalSlot;
import org.apache.flink.runtime.jobmaster.SlotRequestId;
import org.apache.flink.runtime.messages.Acknowledge;
import org.apache.flink.runtime.resourcemanager.ResourceManagerGateway;
import org.apache.flink.runtime.resourcemanager.SlotRequest;
import org.apache.flink.runtime.resourcemanager.utils.TestingResourceManagerGateway;
import org.apache.flink.runtime.rpc.RpcService;
import org.apache.flink.runtime.rpc.RpcUtils;
import org.apache.flink.runtime.rpc.TestingRpcService;
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.runtime.testingUtils.TestingUtils;
import org.apache.flink.runtime.util.clock.ManualClock;
import org.apache.flink.testutils.category.Flip6;
import org.apache.flink.util.ExceptionUtils;
import org.apache.flink.util.FlinkException;
import org.apache.flink.util.TestLogger;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
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 static org.apache.flink.runtime.jobmaster.slotpool.AvailableSlotsTest.DEFAULT_TESTING_PROFILE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@Category(Flip6.class)
public class SlotPoolTest extends TestLogger {
private static final Logger LOG = LoggerFactory.getLogger(SlotPoolTest.class);
private final Time timeout = Time.seconds(10L);
private RpcService rpcService;
private JobID jobId;
private TaskManagerLocation taskManagerLocation;
private SimpleAckingTaskManagerGateway taskManagerGateway;
private TestingResourceManagerGateway resourceManagerGateway;
@Before
public void setUp() throws Exception {
this.rpcService = new TestingRpcService();
this.jobId = new JobID();
taskManagerLocation = new LocalTaskManagerLocation();
taskManagerGateway = new SimpleAckingTaskManagerGateway();
resourceManagerGateway = new TestingResourceManagerGateway();
}
@After
public void tearDown() throws Exception {
rpcService.stopService();
}
@Test
public void testAllocateSimpleSlot() throws Exception {
CompletableFuture<SlotRequest> slotRequestFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(slotRequest -> slotRequestFuture.complete(slotRequest));
final SlotPool slotPool = new SlotPool(rpcService, jobId);
try {
SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
slotPoolGateway.registerTaskManager(taskManagerLocation.getResourceID());
SlotRequestId requestId = new SlotRequestId();
CompletableFuture<LogicalSlot> future = slotPoolGateway.allocateSlot(
requestId,
new DummyScheduledUnit(),
DEFAULT_TESTING_PROFILE,
Collections.emptyList(),
true,
timeout);
assertFalse(future.isDone());
final SlotRequest slotRequest = slotRequestFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
final SlotOffer slotOffer = new SlotOffer(
slotRequest.getAllocationId(),
0,
DEFAULT_TESTING_PROFILE);
assertTrue(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer).get());
LogicalSlot slot = future.get(1, TimeUnit.SECONDS);
assertTrue(future.isDone());
assertTrue(slot.isAlive());
assertEquals(taskManagerLocation, slot.getTaskManagerLocation());
} finally {
RpcUtils.terminateRpcEndpoint(slotPool, timeout);
}
}
@Test
public void testAllocationFulfilledByReturnedSlot() throws Exception {
final ArrayBlockingQueue<SlotRequest> slotRequestQueue = new ArrayBlockingQueue<>(2);
resourceManagerGateway.setRequestSlotConsumer(slotRequest -> {
while (!slotRequestQueue.offer(slotRequest)) {
// noop
}
});
final SlotPool slotPool = new SlotPool(rpcService, jobId);
try {
SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
slotPool.registerTaskManager(taskManagerLocation.getResourceID());
CompletableFuture<LogicalSlot> future1 = slotPoolGateway.allocateSlot(
new SlotRequestId(),
new DummyScheduledUnit(),
DEFAULT_TESTING_PROFILE,
Collections.emptyList(),
true,
timeout);
CompletableFuture<LogicalSlot> future2 = slotPoolGateway.allocateSlot(
new SlotRequestId(),
new DummyScheduledUnit(),
DEFAULT_TESTING_PROFILE,
Collections.emptyList(),
true,
timeout);
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(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer).get());
LogicalSlot slot1 = future1.get(1, TimeUnit.SECONDS);
assertTrue(future1.isDone());
assertFalse(future2.isDone());
// return this slot to pool
slot1.releaseSlot();
// second allocation fulfilled by previous slot returning
LogicalSlot slot2 = future2.get(1, TimeUnit.SECONDS);
assertTrue(future2.isDone());
assertNotEquals(slot1, slot2);
assertFalse(slot1.isAlive());
assertTrue(slot2.isAlive());
assertEquals(slot1.getTaskManagerLocation(), slot2.getTaskManagerLocation());
assertEquals(slot1.getPhysicalSlotNumber(), slot2.getPhysicalSlotNumber());
assertEquals(slot1.getAllocationId(), slot2.getAllocationId());
} finally {
RpcUtils.terminateRpcEndpoint(slotPool, timeout);
}
}
@Test
public void testAllocateWithFreeSlot() throws Exception {
final CompletableFuture<SlotRequest> slotRequestFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(slotRequest -> slotRequestFuture.complete(slotRequest));
final SlotPool slotPool = new SlotPool(rpcService, jobId);
try {
SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
slotPoolGateway.registerTaskManager(taskManagerLocation.getResourceID());
CompletableFuture<LogicalSlot> future1 = slotPoolGateway.allocateSlot(
new SlotRequestId(),
new DummyScheduledUnit(),
DEFAULT_TESTING_PROFILE,
Collections.emptyList(),
true,
timeout);
assertFalse(future1.isDone());
final SlotRequest slotRequest = slotRequestFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
final SlotOffer slotOffer = new SlotOffer(
slotRequest.getAllocationId(),
0,
DEFAULT_TESTING_PROFILE);
assertTrue(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer).get());
LogicalSlot slot1 = future1.get(1, TimeUnit.SECONDS);
assertTrue(future1.isDone());
// return this slot to pool
slot1.releaseSlot();
CompletableFuture<LogicalSlot> future2 = slotPoolGateway.allocateSlot(
new SlotRequestId(),
new DummyScheduledUnit(),
DEFAULT_TESTING_PROFILE,
Collections.emptyList(),
true,
timeout);
// second allocation fulfilled by previous slot returning
LogicalSlot slot2 = future2.get(1, TimeUnit.SECONDS);
assertTrue(future2.isDone());
assertNotEquals(slot1, slot2);
assertFalse(slot1.isAlive());
assertTrue(slot2.isAlive());
assertEquals(slot1.getTaskManagerLocation(), slot2.getTaskManagerLocation());
assertEquals(slot1.getPhysicalSlotNumber(), slot2.getPhysicalSlotNumber());
} finally {
slotPool.shutDown();
}
}
@Test
public void testOfferSlot() throws Exception {
final CompletableFuture<SlotRequest> slotRequestFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(slotRequest -> slotRequestFuture.complete(slotRequest));
final SlotPool slotPool = new SlotPool(rpcService, jobId);
try {
SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
slotPoolGateway.registerTaskManager(taskManagerLocation.getResourceID());
CompletableFuture<LogicalSlot> future = slotPoolGateway.allocateSlot(
new SlotRequestId(),
new DummyScheduledUnit(),
DEFAULT_TESTING_PROFILE,
Collections.emptyList(),
true,
timeout);
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(slotPoolGateway.offerSlot(invalidTaskManagerLocation, taskManagerGateway, slotOffer).get());
final SlotOffer nonRequestedSlotOffer = new SlotOffer(
new AllocationID(),
0,
DEFAULT_TESTING_PROFILE);
// we'll also accept non requested slots
assertTrue(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, nonRequestedSlotOffer).get());
// accepted slot
assertTrue(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer).get());
LogicalSlot slot = future.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
assertTrue(slot.isAlive());
// duplicated offer with using slot
assertTrue(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer).get());
assertTrue(slot.isAlive());
// duplicated offer with free slot
slot.releaseSlot();
assertTrue(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer).get());
} finally {
slotPool.shutDown();
}
}
@Test
public void testReleaseResource() throws Exception {
final CompletableFuture<SlotRequest> slotRequestFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(slotRequest -> slotRequestFuture.complete(slotRequest));
final CompletableFuture<Boolean> slotReturnFuture = new CompletableFuture<>();
final SlotPool slotPool = new SlotPool(rpcService, jobId) {
@Override
public CompletableFuture<Acknowledge> releaseSlot(
SlotRequestId slotRequestId,
@Nullable SlotSharingGroupId slotSharingGroupId,
@Nullable Throwable cause) {
super.releaseSlot(
slotRequestId,
slotSharingGroupId,
cause);
slotReturnFuture.complete(true);
return CompletableFuture.completedFuture(Acknowledge.get());
}
};
try {
SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
slotPoolGateway.registerTaskManager(taskManagerLocation.getResourceID());
CompletableFuture<LogicalSlot> future1 = slotPoolGateway.allocateSlot(
new SlotRequestId(),
new DummyScheduledUnit(),
DEFAULT_TESTING_PROFILE,
Collections.emptyList(),
true,
timeout);
final SlotRequest slotRequest = slotRequestFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
CompletableFuture<LogicalSlot> future2 = slotPoolGateway.allocateSlot(
new SlotRequestId(),
new DummyScheduledUnit(),
DEFAULT_TESTING_PROFILE,
Collections.emptyList(),
true,
timeout);
final SlotOffer slotOffer = new SlotOffer(
slotRequest.getAllocationId(),
0,
DEFAULT_TESTING_PROFILE);
assertTrue(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer).get());
LogicalSlot slot1 = future1.get(1, TimeUnit.SECONDS);
assertTrue(future1.isDone());
assertFalse(future2.isDone());
slotPoolGateway.releaseTaskManager(taskManagerLocation.getResourceID());
// wait until the slot has been returned
slotReturnFuture.get();
assertFalse(slot1.isAlive());
// slot released and not usable, second allocation still not fulfilled
Thread.sleep(10);
assertFalse(future2.isDone());
} finally {
slotPool.shutDown();
}
}
/**
* Tests that a slot request is cancelled if it failed with an exception (e.g. TimeoutException).
*
* <p>See FLINK-7870
*/
@Test
public void testSlotRequestCancellationUponFailingRequest() throws Exception {
final SlotPool slotPool = new SlotPool(rpcService, jobId);
final CompletableFuture<Acknowledge> requestSlotFuture = new CompletableFuture<>();
final CompletableFuture<AllocationID> cancelSlotFuture = new CompletableFuture<>();
final CompletableFuture<AllocationID> requestSlotFutureAllocationId = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotFuture(requestSlotFuture);
resourceManagerGateway.setRequestSlotConsumer(slotRequest -> requestSlotFutureAllocationId.complete(slotRequest.getAllocationId()));
resourceManagerGateway.setCancelSlotConsumer(allocationID -> cancelSlotFuture.complete(allocationID));
final ScheduledUnit scheduledUnit = new ScheduledUnit(
new JobVertexID(),
null,
null);
try {
final SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
CompletableFuture<LogicalSlot> slotFuture = slotPoolGateway.allocateSlot(
new SlotRequestId(),
scheduledUnit,
ResourceProfile.UNKNOWN,
Collections.emptyList(),
true,
timeout);
requestSlotFuture.completeExceptionally(new FlinkException("Testing exception."));
try {
slotFuture.get();
fail("The slot future should not have been completed properly.");
} catch (Exception ignored) {
// expected
}
// check that a failure triggered the slot request cancellation
// with the correct allocation id
assertEquals(requestSlotFutureAllocationId.get(), cancelSlotFuture.get());
} finally {
try {
RpcUtils.terminateRpcEndpoint(slotPool, timeout);
} catch (Exception e) {
LOG.warn("Could not properly terminate the SlotPool.", e);
}
}
}
/**
* Tests that unused offered slots are directly used to fulfill pending slot
* requests.
*
* <p>See FLINK-8089
*/
@Test
public void testFulfillingSlotRequestsWithUnusedOfferedSlots() throws Exception {
final SlotPool slotPool = new SlotPool(rpcService, jobId);
final CompletableFuture<AllocationID> allocationIdFuture = new CompletableFuture<>();
resourceManagerGateway.setRequestSlotConsumer(
(SlotRequest slotRequest) -> allocationIdFuture.complete(slotRequest.getAllocationId()));
final SlotRequestId slotRequestId1 = new SlotRequestId();
final SlotRequestId slotRequestId2 = new SlotRequestId();
try {
final SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
final ScheduledUnit scheduledUnit = new ScheduledUnit(
new JobVertexID(),
null,
null);
CompletableFuture<LogicalSlot> slotFuture1 = slotPoolGateway.allocateSlot(
slotRequestId1,
scheduledUnit,
ResourceProfile.UNKNOWN,
Collections.emptyList(),
true,
timeout);
// wait for the first slot request
final AllocationID allocationId = allocationIdFuture.get();
CompletableFuture<LogicalSlot> slotFuture2 = slotPoolGateway.allocateSlot(
slotRequestId2,
scheduledUnit,
ResourceProfile.UNKNOWN,
Collections.emptyList(),
true,
timeout);
slotPoolGateway.releaseSlot(slotRequestId1, null, 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);
}
final SlotOffer slotOffer = new SlotOffer(allocationId, 0, ResourceProfile.UNKNOWN);
slotPoolGateway.registerTaskManager(taskManagerLocation.getResourceID()).get();
assertTrue(slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotOffer).get());
// the slot offer should fulfill the second slot request
assertEquals(allocationId, slotFuture2.get().getAllocationId());
} finally {
RpcUtils.terminateRpcEndpoint(slotPool, timeout);
}
}
/**
* Tests that a SlotPool shutdown releases all registered slots
*/
@Test
public void testShutdownReleasesAllSlots() throws Exception {
final SlotPool slotPool = new SlotPool(rpcService, jobId);
try {
final SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
slotPoolGateway.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.UNKNOWN));
}
final ArrayBlockingQueue<AllocationID> freedSlotQueue = new ArrayBlockingQueue<>(numSlotOffers);
taskManagerGateway.setFreeSlotConsumer(tuple -> {
while(!freedSlotQueue.offer(tuple.f0)) {}
});
final CompletableFuture<Collection<SlotOffer>> acceptedSlotOffersFuture = slotPoolGateway.offerSlots(taskManagerLocation, taskManagerGateway, slotOffers);
final Collection<SlotOffer> acceptedSlotOffers = acceptedSlotOffersFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
assertThat(acceptedSlotOffers, Matchers.equalTo(slotOffers));
// shut down the slot pool
slotPool.shutDown();
slotPool.getTerminationFuture().get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
// 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()));
} finally {
RpcUtils.terminateRpcEndpoint(slotPool, timeout);
}
}
@Test
public void testCheckIdleSlot() throws Exception {
final ManualClock clock = new ManualClock();
final SlotPool slotPool = new SlotPool(
rpcService,
jobId,
clock,
TestingUtils.infiniteTime(), TestingUtils.infiniteTime(),
timeout);
try {
final BlockingQueue<AllocationID> freedSlots = new ArrayBlockingQueue<>(1);
taskManagerGateway.setFreeSlotConsumer((tuple) -> freedSlots.offer(tuple.f0));
final SlotPoolGateway slotPoolGateway = setupSlotPool(slotPool, resourceManagerGateway);
final AllocationID expiredSlotID = new AllocationID();
final AllocationID freshSlotID = new AllocationID();
final SlotOffer slotToExpire = new SlotOffer(expiredSlotID, 0, ResourceProfile.UNKNOWN);
final SlotOffer slotToNotExpire = new SlotOffer(freshSlotID, 1, ResourceProfile.UNKNOWN);
assertThat(
slotPoolGateway.registerTaskManager(taskManagerLocation.getResourceID()).get(),
Matchers.is(Acknowledge.get()));
assertThat(
slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotToExpire).get(),
Matchers.is(true));
clock.advanceTime(timeout.toMilliseconds() - 1L, TimeUnit.MILLISECONDS);
assertThat(
slotPoolGateway.offerSlot(taskManagerLocation, taskManagerGateway, slotToNotExpire).get(),
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));
} finally {
RpcUtils.terminateRpcEndpoint(slotPool, timeout);
}
}
/**
* Tests that a slot allocation times out wrt to the specified time out.
*/
@Test
public void testSlotAllocationTimeout() throws Exception {
final SlotPool slotPool = new SlotPool(rpcService, jobId);
final Time allocationTimeout = Time.milliseconds(1L);
try {
setupSlotPool(slotPool, resourceManagerGateway);
final SlotProvider slotProvider = slotPool.getSlotProvider();
final CompletableFuture<LogicalSlot> allocationFuture = slotProvider.allocateSlot(
new DummyScheduledUnit(),
true,
Collections.emptyList(),
allocationTimeout);
try {
allocationFuture.get();
fail("Should have failed with a timeout exception.");
} catch (ExecutionException ee) {
assertThat(ExceptionUtils.stripExecutionException(ee), Matchers.instanceOf(TimeoutException.class));
}
} finally {
RpcUtils.terminateRpcEndpoint(slotPool, timeout);
}
}
private static SlotPoolGateway setupSlotPool(
SlotPool slotPool,
ResourceManagerGateway resourceManagerGateway) throws Exception {
final String jobManagerAddress = "foobar";
slotPool.start(JobMasterId.generate(), jobManagerAddress);
slotPool.connectToResourceManager(resourceManagerGateway);
return slotPool.getSelfGateway(SlotPoolGateway.class);
}
private AllocatedSlot createSlot(final AllocationID allocationId) {
return new AllocatedSlot(
allocationId,
taskManagerLocation,
0,
ResourceProfile.UNKNOWN,
taskManagerGateway);
}
}