Skip to content

Commit 1d668c6

Browse files
authored
Activity slot test flake (#2186)
Don't use metrics here and instead rely on wrapper for counts
1 parent b9eeda0 commit 1d668c6

File tree

2 files changed

+16
-47
lines changed

2 files changed

+16
-47
lines changed

temporal-sdk/src/test/java/io/temporal/internal/worker/WorkflowSlotsSmallSizeTests.java

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
import static org.junit.Assert.assertEquals;
2424

25-
import com.uber.m3.tally.RootScopeBuilder;
26-
import com.uber.m3.tally.Scope;
2725
import com.uber.m3.util.ImmutableMap;
2826
import io.temporal.activity.ActivityInterface;
2927
import io.temporal.activity.ActivityMethod;
@@ -32,10 +30,8 @@
3230
import io.temporal.client.WorkflowClient;
3331
import io.temporal.client.WorkflowOptions;
3432
import io.temporal.common.RetryOptions;
35-
import io.temporal.common.reporter.TestStatsReporter;
3633
import io.temporal.testUtils.CountingSlotSupplier;
3734
import io.temporal.testing.internal.SDKTestWorkflowRule;
38-
import io.temporal.worker.MetricsType;
3935
import io.temporal.worker.WorkerOptions;
4036
import io.temporal.worker.tuning.ActivitySlotInfo;
4137
import io.temporal.worker.tuning.CompositeTuner;
@@ -66,13 +62,9 @@ public class WorkflowSlotsSmallSizeTests {
6662
new CountingSlotSupplier<>(MAX_CONCURRENT_ACTIVITY_EXECUTION_SIZE);
6763
private final CountingSlotSupplier<LocalActivitySlotInfo> localActivitySlotSupplier =
6864
new CountingSlotSupplier<>(MAX_CONCURRENT_LOCAL_ACTIVITY_EXECUTION_SIZE);
69-
private final TestStatsReporter reporter = new TestStatsReporter();
7065
static Semaphore parallelSemRunning = new Semaphore(0);
7166
static Semaphore parallelSemBlocked = new Semaphore(0);
7267

73-
Scope metricsScope =
74-
new RootScopeBuilder().reporter(reporter).reportEvery(com.uber.m3.util.Duration.ofMillis(1));
75-
7668
@Parameterized.Parameter public boolean activitiesAreLocal;
7769

7870
@Parameterized.Parameters()
@@ -91,15 +83,13 @@ public static Object[] data() {
9183
activityTaskSlotSupplier,
9284
localActivitySlotSupplier))
9385
.build())
94-
.setMetricsScope(metricsScope)
9586
.setActivityImplementations(new TestActivitySemaphoreImpl())
9687
.setWorkflowTypes(ParallelActivities.class)
9788
.setDoNotStart(true)
9889
.build();
9990

10091
@Before
10192
public void setup() {
102-
reporter.flush();
10393
parallelSemRunning = new Semaphore(0);
10494
parallelSemBlocked = new Semaphore(0);
10595
}
@@ -116,24 +106,9 @@ public void tearDown() {
116106
localActivitySlotSupplier.releasedCount.get());
117107
}
118108

119-
private void assertWorkerSlotCount(int worker, int activity, int localActivity) {
120-
try {
121-
// There can be a delay in metrics emission, another option if this
122-
// is too flaky is to poll the metrics.
123-
Thread.sleep(100);
124-
} catch (InterruptedException e) {
125-
throw new RuntimeException(e);
126-
}
127-
reporter.assertGauge(
128-
MetricsType.WORKER_TASK_SLOTS_AVAILABLE, getWorkerTags("WorkflowWorker"), worker);
129-
// All slots should be available
130-
reporter.assertGauge(
131-
MetricsType.WORKER_TASK_SLOTS_AVAILABLE, getWorkerTags("ActivityWorker"), activity);
132-
// All slots should be available
133-
reporter.assertGauge(
134-
MetricsType.WORKER_TASK_SLOTS_AVAILABLE,
135-
getWorkerTags("LocalActivityWorker"),
136-
localActivity);
109+
private void assertCurrentUsedCount(int activity, int localActivity) {
110+
assertEquals(activity, activityTaskSlotSupplier.currentUsedSet.size());
111+
assertEquals(localActivity, localActivitySlotSupplier.currentUsedSet.size());
137112
}
138113

139114
@WorkflowInterface
@@ -219,14 +194,11 @@ private void assertIntraWFTSlotCount(int allowedToRun) {
219194
int runningLAs = activitiesAreLocal ? allowedToRun : 0;
220195
int runningAs = activitiesAreLocal ? 0 : allowedToRun;
221196
int runningWFTs = activitiesAreLocal ? 1 : 0;
222-
assertWorkerSlotCount(
223-
MAX_CONCURRENT_WORKFLOW_TASK_EXECUTION_SIZE - runningWFTs,
224-
MAX_CONCURRENT_ACTIVITY_EXECUTION_SIZE - runningAs,
225-
MAX_CONCURRENT_LOCAL_ACTIVITY_EXECUTION_SIZE - runningLAs);
197+
assertCurrentUsedCount(runningAs, runningLAs);
226198
}
227199

228200
@Test
229-
public void TestLocalActivitySlotAtLimit() throws InterruptedException {
201+
public void TestActivitySlotAtLimit() throws InterruptedException {
230202
testWorkflowRule.getTestEnvironment().start();
231203
WorkflowClient client = testWorkflowRule.getWorkflowClient();
232204
TestWorkflow workflow =
@@ -244,14 +216,11 @@ public void TestLocalActivitySlotAtLimit() throws InterruptedException {
244216
}
245217
workflow.workflow(true);
246218
// All slots should be available
247-
assertWorkerSlotCount(
248-
MAX_CONCURRENT_WORKFLOW_TASK_EXECUTION_SIZE,
249-
MAX_CONCURRENT_ACTIVITY_EXECUTION_SIZE,
250-
MAX_CONCURRENT_LOCAL_ACTIVITY_EXECUTION_SIZE);
219+
assertCurrentUsedCount(0, 0);
251220
}
252221

253222
@Test
254-
public void TestLocalActivityShutdownWhileWaitingOnSlot() throws InterruptedException {
223+
public void TestActivityShutdownWhileWaitingOnSlot() throws InterruptedException {
255224
testWorkflowRule.getTestEnvironment().start();
256225
WorkflowClient client = testWorkflowRule.getWorkflowClient();
257226
TestWorkflow workflow =
@@ -267,14 +236,12 @@ public void TestLocalActivityShutdownWhileWaitingOnSlot() throws InterruptedExce
267236
parallelSemBlocked.release(2);
268237
testWorkflowRule.getTestEnvironment().getWorkerFactory().awaitTermination(3, TimeUnit.SECONDS);
269238
// All slots should be available
270-
assertWorkerSlotCount(
271-
MAX_CONCURRENT_WORKFLOW_TASK_EXECUTION_SIZE,
272-
MAX_CONCURRENT_ACTIVITY_EXECUTION_SIZE,
273-
MAX_CONCURRENT_LOCAL_ACTIVITY_EXECUTION_SIZE);
239+
// Used count here is actually -2 since the slots weren't marked used
240+
assertCurrentUsedCount(0, 0);
274241
}
275242

276243
@Test
277-
public void TestLocalActivitySlotHitsCapacity() throws InterruptedException {
244+
public void TestActivitySlotHitsCapacity() throws InterruptedException {
278245
testWorkflowRule.getTestEnvironment().start();
279246
WorkflowClient client = testWorkflowRule.getWorkflowClient();
280247
TestWorkflow workflow =
@@ -301,9 +268,6 @@ public void TestLocalActivitySlotHitsCapacity() throws InterruptedException {
301268
parallelSemBlocked.release(100);
302269
workflow.workflow(true);
303270
// All slots should be available
304-
assertWorkerSlotCount(
305-
MAX_CONCURRENT_WORKFLOW_TASK_EXECUTION_SIZE,
306-
MAX_CONCURRENT_ACTIVITY_EXECUTION_SIZE,
307-
MAX_CONCURRENT_LOCAL_ACTIVITY_EXECUTION_SIZE);
271+
assertCurrentUsedCount(0, 0);
308272
}
309273
}

temporal-sdk/src/test/java/io/temporal/testUtils/CountingSlotSupplier.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222

2323
import io.temporal.worker.tuning.*;
2424
import java.util.Optional;
25+
import java.util.concurrent.ConcurrentHashMap;
2526
import java.util.concurrent.atomic.AtomicInteger;
2627

2728
public class CountingSlotSupplier<SI extends SlotInfo> extends FixedSizeSlotSupplier<SI> {
2829
public final AtomicInteger reservedCount = new AtomicInteger();
2930
public final AtomicInteger releasedCount = new AtomicInteger();
3031
public final AtomicInteger usedCount = new AtomicInteger();
32+
public final ConcurrentHashMap.KeySetView<SlotPermit, Boolean> currentUsedSet =
33+
ConcurrentHashMap.newKeySet();
3134

3235
public CountingSlotSupplier(int numSlots) {
3336
super(numSlots);
@@ -52,12 +55,14 @@ public Optional<SlotPermit> tryReserveSlot(SlotReserveContext<SI> ctx) {
5255
@Override
5356
public void markSlotUsed(SlotMarkUsedContext<SI> ctx) {
5457
usedCount.incrementAndGet();
58+
currentUsedSet.add(ctx.getSlotPermit());
5559
super.markSlotUsed(ctx);
5660
}
5761

5862
@Override
5963
public void releaseSlot(SlotReleaseContext<SI> ctx) {
6064
super.releaseSlot(ctx);
65+
currentUsedSet.remove(ctx.getSlotPermit());
6166
releasedCount.incrementAndGet();
6267
}
6368
}

0 commit comments

Comments
 (0)