luigidemasi commented on code in PR #30711:
URL: https://github.com/apache/camel/pull/30711#discussion_r4677716193
##########
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:
**P1 — Release the slot when cancellation wins the race to start.** I
confirmed the case @davsclaus raised by holding both workers before they could
enter their callables. Two requests timed out, the expert received zero calls,
and both workers then drained their cancelled tasks. A fresh request still
returned `Busy`. Once `acquired` is cleared here, only the callable can release
the slot, and a cancelled `FutureTask` skips that callable. Please make
cancellation release the slot exactly once when it wins this race. A provider
that has already started should retain its slot until it exits.
_Codex on behalf of @luigidemasi._
##########
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:
**P2 — Wake queued callers when the console stops.** With this queue,
`shutdownNow()` can return an accepted task that never started. `doStop()`
discards that list, leaving the caller blocked in `task.get(...)`. I stopped
the console after confirming the third request was queued. It waited its full
2.5-second deadline and reported an evaluation timeout. The default deadline is
50 seconds. Please cancel each future removed from the queue and release its
slot through the same pre-start cancellation path.
_Codex on behalf of @luigidemasi._
##########
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:
**P2 — Keep old completions from decrementing the new active count.** I held
a provider call across stop/start, let two calls enter the new pool, and then
allowed the old call to finish. Its `release()` changed `active` from 2 to 1
while both new evaluations were still running. The third caller entered the
queue and timed out instead of receiving `Busy` immediately. The clamp at zero
allows this decrement. Once every accepted request releases exactly once,
keeping the count across restart would also let you remove the reset and clamp.
_Codex on behalf of @luigidemasi._
--
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]