adutra commented on code in PR #2602:
URL: https://github.com/apache/polaris/pull/2602#discussion_r2379465883


##########
runtime/service/src/main/java/org/apache/polaris/service/tracing/RequestIdFilter.java:
##########
@@ -0,0 +1,50 @@
+/*
+ * 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.tracing;
+
+import jakarta.annotation.Priority;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.container.PreMatching;
+import jakarta.ws.rs.ext.Provider;
+import org.apache.polaris.service.config.FilterPriorities;
+import org.apache.polaris.service.logging.LoggingConfiguration;
+
+@PreMatching
+@ApplicationScoped
+@Priority(FilterPriorities.REQUEST_ID_FILTER)
+@Provider
+public class RequestIdFilter implements ContainerRequestFilter {
+  @Inject RequestIdGenerator requestIdGenerator;

Review Comment:
   nit: move this line down to line 40



##########
runtime/service/src/test/java/org/apache/polaris/service/admin/RequestIdHeaderTest.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.admin;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.Response;
+import java.net.URI;
+import java.util.Map;
+import java.util.Objects;
+import java.util.UUID;
+import org.apache.polaris.service.it.env.PolarisApiEndpoints;
+import org.apache.polaris.service.it.env.PolarisClient;
+import org.apache.polaris.service.tracing.RequestIdGenerator;
+import org.junit.jupiter.api.Test;
+
+@QuarkusTest
+@TestProfile(RequestIdHeaderTest.Profile.class)
+public class RequestIdHeaderTest {
+  public static class Profile implements QuarkusTestProfile {
+    @Override
+    public Map<String, String> getConfigOverrides() {
+      return Map.of(
+          "polaris.log.request-id-header-name",
+          REQUEST_ID_HEADER,
+          "polaris.realm-context.header-name",
+          REALM_HEADER,
+          "polaris.realm-context.realms",
+          REALM);
+    }
+  }
+
+  @Inject RequestIdGenerator requestIdGenerator;
+
+  private static final String REQUEST_ID_HEADER = "x-test-request-id-random";
+  private static final String REALM_HEADER = "realm";
+  private static final String REALM = "realm1";
+  private static final String CLIENT_ID = "client1";
+  private static final String CLIENT_SECRET = "secret1";
+
+  private static final URI baseUri =
+      URI.create(
+          "http://localhost:";
+              + Objects.requireNonNull(
+                  Integer.getInteger("quarkus.http.test-port"),
+                  "System property not set correctly: 
quarkus.http.test-port"));
+
+  private Response request(Map<String, String> headers) {
+    try (PolarisClient client =
+        PolarisClient.polarisClient(new PolarisApiEndpoints(baseUri, REALM, 
headers))) {
+      return client
+          .catalogApiPlain()
+          .request("v1/oauth/tokens")
+          .post(
+              Entity.form(
+                  new MultivaluedHashMap<>(
+                      Map.of(
+                          "grant_type",
+                          "client_credentials",
+                          "scope",
+                          "PRINCIPAL_ROLE:ALL",
+                          "client_id",
+                          CLIENT_ID,
+                          "client_secret",
+                          CLIENT_SECRET))));
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Test
+  public void testRequestIdHeaderSpecified() {
+    String requestId = "pre-requested-request-id";
+    Map<String, String> headers = Map.of(REALM_HEADER, REALM, 
REQUEST_ID_HEADER, requestId);
+    assertThat(sendRequest(headers)).matches(s -> s.equals(requestId));
+  }
+
+  @Test
+  public void testRequestIdHeaderNotSpecified() {
+    Map<String, String> headers = Map.of(REALM_HEADER, REALM);
+    assertThat(sendRequest(headers)).matches(this::isValidDefaultUUID);
+  }
+
+  @Test
+  public void testRequestIdHeaderNotSpecifiedAndCounterExhausted() {
+    requestIdGenerator.setCounter(Long.MAX_VALUE / 2 + 1);

Review Comment:
   This test is actually testing the internals of `RequestIdGenerator. I'd 
prefer to move this to a new `RequestIdGeneratorTest` class.



##########
runtime/service/src/main/java/org/apache/polaris/service/tracing/RequestIdGenerator.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.tracing;
+
+import com.google.common.annotations.VisibleForTesting;
+import jakarta.enterprise.context.ApplicationScoped;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLong;
+
+@ApplicationScoped
+public class RequestIdGenerator {
+  private static String BASE_DEFAULT_UUID = UUID.randomUUID().toString();
+  static final AtomicLong COUNTER = new AtomicLong();
+  static final Long COUNTER_SOFT_MAX = Long.MAX_VALUE / 2;
+
+  public String generateRequestId() {
+    String requestId = BASE_DEFAULT_UUID + "_" + COUNTER.incrementAndGet();
+    if (COUNTER.get() >= COUNTER_SOFT_MAX) {

Review Comment:
   There are many issues here: 
   
   1. `COUNTER.get()` will not necessarily return what 
`COUNTER.incrementAndGet()` returned in the previous line. 
   2. Many threads may enter this if block in parallel, and will all try to 
assign the `BASE_DEFAULT_UUID` field.
   3. `BASE_DEFAULT_UUID` is not volatile nor atomic, so the value write on 
line 38 may not be immediately visible to other threads.



##########
runtime/service/src/main/java/org/apache/polaris/service/tracing/RequestIdGenerator.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.tracing;
+
+import com.google.common.annotations.VisibleForTesting;
+import jakarta.enterprise.context.ApplicationScoped;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLong;
+
+@ApplicationScoped
+public class RequestIdGenerator {
+  private static String BASE_DEFAULT_UUID = UUID.randomUUID().toString();

Review Comment:
   ```suggestion
     private static String baseDefaultUuid = UUID.randomUUID().toString();
   ```



##########
runtime/service/src/test/java/org/apache/polaris/service/admin/RequestIdHeaderTest.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.admin;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.Response;
+import java.net.URI;
+import java.util.Map;
+import java.util.Objects;
+import java.util.UUID;
+import org.apache.polaris.service.it.env.PolarisApiEndpoints;
+import org.apache.polaris.service.it.env.PolarisClient;
+import org.apache.polaris.service.tracing.RequestIdGenerator;
+import org.junit.jupiter.api.Test;
+
+@QuarkusTest
+@TestProfile(RequestIdHeaderTest.Profile.class)
+public class RequestIdHeaderTest {
+  public static class Profile implements QuarkusTestProfile {
+    @Override
+    public Map<String, String> getConfigOverrides() {
+      return Map.of(
+          "polaris.log.request-id-header-name",
+          REQUEST_ID_HEADER,
+          "polaris.realm-context.header-name",
+          REALM_HEADER,
+          "polaris.realm-context.realms",
+          REALM);
+    }
+  }
+
+  @Inject RequestIdGenerator requestIdGenerator;
+
+  private static final String REQUEST_ID_HEADER = "x-test-request-id-random";
+  private static final String REALM_HEADER = "realm";
+  private static final String REALM = "realm1";
+  private static final String CLIENT_ID = "client1";
+  private static final String CLIENT_SECRET = "secret1";
+
+  private static final URI baseUri =
+      URI.create(
+          "http://localhost:";
+              + Objects.requireNonNull(
+                  Integer.getInteger("quarkus.http.test-port"),
+                  "System property not set correctly: 
quarkus.http.test-port"));
+
+  private Response request(Map<String, String> headers) {
+    try (PolarisClient client =
+        PolarisClient.polarisClient(new PolarisApiEndpoints(baseUri, REALM, 
headers))) {
+      return client
+          .catalogApiPlain()
+          .request("v1/oauth/tokens")
+          .post(
+              Entity.form(
+                  new MultivaluedHashMap<>(
+                      Map.of(
+                          "grant_type",
+                          "client_credentials",
+                          "scope",
+                          "PRINCIPAL_ROLE:ALL",
+                          "client_id",
+                          CLIENT_ID,
+                          "client_secret",
+                          CLIENT_SECRET))));
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Test
+  public void testRequestIdHeaderSpecified() {
+    String requestId = "pre-requested-request-id";
+    Map<String, String> headers = Map.of(REALM_HEADER, REALM, 
REQUEST_ID_HEADER, requestId);
+    assertThat(sendRequest(headers)).matches(s -> s.equals(requestId));
+  }
+
+  @Test
+  public void testRequestIdHeaderNotSpecified() {
+    Map<String, String> headers = Map.of(REALM_HEADER, REALM);
+    assertThat(sendRequest(headers)).matches(this::isValidDefaultUUID);
+  }
+
+  @Test
+  public void testRequestIdHeaderNotSpecifiedAndCounterExhausted() {
+    requestIdGenerator.setCounter(Long.MAX_VALUE / 2 + 1);
+    Map<String, String> headers = Map.of(REALM_HEADER, REALM);
+    String requestId = sendRequest(headers);
+    assertThat(requestId).matches(this::isValidDefaultUUID);
+    String currentRequestIdBase = requestId.split("_")[0];
+    String uuidBase = currentRequestIdBase;
+
+    requestId = sendRequest(headers);
+    assertThat(requestId).matches(this::isValidDefaultUUID);
+    currentRequestIdBase = requestId.split("_")[0];
+    assertThat(currentRequestIdBase).isNotEqualTo(uuidBase);
+  }
+
+  private boolean isValidDefaultUUID(String str) {

Review Comment:
   Same here, I'm not sure we need to verify that request IDs have any 
particular structure. For all intents and purposes, the ID is an opaque string 
and it doesn't matter how it's generated, as long as it is unique.



-- 
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]

Reply via email to