Skip to content

Commit

Permalink
detect bulkhead capacity overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Jan 6, 2025
1 parent 9b16929 commit 82128e2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ public BulkheadBuilder<T, R> onFinished(Runnable callback) {

@Override
public Builder<T, R> done() {
try {
Math.addExact(limit, queueSize);
} catch (ArithmeticException e) {
throw new IllegalStateException("Bulkhead capacity overflow, " + limit + " + " + queueSize
+ " = " + (limit + queueSize));
}

parent.bulkheadBuilder = this;
return parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ public BulkheadBuilder onFinished(Runnable callback) {

@Override
public Builder done() {
try {
Math.addExact(limit, queueSize);
} catch (ArithmeticException e) {
throw new IllegalStateException("Bulkhead capacity overflow, " + limit + " + " + queueSize
+ " = " + (limit + queueSize));
}

parent.bulkheadBuilder = this;
return parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ public BulkheadBuilder<T> onFinished(Runnable callback) {

@Override
public Builder<T> done() {
try {
Math.addExact(limit, queueSize);
} catch (ArithmeticException e) {
throw new IllegalStateException("Bulkhead capacity overflow, " + limit + " + " + queueSize
+ " = " + (limit + queueSize));
}

parent.bulkheadBuilder = this;
return parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Bulkhead(FaultToleranceStrategy<V> delegate, String description, int size
this.delegate = delegate;
this.description = description;
this.queue = new ConcurrentLinkedDeque<>();
this.capacitySemaphore = new Semaphore(size + queueSize, true);
this.capacitySemaphore = new Semaphore(Math.addExact(size, queueSize), true);
this.workSemaphore = new Semaphore(size, true);
this.syncQueueing = syncQueueing;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ default void validate() {
throw new FaultToleranceDefinitionException(INVALID_BULKHEAD_ON + method()
+ ": waitingTaskQueue shouldn't be lower than 1");
}

try {
Math.addExact(value(), waitingTaskQueue());
} catch (ArithmeticException e) {
throw new FaultToleranceDefinitionException(INVALID_BULKHEAD_ON + method()
+ ": bulkhead capacity overflow, " + value() + " + " + waitingTaskQueue()
+ " = " + (value() + waitingTaskQueue()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.smallrye.faulttolerance.bulkhead.capacity.overflow;

import io.smallrye.faulttolerance.util.ExpectedDeploymentException;
import io.smallrye.faulttolerance.util.FaultToleranceBasicTest;
import jakarta.enterprise.inject.spi.DefinitionException;
import org.junit.jupiter.api.Test;

@FaultToleranceBasicTest
@ExpectedDeploymentException(DefinitionException.class)
public class BulkheadCapacityOverflowTest {
@Test
public void test(MyService ignored) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.smallrye.faulttolerance.bulkhead.capacity.overflow;

import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.faulttolerance.Asynchronous;
import org.eclipse.microprofile.faulttolerance.Bulkhead;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.completedFuture;

@ApplicationScoped
public class MyService {
@Asynchronous
@Bulkhead(value = 1, waitingTaskQueue = Integer.MAX_VALUE)
public CompletionStage<String> hello() {
return completedFuture("hello");
}
}

0 comments on commit 82128e2

Please sign in to comment.