fix(secretsmanager): bound rotation threads and lock storage (#4344)

* fix(secretsmanager): bound rotation threads and lock storage

* fix(secretsmanager): keep rotation threads non-daemon and drop the lock count test

* chore: rerun CI

* fix: set rotation thread flags explicitly and pin the test to the pool size
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-24 21:15:29 -04:00
1 parent 8f606c3587
commit 3096a3cf7c
2 files changed
+126 -8

No files matched your search

@@ -23,20 +23,20 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import io.github.hectorvent.floci.core.resource.ExplorerResource;
import io.github.hectorvent.floci.core.resource.ResourceProvider;
@@ -63,11 +63,44 @@ public class SecretsManagerService implements ResourceProvider {
private final LambdaService lambdaService;
private final ObjectMapper objectMapper;
private final KmsService kmsService;
private final ExecutorService rotationExecutor = Executors.newCachedThreadPool();
private final ConcurrentHashMap<String, Object> rotationLocks = new ConcurrentHashMap<>();
// Fixed threads with an unbounded queue: a burst of rotations is queued, not rejected,
// because RotateSecret documents no overload error.
static final int ROTATION_EXECUTOR_POOL_SIZE = Math.max(4, Runtime.getRuntime().availableProcessors());
private final ExecutorService rotationExecutor =
Executors.newFixedThreadPool(ROTATION_EXECUTOR_POOL_SIZE, new RotationThreadFactory());
// Fixed stripes instead of one lock per ARN, which was never removed. Secrets that share a
// stripe only contend; no lock is held while a rotation Lambda runs.
private static final int ROTATION_LOCK_STRIPE_COUNT = 32;
private final Object[] rotationLockStripes = newLockStripes(ROTATION_LOCK_STRIPE_COUNT);
private static Object[] newLockStripes(int count) {
Object[] stripes = new Object[count];
for (int i = 0; i < count; i++) {
stripes[i] = new Object();
}
return stripes;
}
private Object lockFor(String secretArn) {
return rotationLocks.computeIfAbsent(secretArn, k -> new Object());
int index = Math.floorMod(secretArn.hashCode(), rotationLockStripes.length);
return rotationLockStripes[index];
}
/** Names rotation threads for diagnostics. */
private static final class RotationThreadFactory implements ThreadFactory {
private final AtomicInteger nextId = new AtomicInteger(1);
@Override
public Thread newThread(Runnable runnable) {
// Set explicitly: a new thread otherwise inherits these from whichever thread grows the
// pool, and a daemon rotation thread could be cut off mid-rotation at JVM exit.
Thread thread = new Thread(runnable, "secretsmanager-rotation-" + nextId.getAndIncrement());
thread.setDaemon(false);
thread.setPriority(Thread.NORM_PRIORITY);
return thread;
}
}
@Inject