-
Notifications
You must be signed in to change notification settings - Fork 136
chore(x-goog-spanner-request-id): commit foundation #3643
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
4 commits
Select commit
Hold shift + click to select a range
31d9a8a
chore(x-goog-spanner-request-id): commit foundation
odeke-em 4fea875
RandProcessId MUST be 64-bits and not 32-bits
odeke-em 4c260a8
Use BigInteger to generate the 64-bit integer
odeke-em 331e06e
Make RAND_PROCESS_ID static final too
odeke-em 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
88 changes: 88 additions & 0 deletions
88
google-cloud-spanner/src/main/java/com/google/cloud/spanner/XGoogSpannerRequestId.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,88 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed 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 com.google.cloud.spanner; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import java.math.BigInteger; | ||
| import java.security.SecureRandom; | ||
| import java.util.Objects; | ||
|
|
||
| @InternalApi | ||
| public class XGoogSpannerRequestId { | ||
| // 1. Generate the random process Id singleton. | ||
| @VisibleForTesting | ||
| static final String RAND_PROCESS_ID = XGoogSpannerRequestId.generateRandProcessId(); | ||
|
|
||
| @VisibleForTesting | ||
| static final long VERSION = 1; // The version of the specification being implemented. | ||
|
|
||
| private final long nthClientId; | ||
| private final long nthChannelId; | ||
| private final long nthRequest; | ||
| private long attempt; | ||
|
|
||
| XGoogSpannerRequestId(long nthClientId, long nthChannelId, long nthRequest, long attempt) { | ||
| this.nthClientId = nthClientId; | ||
| this.nthChannelId = nthChannelId; | ||
| this.nthRequest = nthRequest; | ||
| this.attempt = attempt; | ||
| } | ||
|
|
||
| public static XGoogSpannerRequestId of( | ||
| long nthClientId, long nthChannelId, long nthRequest, long attempt) { | ||
| return new XGoogSpannerRequestId(nthClientId, nthChannelId, nthRequest, attempt); | ||
| } | ||
|
|
||
| private static String generateRandProcessId() { | ||
| // Expecting to use 64-bits of randomness to avoid clashes. | ||
| BigInteger bigInt = new BigInteger(64, new SecureRandom()); | ||
| return String.format("%016x", bigInt); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
odeke-em marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return String.format( | ||
| "%d.%s.%d.%d.%d.%d", | ||
| XGoogSpannerRequestId.VERSION, | ||
| XGoogSpannerRequestId.RAND_PROCESS_ID, | ||
| this.nthClientId, | ||
| this.nthChannelId, | ||
| this.nthRequest, | ||
| this.attempt); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object other) { | ||
odeke-em marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // instanceof for a null object returns false. | ||
| if (!(other instanceof XGoogSpannerRequestId)) { | ||
| return false; | ||
| } | ||
|
|
||
| XGoogSpannerRequestId otherReqId = (XGoogSpannerRequestId) (other); | ||
|
|
||
| return Objects.equals(this.nthClientId, otherReqId.nthClientId) | ||
| && Objects.equals(this.nthChannelId, otherReqId.nthChannelId) | ||
| && Objects.equals(this.nthRequest, otherReqId.nthRequest) | ||
| && Objects.equals(this.attempt, otherReqId.attempt); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(this.nthClientId, this.nthChannelId, this.nthRequest, this.attempt); | ||
| } | ||
| } | ||
54 changes: 54 additions & 0 deletions
54
google-cloud-spanner/src/test/java/com/google/cloud/spanner/XGoogSpannerRequestIdTest.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,54 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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 com.google.cloud.spanner; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class XGoogSpannerRequestIdTest { | ||
| private static final Pattern REGEX_RAND_PROCESS_ID = | ||
| Pattern.compile("1.([0-9a-z]{16})(\\.\\d+){3}\\.(\\d+)$"); | ||
odeke-em marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| public void testEquals() { | ||
| XGoogSpannerRequestId reqID1 = XGoogSpannerRequestId.of(1, 1, 1, 1); | ||
| XGoogSpannerRequestId reqID2 = XGoogSpannerRequestId.of(1, 1, 1, 1); | ||
| assertEquals(reqID1, reqID2); | ||
| assertEquals(reqID1, reqID1); | ||
| assertEquals(reqID2, reqID2); | ||
|
|
||
| XGoogSpannerRequestId reqID3 = XGoogSpannerRequestId.of(1, 1, 1, 2); | ||
| assertNotEquals(reqID1, reqID3); | ||
| assertNotEquals(reqID3, reqID1); | ||
| assertEquals(reqID3, reqID3); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEnsureHexadecimalFormatForRandProcessID() { | ||
| String str = XGoogSpannerRequestId.of(1, 2, 3, 4).toString(); | ||
| Matcher m = XGoogSpannerRequestIdTest.REGEX_RAND_PROCESS_ID.matcher(str); | ||
| assertTrue(m.matches()); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.