-
Notifications
You must be signed in to change notification settings - Fork 463
impl(spanner): add request ID generation functions #16465
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
Open
scotthart
wants to merge
2
commits into
googleapis:main
Choose a base branch
from
scotthart:spanner_request_id_header_1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
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
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,84 @@ | ||
| // Copyright 2026 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. | ||
|
|
||
| #include "google/cloud/spanner/internal/spanner_request_id.h" | ||
| #include "google/cloud/internal/random.h" | ||
| #include "absl/strings/str_cat.h" | ||
| #include "absl/strings/str_format.h" | ||
| #include <atomic> | ||
| #include <random> | ||
| #ifndef _WIN32 | ||
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| namespace google { | ||
| namespace cloud { | ||
| namespace spanner_internal { | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
|
||
| std::string ProcessRandomId() { | ||
| #ifndef _WIN32 | ||
| static std::atomic<pid_t> pid{0}; | ||
| static std::atomic<std::uint64_t> random_id{0}; | ||
| pid_t const current_pid = getpid(); | ||
| if (pid.load(std::memory_order_acquire) != current_pid) { | ||
| auto generator = google::cloud::internal::MakeDefaultPRNG(); | ||
| std::uniform_int_distribution<std::uint64_t> dist; | ||
| random_id.store(dist(generator), std::memory_order_release); | ||
| pid.store(current_pid, std::memory_order_release); | ||
| } | ||
| return absl::StrFormat("%016x", random_id.load(std::memory_order_acquire)); | ||
| #else | ||
| static std::string const random_id = [] { | ||
| auto generator = google::cloud::internal::MakeDefaultPRNG(); | ||
| std::uniform_int_distribution<std::uint64_t> dist; | ||
| return absl::StrFormat("%016x", dist(generator)); | ||
| }(); | ||
| return random_id; | ||
| #endif | ||
| } | ||
|
|
||
| std::uint64_t NextClientId() { | ||
| static std::atomic<std::uint64_t> counter{0}; | ||
| return ++counter; | ||
| } | ||
|
|
||
| std::string FormatSpannerRequestStaticPrefix(std::uint32_t version, | ||
| std::string_view process_random_id, | ||
| std::uint64_t client_id) { | ||
| return absl::StrCat(version, ".", process_random_id, ".", client_id, "."); | ||
| } | ||
|
|
||
| std::string FormatSpannerRequestId(std::string_view static_prefix, | ||
| std::uint32_t channel_id, | ||
| std::uint64_t request_index, | ||
| std::uint32_t attempt_index) { | ||
| return absl::StrCat(static_prefix, channel_id, ".", request_index, ".", | ||
| attempt_index); | ||
| } | ||
|
|
||
| std::string FormatSpannerRequestId(std::uint32_t version, | ||
| std::string_view process_random_id, | ||
| std::uint64_t client_id, | ||
| std::uint32_t channel_id, | ||
| std::uint64_t request_index, | ||
| std::uint32_t attempt_index) { | ||
| return absl::StrCat(version, ".", process_random_id, ".", client_id, ".", | ||
| channel_id, ".", request_index, ".", attempt_index); | ||
| } | ||
|
|
||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
| } // namespace spanner_internal | ||
| } // namespace cloud | ||
| } // namespace google |
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,63 @@ | ||
| // Copyright 2026 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. | ||
|
|
||
| #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_INTERNAL_SPANNER_REQUEST_ID_H | ||
| #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_INTERNAL_SPANNER_REQUEST_ID_H | ||
|
|
||
| #include "google/cloud/spanner/version.h" | ||
| #include <cstdint> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| namespace google { | ||
| namespace cloud { | ||
| namespace spanner_internal { | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
|
||
| /// Generates a 16-character zero-padded lowercase hex random ID from a 64-bit | ||
| /// random integer. On POSIX systems, re-generates the ID if a process fork is | ||
| /// detected. On non-POSIX systems (e.g. Windows), generates once per process | ||
| /// lifecycle. | ||
| std::string ProcessRandomId(); | ||
|
|
||
| /// Returns the next sequential process-wide client ID (thread-safe). | ||
| std::uint64_t NextClientId(); | ||
|
|
||
| /// Formats the static 3-field prefix: "<version>.<process_id>.<client_id>." | ||
| std::string FormatSpannerRequestStaticPrefix(std::uint32_t version, | ||
| std::string_view process_random_id, | ||
| std::uint64_t client_id); | ||
|
|
||
| /// Formats full request ID using the cached static prefix: | ||
| /// "<static_prefix><channel_id>.<request_index>.<attempt_index>" | ||
| std::string FormatSpannerRequestId(std::string_view static_prefix, | ||
| std::uint32_t channel_id, | ||
| std::uint64_t request_index, | ||
| std::uint32_t attempt_index); | ||
|
|
||
| /// Convenience overload formatting all 6 fields: | ||
| /// "<version>.<process_id>.<client_id>.<channel_id>.<request_index>.<attempt_index>" | ||
| std::string FormatSpannerRequestId(std::uint32_t version, | ||
| std::string_view process_random_id, | ||
| std::uint64_t client_id, | ||
| std::uint32_t channel_id, | ||
| std::uint64_t request_index, | ||
| std::uint32_t attempt_index); | ||
|
|
||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
| } // namespace spanner_internal | ||
| } // namespace cloud | ||
| } // namespace google | ||
|
|
||
| #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_INTERNAL_SPANNER_REQUEST_ID_H |
124 changes: 124 additions & 0 deletions
124
google/cloud/spanner/internal/spanner_request_id_test.cc
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,124 @@ | ||
| // Copyright 2026 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. | ||
|
|
||
| #include "google/cloud/spanner/internal/spanner_request_id.h" | ||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
| #include <regex> | ||
| #ifndef _WIN32 | ||
| #include <sys/wait.h> | ||
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| namespace google { | ||
| namespace cloud { | ||
| namespace spanner_internal { | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
| namespace { | ||
|
|
||
| using ::testing::Eq; | ||
| using ::testing::Ge; | ||
| using ::testing::Gt; | ||
| using ::testing::Ne; | ||
|
|
||
| MATCHER_P(MatchesStdRegex, pattern, "") { | ||
| if (std::regex_match(arg, std::regex(pattern))) { | ||
| return true; | ||
| } | ||
| *result_listener << "which does not match regex \"" << pattern << "\""; | ||
| return false; | ||
| } | ||
|
|
||
| TEST(SpannerRequestIdTest, ProcessRandomIdFormat) { | ||
| std::string const id1 = ProcessRandomId(); | ||
| EXPECT_THAT(id1, MatchesStdRegex("^[0-9a-f]{16}$")); | ||
| std::string const id2 = ProcessRandomId(); | ||
| EXPECT_THAT(id2, Eq(id1)); | ||
| } | ||
|
|
||
| TEST(SpannerRequestIdTest, NextClientIdMonotonic) { | ||
| std::uint64_t const c1 = NextClientId(); | ||
| std::uint64_t const c2 = NextClientId(); | ||
| std::uint64_t const c3 = NextClientId(); | ||
| EXPECT_THAT(c1, Gt(0ULL)); | ||
| EXPECT_THAT(c2, Eq(c1 + 1)); | ||
| EXPECT_THAT(c3, Eq(c2 + 1)); | ||
| } | ||
|
|
||
| TEST(SpannerRequestIdTest, FormatSpannerRequestStaticPrefix) { | ||
| std::string const prefix = | ||
| FormatSpannerRequestStaticPrefix(1, "0123456789abcdef", 42); | ||
| EXPECT_THAT(prefix, Eq("1.0123456789abcdef.42.")); | ||
| } | ||
|
|
||
| TEST(SpannerRequestIdTest, FormatSpannerRequestIdWithPrefix) { | ||
| std::string const prefix = "1.0123456789abcdef.42."; | ||
| std::string const request_id = FormatSpannerRequestId(prefix, 1, 100, 2); | ||
| EXPECT_THAT(request_id, Eq("1.0123456789abcdef.42.1.100.2")); | ||
| } | ||
|
|
||
| TEST(SpannerRequestIdTest, FormatSpannerRequestIdDirect) { | ||
| std::string const request_id = | ||
| FormatSpannerRequestId(1, "0123456789abcdef", 42, 3, 200, 1); | ||
| EXPECT_THAT(request_id, Eq("1.0123456789abcdef.42.3.200.1")); | ||
| } | ||
|
|
||
| #ifndef _WIN32 | ||
| TEST(SpannerRequestIdTest, ProcessRandomIdForkRegeneration) { | ||
| // Ensure the parent has already initialized ProcessRandomId | ||
| std::string const parent_id = ProcessRandomId(); | ||
| ASSERT_THAT(parent_id, MatchesStdRegex("^[0-9a-f]{16}$")); | ||
|
|
||
| int pipe_fds[2]; | ||
| ASSERT_THAT(pipe(pipe_fds), Eq(0)); | ||
|
|
||
| pid_t const pid = fork(); | ||
| ASSERT_THAT(pid, Ge(0)); | ||
|
|
||
| if (pid == 0) { | ||
| // Child process: read ProcessRandomId and write to pipe | ||
| close(pipe_fds[0]); | ||
| std::string const child_id = ProcessRandomId(); | ||
| ssize_t const bytes_written = | ||
| write(pipe_fds[1], child_id.data(), child_id.size()); | ||
| close(pipe_fds[1]); | ||
| if (bytes_written != static_cast<ssize_t>(child_id.size())) { | ||
| _exit(1); | ||
| } | ||
| _exit(0); | ||
| } | ||
|
|
||
| // Parent process: read child's ID from pipe | ||
| close(pipe_fds[1]); | ||
| char buffer[32] = {0}; | ||
| ssize_t const bytes_read = read(pipe_fds[0], buffer, sizeof(buffer) - 1); | ||
| close(pipe_fds[0]); | ||
|
|
||
| int status = 0; | ||
| waitpid(pid, &status, 0); | ||
| ASSERT_TRUE(WIFEXITED(status)); | ||
| ASSERT_THAT(WEXITSTATUS(status), Eq(0)); | ||
|
|
||
| ASSERT_THAT(bytes_read, Eq(16)); | ||
| std::string const child_id(buffer, static_cast<std::size_t>(bytes_read)); | ||
| EXPECT_THAT(child_id, MatchesStdRegex("^[0-9a-f]{16}$")); | ||
| EXPECT_THAT(child_id, Ne(parent_id)); | ||
| } | ||
| #endif | ||
|
|
||
| } // namespace | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
| } // namespace spanner_internal | ||
| } // namespace cloud | ||
| } // namespace google | ||
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
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: why not check for
Eq(1),Eq(2),Eq(3)?and monotonic would be more relaxed like: