davsclaus commented on code in PR #30711:
URL: https://github.com/apache/camel/pull/30711#discussion_r4680003673
##########
components/camel-ai/camel-semantic/src/main/java/org/apache/camel/semantic/SemanticEvaluateConsole.java:
##########
@@ -125,12 +144,25 @@ protected Map<String, Object> doCallJson(Map<String,
Object> options) {
SemanticAuditService.Request request
= audit.request(options.containsKey(OPERATION) ?
"direct_sample" : "definition_sample");
String failure;
+ boolean acquired = false;
try {
long timeout = optionLong(options, TIMEOUT, 50000);
if (timeout < 1 || timeout > 50000) {
throw new IllegalArgumentException("Evaluation timeout must be
between 1 and 50000 milliseconds");
}
- task = executor().submit(() -> evaluate(options, request));
+ if (!tryAcquire()) {
+ throw new RejectedExecutionException("Busy: semantic
evaluations are already running");
+ }
+ acquired = true;
+ ExecutorService exec = executor();
+ task = exec.submit(() -> {
+ try {
+ return evaluate(options, request);
+ } finally {
+ release();
+ }
+ });
+ acquired = false;
Review Comment:
Fixed in 445575435d: the `claimed` flag lets exactly one side release. The
callable claims it before `evaluate`, and the caller claims it in `finally`
after `task.cancel(true)`, so a task cancelled before it starts gives its slot
back once.
_Claude Code on behalf of davsclaus_
##########
components/camel-ai/camel-semantic/src/main/java/org/apache/camel/semantic/SemanticEvaluateConsole.java:
##########
@@ -76,6 +94,7 @@ private synchronized ExecutorService executor() throws
Exception {
@Override
protected synchronized void doStart() throws Exception {
stopped = false;
+ active.set(0);
Review Comment:
Fixed in 445575435d: `doStart()` no longer resets `active`, and `release()`
no longer clamps. Each accepted request releases exactly once, so an evaluation
still running on the old executor keeps its slot until it ends.
`oldEvaluationFinishingAfterStopStartMaintainsSlotAccounting` covers it.
_Claude Code on behalf of davsclaus_
##########
components/camel-ai/camel-semantic/src/main/java/org/apache/camel/semantic/SemanticEvaluateConsole.java:
##########
@@ -58,16 +59,33 @@ public class SemanticEvaluateConsole extends
AbstractDevConsole {
javaType = "java.lang.Long")
public static final String TIMEOUT = "timeout";
+ private final AtomicInteger active = new AtomicInteger();
private ExecutorService executor;
// A resolver-created console may be called without starting it; only an
explicit stop rejects calls.
private boolean stopped;
+ private boolean tryAcquire() {
+ while (true) {
+ int count = active.get();
+ if (count >= 2) {
+ return false;
+ }
+ if (active.compareAndSet(count, count + 1)) {
+ return true;
+ }
+ }
+ }
+
+ private void release() {
+ active.updateAndGet(count -> Math.max(0, count - 1));
+ }
+
private synchronized ExecutorService executor() throws Exception {
if (stopped || isStopping()) {
throw new IllegalStateException("Semantic evaluation console is
stopping or stopped");
}
if (executor == null) {
- executor = new
ThreadPoolBuilder(getCamelContext()).poolSize(2).maxPoolSize(2).maxQueueSize(0)
+ executor = new
ThreadPoolBuilder(getCamelContext()).poolSize(2).maxPoolSize(2).maxQueueSize(2)
Review Comment:
Fixed in 445575435d: `doStop()` cancels each future that `shutdownNow`
returns, so a queued caller gets a `CancellationException` straight away and
releases its slot through the same claim path.
_Claude Code on behalf of davsclaus_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]