-
Notifications
You must be signed in to change notification settings - Fork 314
Extract interface for RequestIdGenerator #2720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...e/service/src/main/java/org/apache/polaris/service/tracing/DefaultRequestIdGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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 io.smallrye.mutiny.Uni; | ||
import jakarta.annotation.Nonnull; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import java.util.UUID; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* Default implementation of {@link RequestIdGenerator}, striking a balance between randomness and | ||
* performance. | ||
* | ||
* <p>The IDs generated by this generator are of the form: {@code UUID_COUNTER}. The UUID part is | ||
* randomly generated at startup, and the counter is incremented for each request. | ||
* | ||
* <p>In the unlikely event that the counter overflows, a new UUID is generated and the counter is | ||
* reset to 1. | ||
*/ | ||
@ApplicationScoped | ||
public class DefaultRequestIdGenerator implements RequestIdGenerator { | ||
|
||
record RequestId(UUID uuid, long counter) { | ||
|
||
RequestId() { | ||
this(UUID.randomUUID(), 1); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public String toString() { | ||
return String.format("%s_%019d", uuid(), counter()); | ||
} | ||
|
||
RequestId increment() { | ||
return counter == Long.MAX_VALUE ? new RequestId() : new RequestId(uuid, counter + 1); | ||
} | ||
} | ||
|
||
final AtomicReference<RequestId> state = new AtomicReference<>(new RequestId()); | ||
|
||
@Override | ||
public Uni<String> generateRequestId(ContainerRequestContext requestContext) { | ||
return Uni.createFrom().item(nextRequestId().toString()); | ||
} | ||
|
||
RequestId nextRequestId() { | ||
return state.getAndUpdate(RequestId::increment); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
...ime/service/src/main/java/org/apache/polaris/service/tracing/RequestIdResponseFilter.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...rvice/src/test/java/org/apache/polaris/service/tracing/DefaultRequestIdGeneratorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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 java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentSkipListSet; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import org.apache.polaris.service.tracing.DefaultRequestIdGenerator.RequestId; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class DefaultRequestIdGeneratorTest { | ||
|
||
private DefaultRequestIdGenerator requestIdGenerator; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
requestIdGenerator = new DefaultRequestIdGenerator(); | ||
} | ||
|
||
@Test | ||
void testGeneratesUniqueIds() { | ||
Set<String> generatedIds = new ConcurrentSkipListSet<>(); | ||
try (ExecutorService executor = Executors.newFixedThreadPool(10)) { | ||
for (int i = 0; i < 1000; i++) { | ||
executor.execute(() -> generatedIds.add(requestIdGenerator.nextRequestId().toString())); | ||
} | ||
} | ||
assertThat(generatedIds).hasSize(1000); | ||
} | ||
|
||
@Test | ||
void testCounterIncrementsSequentially() { | ||
assertThat(requestIdGenerator.nextRequestId().counter()).isEqualTo(1L); | ||
assertThat(requestIdGenerator.nextRequestId().counter()).isEqualTo(2L); | ||
assertThat(requestIdGenerator.nextRequestId().counter()).isEqualTo(3L); | ||
} | ||
|
||
@Test | ||
void testCounterRotationAtMax() { | ||
requestIdGenerator.state.set(new RequestId(UUID.randomUUID(), Long.MAX_VALUE)); | ||
|
||
var beforeRotation = requestIdGenerator.nextRequestId(); | ||
var afterRotation = requestIdGenerator.nextRequestId(); | ||
|
||
// The UUID should be different after rotation | ||
assertThat(beforeRotation.uuid()).isNotEqualTo(afterRotation.uuid()); | ||
|
||
// The counter should be reset to 1 after rotation | ||
assertThat(beforeRotation.counter()).isEqualTo(Long.MAX_VALUE); | ||
assertThat(afterRotation.counter()).isEqualTo(1L); | ||
} | ||
|
||
@Test | ||
void testRequestIdToString() { | ||
var uuid = UUID.randomUUID(); | ||
assertThat(new RequestId(uuid, 1L).toString()).isEqualTo(uuid + "_0000000000000000001"); | ||
assertThat(new RequestId(uuid, 12345L).toString()).isEqualTo(uuid + "_0000000000000012345"); | ||
assertThat(new RequestId(uuid, Long.MAX_VALUE).toString()) | ||
.isEqualTo(uuid + "_9223372036854775807"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: if lexicographic ordering is desired (based on previous conversations), it might be worth using something like
<MILLIS_SINCE_EPOCH_PADDED>_<COUNTER_PADDED>_<UUID>
WDYT?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or
<MILLIS_PADDED>_<SMALL_COUNTER>_<UUID>
-SMALL_COUNTER
meaning something in the range of 10K to avoid calling the system clock too often.but this might be getting too much into the monotonic clock area 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure lexicographic sort order is desirable for anything else than making it easier for humans to visualize and compare request IDs – so any of your suggestions is fine 😄
How about we get this in without changing the generation logic, and then, we look into ways of making the logic better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I think it's fine if the requests are only lexicographically sorted across all requests made per node. IMO request ID sorting doesn't mean much for API clients as they also usually log timestamps.
I'm okay to change it, if you both feel there's something better - but just my two cents 😃