|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +require "grpc" |
| 17 | +require "securerandom" |
| 18 | +require "mutex_m" |
| 19 | +require "google/cloud/spanner/errors" |
| 20 | + |
| 21 | +module Google |
| 22 | + module Cloud |
| 23 | + module Spanner |
| 24 | + ## |
| 25 | + # RequestIdInterceptor is a GRPC interceptor class that captures all the rpc calls |
| 26 | + # made by the GRPC layer inserting a new Header with a specific ID for debugging purposes. |
| 27 | + # |
| 28 | + class RequestIdInterceptor < GRPC::ClientInterceptor |
| 29 | + @client_id_counter = 0 |
| 30 | + @client_mutex = Mutex.new |
| 31 | + @channel_id_counter = 0 |
| 32 | + @channel_mutex = Mutex.new |
| 33 | + @request_id_counter = 0 |
| 34 | + @request_id_mutex = Mutex.new |
| 35 | + @process_id = nil |
| 36 | + @process_id_mutex = Mutex.new |
| 37 | + |
| 38 | + # @private |
| 39 | + # Gets the next client ID and increments it. |
| 40 | + # |
| 41 | + # @return [Integer] |
| 42 | + private_class_method def self.next_client_id |
| 43 | + @client_mutex.synchronize do |
| 44 | + @client_id_counter += 1 |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + # @private |
| 49 | + # Gets the next channel ID and increments it. |
| 50 | + # |
| 51 | + # @return [Integer] |
| 52 | + private_class_method def self.next_channel_id |
| 53 | + @channel_mutex.synchronize do |
| 54 | + @channel_id_counter += 1 |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + # @private |
| 59 | + # Returns a process ID for the context of the request id header. |
| 60 | + # A process ID is a Hex encoded 64 bit value |
| 61 | + # |
| 62 | + # @param [String, int] process_id A 64 bit value in Hex or Integer format |
| 63 | + # @return [String] |
| 64 | + private_class_method def self.get_process_id process_id = nil |
| 65 | + @process_id_mutex.synchronize do |
| 66 | + if process_id.nil? || !@process_id.nil? |
| 67 | + return @process_id ||= (SecureRandom.hex 8) |
| 68 | + end |
| 69 | + |
| 70 | + case process_id |
| 71 | + when Integer |
| 72 | + if process_id >= 0 && process_id.bit_length <= 64 |
| 73 | + return process_id.to_s(16).rjust(16, "0") |
| 74 | + end |
| 75 | + when String |
| 76 | + if process_id =~ /\A[0-9a-fA-F]{16}\z/ |
| 77 | + return process_id |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + raise ArgumentError, "process_id must be a 64-bit integer or 16-character hex string" |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + # Initializes a request_id_interceptor instance. |
| 86 | + # |
| 87 | + # @param [String, int] process_id A 64 bit value in Hex or Integer format |
| 88 | + # @return [Google::Cloud::Spanner::RequestIdInterceptor] |
| 89 | + def initialize process_id: nil |
| 90 | + super |
| 91 | + @version = 1 |
| 92 | + @process_id = self.class.send :get_process_id, process_id |
| 93 | + @client_id = self.class.send :next_client_id |
| 94 | + @channel_id = self.class.send :next_channel_id |
| 95 | + @request_id_counter = 0 |
| 96 | + @request_mutex = Mutex.new |
| 97 | + end |
| 98 | + |
| 99 | + # Intercepts a request_response rpc call |
| 100 | + # |
| 101 | + # @param [String] method The RPC method name |
| 102 | + # @param [Google::Protobuf::MessageExts] request The request to be sent to the RPC call |
| 103 | + # @param [GRPC::ActiveCall::InterceptableView] call An interceptable view object for the call class |
| 104 | + # @param [Hash] metadata All the metadata to be sent to the RPC call |
| 105 | + # @return [void] |
| 106 | + def request_response method:, request:, call:, metadata:, &block |
| 107 | + # Unused. This is to avoid Rubocop's Lint/UnusedMethodArgument |
| 108 | + _method = method |
| 109 | + _request = request |
| 110 | + _call = call |
| 111 | + update_metadata_for_call metadata, &block |
| 112 | + end |
| 113 | + |
| 114 | + # Intercepts a client_streamer rpc call |
| 115 | + # |
| 116 | + # @param [String] method The RPC method name |
| 117 | + # @param [Google::Protobuf::MessageExts] request The request to be sent to the RPC call |
| 118 | + # @param [GRPC::ActiveCall::InterceptableView] call An interceptable view object for the call class |
| 119 | + # @param [Hash] metadata All the metadata to be sent to the RPC call |
| 120 | + # @return [void] |
| 121 | + def client_streamer method:, request:, call:, metadata:, &block |
| 122 | + # Unused. This is to avoid Rubocop's Lint/UnusedMethodArgument |
| 123 | + _method = method |
| 124 | + _request = request |
| 125 | + _call = call |
| 126 | + update_metadata_for_call metadata, &block |
| 127 | + end |
| 128 | + |
| 129 | + # Intercepts a server_streamer rpc call |
| 130 | + # |
| 131 | + # @param [String] method The RPC method name |
| 132 | + # @param [Google::Protobuf::MessageExts] request The request to be sent to the RPC call |
| 133 | + # @param [GRPC::ActiveCall::InterceptableView] call An interceptable view object for the call class |
| 134 | + # @param [Hash] metadata All the metadata to be sent to the RPC call |
| 135 | + # @return [void] |
| 136 | + def server_streamer method:, request:, call:, metadata:, &block |
| 137 | + # Unused. This is to avoid Rubocop's Lint/UnusedMethodArgument |
| 138 | + _method = method |
| 139 | + _request = request |
| 140 | + _call = call |
| 141 | + update_metadata_for_call metadata, &block |
| 142 | + end |
| 143 | + |
| 144 | + # Intercepts a bidi_streamer rpc call |
| 145 | + # |
| 146 | + # @param [String] method The RPC method name |
| 147 | + # @param [Google::Protobuf::MessageExts] request The request to be sent to the RPC call |
| 148 | + # @param [GRPC::ActiveCall::InterceptableView] call An interceptable view object for the call class |
| 149 | + # @param [Hash] metadata The metadata to be sent to the RPC call |
| 150 | + # @return [void] |
| 151 | + def bidi_streamer method:, request:, call:, metadata:, &block |
| 152 | + # Unused. This is to avoid Rubocop's Lint/UnusedMethodArgument |
| 153 | + _method = method |
| 154 | + _request = request |
| 155 | + _call = call |
| 156 | + update_metadata_for_call metadata, &block |
| 157 | + end |
| 158 | + |
| 159 | + private |
| 160 | + |
| 161 | + # @private |
| 162 | + # Inserts the Spanner request id header to the metadata for the RPC call |
| 163 | + # |
| 164 | + # @param [Hash] metadata The metadata to be sent to the RPC call |
| 165 | + # @return [void] |
| 166 | + def update_metadata_for_call metadata |
| 167 | + request_id = nil |
| 168 | + attempt = 1 |
| 169 | + |
| 170 | + if metadata.include? :"x-goog-spanner-request-id" |
| 171 | + request_id, attempt = get_header_request_id_and_attempt metadata[:"x-goog-spanner-request-id"] |
| 172 | + else |
| 173 | + request_id = @request_mutex.synchronize { @request_id_counter += 1 } |
| 174 | + end |
| 175 | + |
| 176 | + formatted_request_id = format_request_id request_id, attempt |
| 177 | + metadata[:"x-goog-spanner-request-id"] = formatted_request_id |
| 178 | + |
| 179 | + yield |
| 180 | + rescue StandardError => e |
| 181 | + e.instance_variable_set :@spanner_header_id, formatted_request_id |
| 182 | + raise e |
| 183 | + end |
| 184 | + |
| 185 | + # @private |
| 186 | + # Creates the Spanner request id header in the correct format |
| 187 | + # |
| 188 | + # @param [String] request_id The request id of the Spanner request |
| 189 | + # @param [String] attempt The attempt of the current request after retries |
| 190 | + # @return [String] |
| 191 | + def format_request_id request_id, attempt |
| 192 | + "#{@version}.#{@process_id}.#{@client_id}.#{@channel_id}.#{request_id}.#{attempt}" |
| 193 | + end |
| 194 | + |
| 195 | + # @private |
| 196 | + # Parses a request id header and returns the request id and the attempt |
| 197 | + # |
| 198 | + # @param [String] header A string representation of a Spanner request ID. |
| 199 | + # @return [array] |
| 200 | + def get_header_request_id_and_attempt header |
| 201 | + _, _, _, _, request_id, attempt = header.split "." |
| 202 | + [request_id, attempt.to_i + 1] |
| 203 | + end |
| 204 | + end |
| 205 | + end |
| 206 | + end |
| 207 | +end |
0 commit comments