mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
1 parent
3dee17e51d
commit
438ae619fd
5 files changed
+86
-15
No files matched your search
+18
-12
@@ -13,6 +13,7 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
@@ -27,8 +28,8 @@ import java.util.concurrent.TimeoutException;
|
||||
public class LambdaExecutorService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(LambdaExecutorService.class);
|
||||
/** Grace period beyond the configured function timeout to allow the runtime to report back. */
|
||||
private static final int TIMEOUT_GRACE_SECONDS = 2;
|
||||
/** Extra time for a newly started runtime to request its first invocation. */
|
||||
private static final int RUNTIME_DISPATCH_GRACE_SECONDS = 2;
|
||||
|
||||
private final WarmPool warmPool;
|
||||
private final ObjectMapper objectMapper;
|
||||
@@ -99,26 +100,31 @@ public class LambdaExecutorService {
|
||||
|
||||
handle.getRuntimeApiServer().enqueue(invocation);
|
||||
|
||||
InvokeResult result = invocation.getResultFuture()
|
||||
.get(fn.getTimeout() + TIMEOUT_GRACE_SECONDS, TimeUnit.SECONDS);
|
||||
java.util.concurrent.CompletableFuture.anyOf(
|
||||
invocation.getDispatchedFuture(), invocation.getResultFuture())
|
||||
.get(fn.getTimeout() + RUNTIME_DISPATCH_GRACE_SECONDS, TimeUnit.SECONDS);
|
||||
InvokeResult result = invocation.getResultFuture().get();
|
||||
|
||||
warmPool.release(handle);
|
||||
return result;
|
||||
|
||||
} catch (TimeoutException e) {
|
||||
LOG.warnv("Function {0} timed out after {1}s", fn.getFunctionName(), fn.getTimeout());
|
||||
warmPool.destroyHandle(handle);
|
||||
return new InvokeResult(200, "Unhandled",
|
||||
buildErrorPayload("Task timed out after " + fn.getTimeout() + " seconds", "Function.TimedOut"),
|
||||
null, requestId);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
warmPool.destroyHandle(handle);
|
||||
return new InvokeResult(200, "Unhandled", buildErrorPayload("Invocation interrupted", "Interrupted"), null, requestId);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Invocation error for function {0}: {1}", fn.getFunctionName(), e.getMessage());
|
||||
Throwable cause = e instanceof ExecutionException && e.getCause() != null ? e.getCause() : e;
|
||||
if (cause instanceof TimeoutException) {
|
||||
LOG.warnv("Function {0} timed out after {1}s", fn.getFunctionName(), fn.getTimeout());
|
||||
warmPool.destroyHandle(handle);
|
||||
return new InvokeResult(200, "Unhandled",
|
||||
buildErrorPayload("Task timed out after " + fn.getTimeout() + " seconds", "Function.TimedOut"),
|
||||
null, requestId);
|
||||
}
|
||||
LOG.warnv("Invocation error for function {0}: {1}", fn.getFunctionName(), cause.getMessage());
|
||||
warmPool.destroyHandle(handle);
|
||||
return new InvokeResult(200, "Unhandled", buildErrorPayload(e.getMessage(), "InvocationError"), null, requestId);
|
||||
return new InvokeResult(200, "Unhandled",
|
||||
buildErrorPayload(cause.getMessage(), "InvocationError"), null, requestId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+15
-1
@@ -1,27 +1,41 @@
|
||||
package io.github.hectorvent.floci.services.lambda.model;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class PendingInvocation {
|
||||
|
||||
private final String requestId;
|
||||
private final byte[] payload;
|
||||
private final long deadlineMs;
|
||||
private final long timeoutMs;
|
||||
private volatile long deadlineMs;
|
||||
private final String functionArn;
|
||||
private final CompletableFuture<InvokeResult> resultFuture;
|
||||
private final CompletableFuture<Void> dispatchedFuture = new CompletableFuture<>();
|
||||
|
||||
public PendingInvocation(String requestId, byte[] payload, long deadlineMs,
|
||||
String functionArn, CompletableFuture<InvokeResult> resultFuture) {
|
||||
this.requestId = requestId;
|
||||
this.payload = payload;
|
||||
this.deadlineMs = deadlineMs;
|
||||
this.timeoutMs = Math.max(0, deadlineMs - System.currentTimeMillis());
|
||||
this.functionArn = functionArn;
|
||||
this.resultFuture = resultFuture;
|
||||
}
|
||||
|
||||
public void prepareForDispatch() {
|
||||
deadlineMs = System.currentTimeMillis() + timeoutMs;
|
||||
}
|
||||
|
||||
public void markDispatched() {
|
||||
resultFuture.orTimeout(Math.max(0, deadlineMs - System.currentTimeMillis()), TimeUnit.MILLISECONDS);
|
||||
dispatchedFuture.complete(null);
|
||||
}
|
||||
|
||||
public String getRequestId() { return requestId; }
|
||||
public byte[] getPayload() { return payload; }
|
||||
public long getDeadlineMs() { return deadlineMs; }
|
||||
public String getFunctionArn() { return functionArn; }
|
||||
public CompletableFuture<InvokeResult> getResultFuture() { return resultFuture; }
|
||||
public CompletableFuture<Void> getDispatchedFuture() { return dispatchedFuture; }
|
||||
}
|
||||
+5
-1
@@ -868,6 +868,7 @@ public class RuntimeApiServer {
|
||||
|
||||
private void sendInvocation(RoutingContext ctx, PendingInvocation invocation) {
|
||||
beforeSendInvocationWrite(invocation.getRequestId());
|
||||
invocation.prepareForDispatch();
|
||||
// Invariant: callers put the invocation in inFlight under the lock before
|
||||
// dispatching, so no inFlight.put here.
|
||||
byte[] payload = invocation.getPayload();
|
||||
@@ -891,7 +892,10 @@ public class RuntimeApiServer {
|
||||
}
|
||||
// Gated on the successful write: onFailure below requeues the invocation, and a fan-out
|
||||
// before the write would emit a second INVOKE for the same requestId on redelivery.
|
||||
write.onSuccess(v -> notifyExtensionsOfInvoke(invocation));
|
||||
write.onSuccess(v -> {
|
||||
invocation.markDispatched();
|
||||
notifyExtensionsOfInvoke(invocation);
|
||||
});
|
||||
write.onFailure(err -> {
|
||||
// Requeue for the next /next poller; mirrors enqueue()'s ctx-ended branch.
|
||||
// Skip if quiesce() beat us — it already swept inFlight and settled the
|
||||
|
||||
Reference in new issue
Block a user