adutra commented on code in PR #2602: URL: https://github.com/apache/polaris/pull/2602#discussion_r2390618074
########## runtime/service/src/main/java/org/apache/polaris/service/tracing/RequestIdGenerator.java: ########## @@ -0,0 +1,56 @@ +/* + * 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.AtomicReference; + +@ApplicationScoped +public class RequestIdGenerator { + static final Long COUNTER_SOFT_MAX = Long.MAX_VALUE / 2; + + record State(String uuid, long counter) { + + State() { + this(UUID.randomUUID().toString(), 1); + } + + String requestId() { + return String.format("%s_%019d", uuid, counter); + } + + State increment() { + return counter >= COUNTER_SOFT_MAX ? new State() : new State(uuid, counter + 1); Review Comment: Why `COUNTER_SOFT_MAX` ? There is no risk of overflowing here. ########## runtime/service/src/test/java/org/apache/polaris/service/tracing/RequestIdGeneratorTest.java: ########## @@ -0,0 +1,122 @@ +/* + * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class RequestIdGeneratorTest { + + private RequestIdGenerator requestIdGenerator; + + @BeforeEach + void setUp() { + requestIdGenerator = new RequestIdGenerator(); + } + + @Test + void testGenerateRequestId_ReturnsValidFormat() { + String requestId = requestIdGenerator.generateRequestId(); + + assertThat(requestId).isNotNull(); + assertThat(requestId).matches(this::isValidRequestIdFormat); + // First call should increment counter to 1 + assertThat(extractCounterFromRequestId(requestId)).isEqualTo(1); + } + + @Test + void testGenerateRequestId_ReturnsUniqueIds() { Review Comment: This test could be made multi-threaded. ########## runtime/service/src/test/java/org/apache/polaris/service/tracing/RequestIdGeneratorTest.java: ########## @@ -0,0 +1,122 @@ +/* + * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class RequestIdGeneratorTest { + + private RequestIdGenerator requestIdGenerator; + + @BeforeEach + void setUp() { + requestIdGenerator = new RequestIdGenerator(); + } + + @Test + void testGenerateRequestId_ReturnsValidFormat() { + String requestId = requestIdGenerator.generateRequestId(); + + assertThat(requestId).isNotNull(); + assertThat(requestId).matches(this::isValidRequestIdFormat); + // First call should increment counter to 1 + assertThat(extractCounterFromRequestId(requestId)).isEqualTo(1); + } + + @Test + void testGenerateRequestId_ReturnsUniqueIds() { + Set<String> generatedIds = new HashSet<>(); + + // Generate multiple request IDs and verify they're all unique + for (int i = 0; i < 1000; i++) { + String requestId = requestIdGenerator.generateRequestId(); + assertThat(generatedIds).doesNotContain(requestId); + generatedIds.add(requestId); + } + + assertThat(generatedIds).hasSize(1000); + } + + @Test + void testCounterIncrementsSequentially() { + // requestIdGenerator.setCounter(0); + + String firstId = requestIdGenerator.generateRequestId(); + String secondId = requestIdGenerator.generateRequestId(); + String thirdId = requestIdGenerator.generateRequestId(); + + assertThat(extractCounterFromRequestId(firstId)).isEqualTo(1); + assertThat(extractCounterFromRequestId(secondId)).isEqualTo(2); + assertThat(extractCounterFromRequestId(thirdId)).isEqualTo(3); + } + + @Test + void testCounterRotationAtSoftMax() { + // Set counter close to soft max + long softMax = RequestIdGenerator.COUNTER_SOFT_MAX; + requestIdGenerator.setCounter(softMax); + + String beforeRotation = requestIdGenerator.generateRequestId(); + String afterRotation = requestIdGenerator.generateRequestId(); + + // The UUID part should be different after rotation + String beforeUuidPart = beforeRotation.substring(0, beforeRotation.lastIndexOf('_')); + String afterUuidPart = afterRotation.substring(0, afterRotation.lastIndexOf('_')); + assertNotEquals(beforeUuidPart, afterUuidPart); + + assertThat(extractCounterFromRequestId(beforeRotation)).isEqualTo(softMax); + // Counter reset to 1 (after increment from 0) + assertThat(extractCounterFromRequestId(afterRotation)).isEqualTo(1); + } + + @Test + void testSetCounterChangesNextGeneratedId() { Review Comment: Not sure this test has any value, `setCounter` is not meant to be called outside of tests, and besides, I think we can remove that method. ########## runtime/service/src/main/java/org/apache/polaris/service/tracing/RequestIdGenerator.java: ########## @@ -0,0 +1,56 @@ +/* + * 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.AtomicReference; + +@ApplicationScoped +public class RequestIdGenerator { + static final Long COUNTER_SOFT_MAX = Long.MAX_VALUE / 2; + + record State(String uuid, long counter) { + + State() { + this(UUID.randomUUID().toString(), 1); + } + + String requestId() { + return String.format("%s_%019d", uuid, counter); + } + + State increment() { + return counter >= COUNTER_SOFT_MAX ? new State() : new State(uuid, counter + 1); + } + } + + final AtomicReference<State> state = new AtomicReference<>(new State()); + + public String generateRequestId() { + return state.getAndUpdate(State::increment).requestId(); + } + + @VisibleForTesting + public void setCounter(long counter) { Review Comment: You don't need this method, `RequestIdGenerator.state` is already package-private. ########## runtime/service/src/test/java/org/apache/polaris/service/tracing/RequestIdGeneratorTest.java: ########## @@ -0,0 +1,122 @@ +/* + * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class RequestIdGeneratorTest { + + private RequestIdGenerator requestIdGenerator; + + @BeforeEach + void setUp() { + requestIdGenerator = new RequestIdGenerator(); + } + + @Test + void testGenerateRequestId_ReturnsValidFormat() { + String requestId = requestIdGenerator.generateRequestId(); + + assertThat(requestId).isNotNull(); + assertThat(requestId).matches(this::isValidRequestIdFormat); + // First call should increment counter to 1 + assertThat(extractCounterFromRequestId(requestId)).isEqualTo(1); + } + + @Test + void testGenerateRequestId_ReturnsUniqueIds() { + Set<String> generatedIds = new HashSet<>(); + + // Generate multiple request IDs and verify they're all unique + for (int i = 0; i < 1000; i++) { + String requestId = requestIdGenerator.generateRequestId(); + assertThat(generatedIds).doesNotContain(requestId); + generatedIds.add(requestId); + } + + assertThat(generatedIds).hasSize(1000); + } + + @Test + void testCounterIncrementsSequentially() { + // requestIdGenerator.setCounter(0); Review Comment: nit: dangling 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]
