refactor: 각 어댑터터별 리펙토링 진행
This commit is contained in:
+102
-3
@@ -2,7 +2,10 @@ package dev.caskeleton.messaging.policy;
|
||||
|
||||
import dev.caskeleton.messaging.api.error.MessageBackpressureException;
|
||||
import dev.caskeleton.messaging.api.error.MessageTooLargeException;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* The single gate every publish passes before any transport work begins.
|
||||
@@ -16,22 +19,49 @@ import java.util.Objects;
|
||||
* under the same message id without risking a duplicate. That is the reason admission lives at the
|
||||
* front of the pipeline instead of being folded into the adapter's error handling, where a partial
|
||||
* write would already have made the outcome unknowable.
|
||||
*
|
||||
* <p>Two ceilings, because one is not enough. The per-destination ceiling stops a single slow
|
||||
* downstream from consuming every permit in the process, and the process-wide ceiling stops the sum
|
||||
* of well-behaved destinations from exhausting memory — without it, adding a destination silently
|
||||
* raises what the process can be holding at once.
|
||||
*/
|
||||
public final class MessagingAdmissionController {
|
||||
|
||||
private final PayloadLimitGuard payloadGuard;
|
||||
private final InFlightLimiter limiter;
|
||||
private final int perDestinationLimit;
|
||||
private final Map<String, AtomicInteger> perDestination = new ConcurrentHashMap<>();
|
||||
private volatile boolean acceptingNewWork = true;
|
||||
|
||||
/**
|
||||
* Creates an admission controller.
|
||||
* Creates an admission controller whose only ceiling is the process-wide one.
|
||||
*
|
||||
* @param payloadGuard the payload size guard
|
||||
* @param limiter the in-flight permit pool
|
||||
*/
|
||||
public MessagingAdmissionController(PayloadLimitGuard payloadGuard, InFlightLimiter limiter) {
|
||||
this(payloadGuard, limiter, limiter.limit());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an admission controller with a per-destination ceiling.
|
||||
*
|
||||
* @param payloadGuard the payload size guard
|
||||
* @param limiter the in-flight permit pool
|
||||
* @param perDestinationLimit how much in-flight work one destination may hold
|
||||
*/
|
||||
public MessagingAdmissionController(
|
||||
PayloadLimitGuard payloadGuard, InFlightLimiter limiter, int perDestinationLimit) {
|
||||
this.payloadGuard = Objects.requireNonNull(payloadGuard, "payloadGuard must not be null");
|
||||
this.limiter = Objects.requireNonNull(limiter, "limiter must not be null");
|
||||
if (perDestinationLimit < 1) {
|
||||
throw new IllegalArgumentException("perDestinationLimit must be at least 1");
|
||||
}
|
||||
if (perDestinationLimit > limiter.limit()) {
|
||||
throw new IllegalArgumentException(
|
||||
"perDestinationLimit cannot exceed the process-wide limit, or it is not a limit");
|
||||
}
|
||||
this.perDestinationLimit = perDestinationLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,15 +81,27 @@ public final class MessagingAdmissionController {
|
||||
"SHUTTING_DOWN", "the messaging runtime stopped accepting new publishes");
|
||||
}
|
||||
|
||||
if (!reserve(destination)) {
|
||||
throw new MessageBackpressureException(
|
||||
"DESTINATION_IN_FLIGHT_LIMIT_EXCEEDED",
|
||||
"%s already holds its %d in-flight publishes; one slow destination does not get to"
|
||||
.formatted(destination, perDestinationLimit)
|
||||
+ " consume the whole process");
|
||||
}
|
||||
|
||||
boolean acquired;
|
||||
try {
|
||||
acquired = limiter.tryAcquire();
|
||||
} catch (InterruptedException interrupted) {
|
||||
// The destination slot was taken a moment ago and no publish will use it, so it goes back
|
||||
// here: a slot leaked per interruption shrinks the destination's ceiling until it is zero.
|
||||
release(destination);
|
||||
Thread.currentThread().interrupt();
|
||||
throw new MessageBackpressureException(
|
||||
"ADMISSION_INTERRUPTED", "interrupted while waiting for a publish permit");
|
||||
}
|
||||
if (!acquired) {
|
||||
release(destination);
|
||||
throw new MessageBackpressureException(
|
||||
"IN_FLIGHT_LIMIT_EXCEEDED",
|
||||
"no publish permit became available for %s within the bounded wait"
|
||||
@@ -67,8 +109,24 @@ public final class MessagingAdmissionController {
|
||||
}
|
||||
}
|
||||
|
||||
/** Releases the permit taken by a successful {@link #admit}. */
|
||||
public void complete() {
|
||||
/**
|
||||
* Releases the permit taken by a successful {@link #admit}.
|
||||
*
|
||||
* <p>The destination is named because the caller cannot release a permit it never took: a release
|
||||
* carrying a different destination would return a slot to a queue that has nothing outstanding
|
||||
* and leave the real one occupied forever.
|
||||
*
|
||||
* @param destination the destination this publish was admitted for
|
||||
*/
|
||||
public void complete(String destination) {
|
||||
Objects.requireNonNull(destination, "destination must not be null");
|
||||
if (!release(destination)) {
|
||||
// A completion for a destination that holds nothing: either it names the wrong destination or
|
||||
// it is a second completion for the same publish. Returning the process permit anyway frees
|
||||
// one nobody took, and the process-wide ceiling then reads below what is really in flight and
|
||||
// admits more work than the process can carry.
|
||||
return;
|
||||
}
|
||||
limiter.release();
|
||||
}
|
||||
|
||||
@@ -98,4 +156,45 @@ public final class MessagingAdmissionController {
|
||||
public int inFlight() {
|
||||
return limiter.inFlight();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns how much in-flight work one destination holds.
|
||||
*
|
||||
* @param destination the logical destination name
|
||||
* @return the destination's in-flight count
|
||||
*/
|
||||
public int inFlight(String destination) {
|
||||
AtomicInteger counter = perDestination.get(destination);
|
||||
return counter == null ? 0 : counter.get();
|
||||
}
|
||||
|
||||
private boolean reserve(String destination) {
|
||||
AtomicInteger counter = perDestination.computeIfAbsent(destination, key -> new AtomicInteger());
|
||||
while (true) {
|
||||
int current = counter.get();
|
||||
if (current >= perDestinationLimit) {
|
||||
return false;
|
||||
}
|
||||
if (counter.compareAndSet(current, current + 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives one slot back to a destination.
|
||||
*
|
||||
* @return true when this call actually released a slot the destination was holding
|
||||
*/
|
||||
private boolean release(String destination) {
|
||||
AtomicInteger counter = perDestination.get(destination);
|
||||
if (counter == null) {
|
||||
return false;
|
||||
}
|
||||
int before = counter.getAndUpdate(current -> current > 0 ? current - 1 : current);
|
||||
// Drop the entry at zero, atomically, so the map does not accumulate one counter per
|
||||
// destination ever published to for the life of the process.
|
||||
perDestination.computeIfPresent(destination, (key, value) -> value.get() == 0 ? null : value);
|
||||
return before > 0;
|
||||
}
|
||||
}
|
||||
|
||||
+79
-4
@@ -26,7 +26,7 @@ class MessagingAdmissionControllerTest {
|
||||
controller.admit(DESTINATION, 100);
|
||||
|
||||
assertThat(controller.inFlight()).isEqualTo(1);
|
||||
controller.complete();
|
||||
controller.complete(DESTINATION);
|
||||
assertThat(controller.inFlight()).isZero();
|
||||
}
|
||||
|
||||
@@ -81,8 +81,8 @@ class MessagingAdmissionControllerTest {
|
||||
void aReleaseWithoutAnAdmitCannotRaiseTheCeiling() {
|
||||
MessagingAdmissionController controller = controller(1);
|
||||
|
||||
controller.complete();
|
||||
controller.complete();
|
||||
controller.complete(DESTINATION);
|
||||
controller.complete(DESTINATION);
|
||||
controller.admit(DESTINATION, 100);
|
||||
|
||||
assertThatThrownBy(() -> controller.admit(DESTINATION, 100))
|
||||
@@ -90,6 +90,81 @@ class MessagingAdmissionControllerTest {
|
||||
.isInstanceOf(MessageBackpressureException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void oneSlowDestinationCannotOccupyEveryPermit() {
|
||||
MessagingAdmissionController controller =
|
||||
new MessagingAdmissionController(
|
||||
new PayloadLimitGuard(new PayloadPolicy(1024, 512)),
|
||||
new InFlightLimiter(4, Duration.ZERO),
|
||||
2);
|
||||
|
||||
controller.admit(DESTINATION, 100);
|
||||
controller.admit(DESTINATION, 100);
|
||||
|
||||
assertThatThrownBy(() -> controller.admit(DESTINATION, 100))
|
||||
.as("a single slow downstream taking every permit is how one destination stops the others")
|
||||
.isInstanceOf(MessageBackpressureException.class);
|
||||
assertThat(controller.inFlight("other.destination")).isZero();
|
||||
controller.admit("other.destination", 100);
|
||||
assertThat(controller.inFlight()).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRefusedAdmissionLeavesNoDestinationSlotBehind() {
|
||||
MessagingAdmissionController controller =
|
||||
new MessagingAdmissionController(
|
||||
new PayloadLimitGuard(new PayloadPolicy(1024, 512)),
|
||||
new InFlightLimiter(1, Duration.ZERO),
|
||||
1);
|
||||
controller.admit(DESTINATION, 100);
|
||||
|
||||
assertThatThrownBy(() -> controller.admit("other.destination", 100))
|
||||
.isInstanceOf(MessageBackpressureException.class);
|
||||
|
||||
assertThat(controller.inFlight("other.destination"))
|
||||
.as("a slot leaked per refusal shrinks the destination's ceiling until it is zero")
|
||||
.isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void completingReturnsBothTheProcessPermitAndTheDestinationSlot() {
|
||||
MessagingAdmissionController controller =
|
||||
new MessagingAdmissionController(
|
||||
new PayloadLimitGuard(new PayloadPolicy(1024, 512)),
|
||||
new InFlightLimiter(4, Duration.ZERO),
|
||||
2);
|
||||
controller.admit(DESTINATION, 100);
|
||||
|
||||
controller.complete(DESTINATION);
|
||||
|
||||
assertThat(controller.inFlight()).isZero();
|
||||
assertThat(controller.inFlight(DESTINATION)).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void completingAnUnknownDestinationFreesNoProcessPermit() {
|
||||
MessagingAdmissionController controller = controller(2);
|
||||
controller.admit(DESTINATION, 100);
|
||||
|
||||
controller.complete("something-else");
|
||||
|
||||
assertThat(controller.inFlight())
|
||||
.as("a permit nobody took cannot be given back; doing so makes the ceiling fiction")
|
||||
.isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSecondCompletionForOnePublishFreesNoSecondPermit() {
|
||||
MessagingAdmissionController controller = controller(4);
|
||||
controller.admit(DESTINATION, 100);
|
||||
|
||||
controller.complete(DESTINATION);
|
||||
controller.complete(DESTINATION);
|
||||
|
||||
assertThat(controller.inFlight()).isZero();
|
||||
assertThat(controller.inFlight(DESTINATION)).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBatchIsBoundedByCountAndByAggregateBytes() {
|
||||
PayloadLimitGuard guard = new PayloadLimitGuard(new PayloadPolicy(1024, 512));
|
||||
@@ -113,7 +188,7 @@ class MessagingAdmissionControllerTest {
|
||||
|
||||
Thread waiter = new Thread(() -> controller.admit(DESTINATION, 100));
|
||||
waiter.start();
|
||||
controller.complete();
|
||||
controller.complete(DESTINATION);
|
||||
waiter.join(Duration.ofSeconds(5).toMillis());
|
||||
|
||||
assertThat(waiter.isAlive()).isFalse();
|
||||
|
||||
Reference in New Issue
Block a user