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


##########
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:
   Done in the next revision!



##########
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:
   Got it - I'm still testing the format in `RequestIdGenerator` (just to make 
sure that what we expect to happen is still happening), but removed it from 
here.



##########
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:
   TIL, thanks!



##########
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:
   Sorry, you are right and I'm embarrassed that I didn't catch these. I've 
fixed this in the next revision using an AtomicBoolean as a way to track 
whether a reset is in progress. Please let me know if I've missed anything else.



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