-
Notifications
You must be signed in to change notification settings - Fork 20
feat: Add support for the header id feature #215
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # Copyright 2024 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. | ||
|
|
||
|
|
||
| require "grpc" | ||
| require "securerandom" | ||
| require "mutex_m" | ||
| require "google/cloud/spanner/errors" | ||
|
|
||
| module Google | ||
| module Cloud | ||
| module Spanner | ||
| class RequestIdInterceptor < GRPC::ClientInterceptor | ||
| @client_id_counter = 0 | ||
| @client_mutex = Mutex.new | ||
| @channel_id_counter = 0 | ||
| @channel_mutex = Mutex.new | ||
| @request_id_counter = 0 | ||
| @request_id_mutex = Mutex.new | ||
| @process_id = nil; | ||
| @process_id_mutex = Mutex.new | ||
|
|
||
| def self.next_client_id | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add some lightweight docstrings here to these methods? Also we need to make sure appropriate methods are private where we dont expect to expose the API. I assume most of these. |
||
| @client_mutex.synchronize do | ||
| @client_id_counter += 1 | ||
| end | ||
| end | ||
|
|
||
| def self.next_channel_id | ||
| @channel_mutex.synchronize do | ||
| @channel_id_counter += 1 | ||
| end | ||
| end | ||
|
|
||
| def self.get_process_id process_id = nil | ||
| @process_id_mutex.synchronize do | ||
| if process_id.nil? || !@process_id.nil? | ||
| return @process_id ||= SecureRandom.hex(8) | ||
| end | ||
|
|
||
| case process_id | ||
| when Integer | ||
| if process_id >= 0 && process_id.bit_length <= 64 | ||
| return process_id.to_s(16).rjust(16,'0') | ||
| end | ||
| when String | ||
| if (process_id =~ /\A[0-9a-fA-F]{16}\z/) | ||
| return process_id | ||
| end | ||
| end | ||
|
|
||
| raise ArgumentError,'process_id must be a 64-bit integer or 16-character hex string' | ||
| end | ||
| end | ||
|
|
||
| def initialize process_id: nil | ||
| @version = 1 | ||
| @process_id = self.class.get_process_id process_id | ||
| @client_id = self.class.next_client_id | ||
| @channel_id = self.class.next_channel_id | ||
| @request_id_counter = 0 | ||
| @request_mutex = Mutex.new | ||
| end | ||
|
|
||
| def request_response method:, request:, call:, metadata: | ||
| update_metadata_for_call call, metadata do | ||
| yield | ||
| end | ||
| end | ||
|
|
||
| def client_streamer method:, request:, call:, metadata: | ||
| update_metadata_for_call call, metadata do | ||
| yield | ||
| end | ||
| end | ||
|
|
||
| def server_streamer method:, request:, call:, metadata: | ||
| update_metadata_for_call call, metadata do | ||
| yield | ||
| end | ||
| end | ||
|
|
||
| def bidi_streamer method:, request:, call:, metadata: | ||
| update_metadata_for_call call, metadata do | ||
| yield | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def validate_process_id process_id | ||
| value.is_a?(Integer) && value >=0 && value.bit_length <= 64 | ||
| end | ||
|
|
||
| def update_metadata_for_call call, metadata | ||
| request_id = nil | ||
| attempt = 1; | ||
|
|
||
| if metadata.include? :"x-goog-spanner-request-id" | ||
| request_id, attempt = get_header_info metadata[:"x-goog-spanner-request-id"] | ||
| else | ||
| request_id = @request_mutex.synchronize { @request_id_counter += 1 } | ||
| end | ||
|
|
||
| formatted_request_id = format_request_id request_id, attempt | ||
| metadata[:"x-goog-spanner-request-id"] = formatted_request_id | ||
|
|
||
| response = yield | ||
| response | ||
| rescue => ex | ||
| ex.instance_variable_set :@spanner_header_id, formatted_request_id | ||
| raise ex | ||
| end | ||
|
|
||
| def format_request_id request_id, attempt | ||
| "#{@version}.#{@process_id}.#{@client_id}.#{@channel_id}.#{request_id}.#{attempt}" | ||
| end | ||
|
|
||
| def get_header_info header | ||
| version, process_id, client_id, channel_id, request_id, attempt = header.split('.') | ||
| [request_id, attempt.to_i + 1]; | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Copyright 2025 Google LLC | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
| # | ||
| # 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. | ||
|
|
||
|
|
||
| require "google/cloud/errors" | ||
|
|
||
| # This is a monkey patch for Google::Cloud::Error to add support for the request_id method | ||
| # to keep this spanner exclusive method inside the spanner code. | ||
| module Google | ||
| module Cloud | ||
| class Error | ||
| ## | ||
| # The Spanner header ID if there was an error on the request. | ||
| # | ||
| # @return [String, nil] | ||
| # | ||
| def request_id | ||
| return nil unless cause.instance_variable_defined? :@spanner_header_id | ||
| cause.instance_variable_get :@spanner_header_id | ||
| end | ||
| end | ||
| end | ||
| end | ||
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.
2026