Skip to content

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 4 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
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) {
// 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);
}
}
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+)$");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The process ID should be a 32-bit unsigned number, so length 8 in hex format:

Suggested change
Pattern.compile("1.([0-9a-z]{16})(\\.\\d+){3}\\.(\\d+)$");
Pattern.compile("1.([0-9a-z]{8})(\\.\\d+){3}\\.(\\d+)$");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olavloite we had a long discussion with Simil and team in the same group that you are, about why it should be 64-bits due to higher collisions if it were 32-bits per https://orijtech.notion.site/x-goog-spanner-request-id-always-on-gRPC-header-to-aid-in-quick-debugging-of-errors-14aba6bc91348091a58fca7a505c9827#14aba6bc9134807f9f17d3ef58e26128


@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());
}
}
Loading