|
| 1 | +package com.hubspot.singularity.scheduler; |
| 2 | + |
| 3 | +import com.google.inject.Inject; |
| 4 | +import com.hubspot.singularity.SingularityPendingRequest; |
| 5 | +import com.hubspot.singularity.SingularityPendingTaskBuilder; |
| 6 | +import com.hubspot.singularity.SingularityPendingTaskId; |
| 7 | +import com.hubspot.singularity.helpers.TaskLagGuardrail; |
| 8 | +import com.hubspot.singularity.mesos.SingularitySchedulerLock; |
| 9 | +import java.util.concurrent.CountDownLatch; |
| 10 | +import java.util.concurrent.ExecutorService; |
| 11 | +import java.util.concurrent.Executors; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +public class SingularitySchedulerLockTest extends SingularitySchedulerTestBase { |
| 17 | + private final ExecutorService executor; |
| 18 | + |
| 19 | + @Inject |
| 20 | + private SingularitySchedulerLock lock; |
| 21 | + |
| 22 | + @Inject |
| 23 | + private TaskLagGuardrail lagGuardrail; |
| 24 | + |
| 25 | + private String name = SingularitySchedulerLockTest.class.getSimpleName(); |
| 26 | + |
| 27 | + public SingularitySchedulerLockTest() { |
| 28 | + super(false); |
| 29 | + this.executor = Executors.newSingleThreadExecutor(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testLowPriorityLockNormal() { |
| 34 | + TestRunnable runnable = new TestRunnable(); |
| 35 | + lock.runWithRequestLock( |
| 36 | + runnable, |
| 37 | + requestId, |
| 38 | + name, |
| 39 | + SingularitySchedulerLock.Priority.LOW |
| 40 | + ); |
| 41 | + Assertions.assertTrue(runnable.ran); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testLowPriorityLockLaggedWithoutContention() { |
| 46 | + laggedRequest(); |
| 47 | + TestRunnable runnable = new TestRunnable(); |
| 48 | + lock.runWithRequestLock( |
| 49 | + runnable, |
| 50 | + requestId, |
| 51 | + name, |
| 52 | + SingularitySchedulerLock.Priority.LOW |
| 53 | + ); |
| 54 | + Assertions.assertTrue(runnable.ran); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testLowPriorityLockLaggedWithContention() { |
| 59 | + laggedRequest(); |
| 60 | + |
| 61 | + CountDownLatch started = new CountDownLatch(1); |
| 62 | + CountDownLatch finished = new CountDownLatch(1); |
| 63 | + hold(started, finished); |
| 64 | + |
| 65 | + TestRunnable runnable = new TestRunnable(); |
| 66 | + lock.runWithRequestLock( |
| 67 | + runnable, |
| 68 | + requestId, |
| 69 | + name, |
| 70 | + SingularitySchedulerLock.Priority.LOW |
| 71 | + ); |
| 72 | + Assertions.assertFalse(runnable.ran); |
| 73 | + finished.countDown(); |
| 74 | + } |
| 75 | + |
| 76 | + private void hold(CountDownLatch started, CountDownLatch finished) { |
| 77 | + executor.submit( |
| 78 | + () -> { |
| 79 | + lock.runWithRequestLock( |
| 80 | + () -> { |
| 81 | + try { |
| 82 | + started.countDown(); |
| 83 | + finished.await(5, TimeUnit.SECONDS); |
| 84 | + } catch (InterruptedException e) { |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + }, |
| 88 | + requestId, |
| 89 | + name |
| 90 | + ); |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + try { |
| 95 | + started.await(5, TimeUnit.SECONDS); |
| 96 | + } catch (InterruptedException e) { |
| 97 | + throw new RuntimeException(e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void laggedRequest() { |
| 102 | + taskManager.savePendingTask( |
| 103 | + new SingularityPendingTaskBuilder() |
| 104 | + .setPendingTaskId( |
| 105 | + new SingularityPendingTaskId( |
| 106 | + requestId, |
| 107 | + firstDeployId, |
| 108 | + 0, |
| 109 | + 0, |
| 110 | + SingularityPendingRequest.PendingType.NEW_DEPLOY, |
| 111 | + 0 |
| 112 | + ) |
| 113 | + ) |
| 114 | + .build() |
| 115 | + ); |
| 116 | + lagGuardrail.updateLateTasksByRequestId(); |
| 117 | + } |
| 118 | + |
| 119 | + private static class TestRunnable implements Runnable { |
| 120 | + boolean ran = false; |
| 121 | + |
| 122 | + @Override |
| 123 | + public void run() { |
| 124 | + ran = true; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments