adutra commented on code in PR #4061:
URL: https://github.com/apache/polaris/pull/4061#discussion_r3046518138
##########
CHANGELOG.md:
##########
@@ -73,6 +73,7 @@ request adding CHANGELOG notes for breaking (!) changes and
possibly other secti
- Changed from Poetry to UV for Python package management.
- Exclude KMS policies when KMS is not being used for S3.
- Improved default KMS permission handling to better distinguish read-only and
read-write access.
+- Request IDs are now propagated to async task threads via CDI-injectable
`RequestIdSupplier`, so task log messages carry the originating request's ID.
Context propagation across task boundaries is now handled by the pluggable
`AsyncContextPropagator` SPI.
Review Comment:
```suggestion
- Request IDs are now propagated to async task threads via CDI-injectable
`RequestIdSupplier`, so task log messages carry the originating request's ID.
Context propagation across task boundaries is now handled by the pluggable
`TaskContextPropagator` SPI.
```
##########
runtime/service/src/main/java/org/apache/polaris/service/task/TaskExecutorImpl.java:
##########
@@ -159,27 +149,27 @@ public void addTaskHandlerContext(long taskEntityId,
CallContext callContext) {
// Capture the metadata now in order to capture the principal and request
ID, if any.
PolarisEventMetadata eventMetadata = eventMetadataFactory.create();
- tryHandleTask(taskEntityId, clone, eventMetadata, null, 1);
+
+ // Capture request-scoped context for propagation into the task thread.
+ TaskContextPropagator.CapturedTaskContext captured =
taskContextPropagator.capture();
Review Comment:
With this change, do we still need to clone `CallContext` on line 148? Do we
need `CallContext` in this class _at all_?
##########
runtime/service/src/main/java/org/apache/polaris/service/context/catalog/RequestIdHolder.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.service.context.catalog;
+
+import jakarta.annotation.Nullable;
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.enterprise.inject.Produces;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.polaris.core.context.RequestIdSupplier;
+
+/**
+ * Request-scoped holder for the request ID.
+ *
+ * <p>On HTTP request threads the request ID is set by {@code
RequestIdFilter}. On async task
+ * threads a new CDI request scope is activated by {@code TaskExecutorImpl},
and the request ID from
+ * the originating request is propagated into this holder by {@code
RequestIdPropagator}.
+ *
+ * <p>The holder exposes the stored ID as a {@link RequestIdSupplier} produced
bean so that any
+ * component injecting {@code Instance<RequestIdSupplier>} can resolve it
without depending on
+ * JAX-RS internals.
+ */
+@RequestScoped
+public class RequestIdHolder {
+
+ private final AtomicReference<String> requestId = new AtomicReference<>();
+
+ @Nullable
+ public String get() {
+ return requestId.get();
+ }
+
+ /**
+ * Produces a {@link RequestIdSupplier} for the current request scope. The
returned supplier reads
+ * the request ID from this holder at the time it is called, so it reflects
any value set after
+ * this method is first invoked.
+ */
+ @Produces
+ @RequestScoped
+ public RequestIdSupplier getRequestIdSupplier() {
+ return () -> requestId.get();
Review Comment:
nit:
```suggestion
return requestId::get
```
##########
runtime/service/src/main/java/org/apache/polaris/service/context/catalog/RequestIdHolder.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.service.context.catalog;
+
+import jakarta.annotation.Nullable;
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.enterprise.inject.Produces;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.polaris.core.context.RequestIdSupplier;
+
+/**
+ * Request-scoped holder for the request ID.
+ *
+ * <p>On HTTP request threads the request ID is set by {@code
RequestIdFilter}. On async task
+ * threads a new CDI request scope is activated by {@code TaskExecutorImpl},
and the request ID from
+ * the originating request is propagated into this holder by {@code
RequestIdPropagator}.
+ *
+ * <p>The holder exposes the stored ID as a {@link RequestIdSupplier} produced
bean so that any
+ * component injecting {@code Instance<RequestIdSupplier>} can resolve it
without depending on
+ * JAX-RS internals.
+ */
+@RequestScoped
+public class RequestIdHolder {
+
+ private final AtomicReference<String> requestId = new AtomicReference<>();
+
+ @Nullable
+ public String get() {
+ return requestId.get();
+ }
+
+ /**
+ * Produces a {@link RequestIdSupplier} for the current request scope. The
returned supplier reads
+ * the request ID from this holder at the time it is called, so it reflects
any value set after
+ * this method is first invoked.
+ */
+ @Produces
+ @RequestScoped
+ public RequestIdSupplier getRequestIdSupplier() {
Review Comment:
Do we need both a `RequestIdHolder` and a `RequestIdSupplier`? I think the
holder could now just expose the raw request ID. The supplier creates an
unnecessary indirection, it seems.
##########
runtime/service/src/main/java/org/apache/polaris/service/task/TaskContextPropagator.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.service.task;
+
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.apache.polaris.core.auth.ImmutablePolarisPrincipal;
+import org.apache.polaris.core.auth.PolarisPrincipal;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.service.context.catalog.PolarisPrincipalHolder;
+import org.apache.polaris.service.context.catalog.RealmContextHolder;
+import org.apache.polaris.service.context.catalog.RequestIdHolder;
+
+/**
+ * Captures request-scoped context on the calling thread and restores it
inside a task thread's CDI
+ * request scope.
+ *
+ * <p>This follows the same pattern as {@link
org.apache.polaris.service.config.Bootstrapper}:
+ * context is captured before async submission and explicitly restored after a
new CDI request scope
+ * is activated on the task thread.
+ *
+ * <p>{@link #capture()} reads the current realm, principal, and request ID.
{@link
+ * #restore(CapturedTaskContext)} writes them into the holders of the task
thread's fresh request
+ * scope.
+ */
+@ApplicationScoped
+class TaskContextPropagator {
+
+ private final RealmContextHolder realmContextHolder;
+ private final PolarisPrincipalHolder polarisPrincipalHolder;
+ private final RequestIdHolder requestIdHolder;
+ private final PolarisPrincipal polarisPrincipal;
+
+ @SuppressWarnings("unused") // Required by CDI
+ protected TaskContextPropagator() {
+ this(null, null, null, null);
+ }
+
+ @Inject
+ TaskContextPropagator(
+ RealmContextHolder realmContextHolder,
+ PolarisPrincipalHolder polarisPrincipalHolder,
+ RequestIdHolder requestIdHolder,
+ PolarisPrincipal polarisPrincipal) {
+ this.realmContextHolder = realmContextHolder;
+ this.polarisPrincipalHolder = polarisPrincipalHolder;
+ this.requestIdHolder = requestIdHolder;
+ this.polarisPrincipal = polarisPrincipal;
+ }
+
+ /**
+ * Captures the current request-scoped context for later propagation to a
task thread.
+ *
+ * <p>Must be called on a thread with an active CDI request scope (e.g. an
HTTP request thread or
+ * a task thread that already has context restored). The principal is cloned
into an immutable
+ * snapshot so the captured context is independent of the originating
scope's lifecycle.
+ */
+ CapturedTaskContext capture() {
+ return new CapturedTaskContext(
+ realmContextHolder.get(),
+ ImmutablePolarisPrincipal.builder().from(polarisPrincipal).build(),
+ requestIdHolder.get());
+ }
+
+ /**
+ * Restores previously captured context into the current (task) thread's
request scope.
+ *
+ * <p>Must be called inside an active CDI request scope on the task thread
(e.g. inside a method
+ * annotated with {@code @ActivateRequestContext}).
+ */
+ void restore(CapturedTaskContext context) {
+ realmContextHolder.set(context.realmContext());
+ polarisPrincipalHolder.set(context.principal());
+ requestIdHolder.set(context.requestId());
+ }
+
+ /**
+ * Immutable snapshot of request-scoped context captured for propagation
across async boundaries.
+ */
+ record CapturedTaskContext(
Review Comment:
This imo should be public as it is being exposed outside the package scope:
in the protected `handleTaskWithTracing` method.
It should probably be made top-level as well.
##########
runtime/service/src/main/java/org/apache/polaris/service/events/PolarisEventMetadataFactory.java:
##########
@@ -23,24 +23,23 @@
import io.quarkus.security.identity.CurrentIdentityAssociation;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.context.ContextNotActiveException;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import java.time.Clock;
import java.util.Map;
import java.util.Optional;
import org.apache.polaris.core.auth.PolarisPrincipal;
import org.apache.polaris.core.context.RealmContext;
-import org.apache.polaris.service.tracing.RequestIdFilter;
-import org.jboss.resteasy.reactive.server.core.CurrentRequestManager;
-import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
-import org.jboss.resteasy.reactive.server.jaxrs.ContainerRequestContextImpl;
+import org.apache.polaris.core.context.RequestIdSupplier;
@ApplicationScoped
public class PolarisEventMetadataFactory {
@Inject Clock clock;
@Inject CurrentIdentityAssociation currentIdentityAssociation;
@Inject Instance<RealmContext> realmContext;
+ @Inject RequestIdSupplier requestIdSupplier;
Review Comment:
I think `RequestIdHolder` is enough (see my other comment).
--
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]