|
| 1 | +// Copyright 2025 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 | +using Grpc.Core; |
| 16 | +using Google.Api.Gax.Grpc; |
| 17 | +using System; |
| 18 | +using System.Linq; |
| 19 | +using Xunit; |
| 20 | +using Google.Cloud.Spanner.V1.Tests.Common; |
| 21 | + |
| 22 | +namespace Google.Cloud.Spanner.V1.Tests; |
| 23 | + |
| 24 | +public class RequestIdTests |
| 25 | +{ |
| 26 | + private const string SampleDatabaseName = "projects/proj/instances/inst/databases/db"; |
| 27 | + private const string SampleSessionName = "projects/proj/instances/inst/databases/db/sessions/sess"; |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [MemberData(nameof(SpannerClientActions))] |
| 31 | + public void RequestId_Format(Action<SpannerClient> action) |
| 32 | + { |
| 33 | + var invoker = new SyncFailureCallInvoker(0); |
| 34 | + var client = new SpannerClientBuilder { CallInvoker = invoker }.Build(); |
| 35 | + |
| 36 | + action(client); |
| 37 | + |
| 38 | + // The expected format is 6 parts that break down as such: |
| 39 | + // {VersionId}.{ProcessId}.{ClientId}.{ChannelId}.{RequestId}.{AttemptNum} |
| 40 | + Metadata headerMetadata = invoker.CapturedMetadata[0]; |
| 41 | + var idString = headerMetadata.Get("x-goog-spanner-request-id").Value; |
| 42 | + |
| 43 | + var parts = idString.Split('.'); |
| 44 | + |
| 45 | + Assert.Equal(6, parts.Length); |
| 46 | + var versionId = parts[0]; |
| 47 | + var processId = parts[1]; |
| 48 | + var clientId = parts[2]; |
| 49 | + var channelId = parts[3]; |
| 50 | + var requestId = parts[4]; |
| 51 | + var attemptNum = parts[5]; |
| 52 | + |
| 53 | + Assert.Equal("1", versionId); |
| 54 | + Assert.True(ulong.TryParse(processId, out _)); |
| 55 | + Assert.True(int.TryParse(clientId, out _)); |
| 56 | + Assert.Equal("1", channelId); |
| 57 | + Assert.True(int.TryParse(requestId, out _)); |
| 58 | + Assert.Equal("1", attemptNum); |
| 59 | + } |
| 60 | + |
| 61 | + [Theory] |
| 62 | + [MemberData(nameof(SpannerClientActions))] |
| 63 | + public void SetsHeaderOnRpcCalls(Action<SpannerClient> action) |
| 64 | + { |
| 65 | + var invoker = new SyncFailureCallInvoker(0); |
| 66 | + var client = new SpannerClientBuilder { CallInvoker = invoker }.Build(); |
| 67 | + action(client); |
| 68 | + Metadata.Entry entry = Assert.Single(invoker.CapturedMetadata[0], e => e.Key == "x-goog-spanner-request-id"); |
| 69 | + Assert.NotNull(entry.Value); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void IncrementsRequestIdOnRetry() |
| 74 | + { |
| 75 | + var invoker = new SyncFailureCallInvoker(numberOfFailuresToSimulate: 1, statusCodeToThrow: StatusCode.ResourceExhausted); |
| 76 | + var settings = new SpannerSettings |
| 77 | + { |
| 78 | + // Configure the CreateSession call to retry on Unavailable errors. |
| 79 | + CallSettings = CallSettings.FromRetry(RetrySettings.FromExponentialBackoff( |
| 80 | + maxAttempts: 3, |
| 81 | + initialBackoff: TimeSpan.FromMilliseconds(1), |
| 82 | + maxBackoff: TimeSpan.FromMilliseconds(1), |
| 83 | + backoffMultiplier: 1.0, |
| 84 | + retryFilter: RetrySettings.FilterForStatusCodes(StatusCode.Unavailable))) |
| 85 | + }; |
| 86 | + var client = new SpannerClientBuilder { CallInvoker = invoker, Settings = settings }.Build(); |
| 87 | + |
| 88 | + client.CreateSession(new CreateSessionRequest { Database = SampleDatabaseName }); |
| 89 | + client.CreateSession(new CreateSessionRequest { Database = SampleDatabaseName }); |
| 90 | + client.CreateSession(new CreateSessionRequest { Database = SampleDatabaseName }); |
| 91 | + |
| 92 | + // Assert that the invoker was called four times for the three client calls. |
| 93 | + // The first call should have failed the first time and succeeded on retry. |
| 94 | + Assert.Equal(4, invoker.CapturedMetadata.Count); |
| 95 | + |
| 96 | + var requestIds = invoker.CapturedMetadata |
| 97 | + .Select(m => m.Single(e => e.Key == "x-goog-spanner-request-id").Value) |
| 98 | + .Select(id => new SpannerRequestIdParts(id)) |
| 99 | + .ToList(); |
| 100 | + |
| 101 | + Assert.Equal((1, 1), (requestIds[0].RequestId, requestIds[0].AttemptNum)); |
| 102 | + Assert.Equal((1, 2), (requestIds[1].RequestId, requestIds[1].AttemptNum)); |
| 103 | + Assert.Equal((2, 1), (requestIds[2].RequestId, requestIds[2].AttemptNum)); |
| 104 | + Assert.Equal((3, 1), (requestIds[3].RequestId, requestIds[3].AttemptNum)); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void RequestIdSource_ProcessId_OverwritingBehavior() => |
| 109 | + // The process ID should only ever be set once per process. |
| 110 | + Assert.Throws<InvalidOperationException>(() => |
| 111 | + { |
| 112 | + // Note in the case of test re-runs within the same process the |
| 113 | + // "first_override" may cause the exception to be thrown as it was |
| 114 | + // set prior. |
| 115 | + SpannerClientImpl.ProcessId = 1UL; |
| 116 | + SpannerClientImpl.ProcessId = 2UL; |
| 117 | + }); |
| 118 | + |
| 119 | + public static TheoryData<Action<SpannerClient>> SpannerClientActions { get; } = new TheoryData<Action<SpannerClient>> |
| 120 | + { |
| 121 | + client => client.ExecuteSql(new ExecuteSqlRequest { Session = SampleSessionName, Sql = "SELECT 1" }), |
| 122 | + client => client.ExecuteStreamingSql(new ExecuteSqlRequest { Session = SampleSessionName, Sql = "SELECT 1" }), |
| 123 | + client => client.GetSession(new GetSessionRequest { Name = SampleSessionName }), |
| 124 | + client => client.ListSessions(new ListSessionsRequest { Database = SampleDatabaseName }).AsRawResponses().First(), |
| 125 | + client => client.DeleteSession(new DeleteSessionRequest { Name = SampleSessionName }), |
| 126 | + client => client.ExecuteSql(new ExecuteSqlRequest { Session = SampleSessionName }), |
| 127 | + client => client.ExecuteBatchDml(new ExecuteBatchDmlRequest { Session = SampleSessionName }), |
| 128 | + client => client.Read(new ReadRequest { Session = SampleSessionName }), |
| 129 | + client => client.StreamingRead(new ReadRequest { Session = SampleSessionName }), |
| 130 | + client => client.BeginTransaction(new BeginTransactionRequest { Session = SampleSessionName }), |
| 131 | + client => client.Commit(new CommitRequest { Session = SampleSessionName }), |
| 132 | + client => client.Rollback(new RollbackRequest { Session = SampleSessionName }), |
| 133 | + client => client.PartitionQuery(new PartitionQueryRequest { Session = SampleSessionName }), |
| 134 | + client => client.PartitionRead(new PartitionReadRequest { Session = SampleSessionName }), |
| 135 | + }; |
| 136 | + |
| 137 | + private struct SpannerRequestIdParts |
| 138 | + { |
| 139 | + public int VersionId { get; } |
| 140 | + public ulong ProcessId { get; } |
| 141 | + public int ClientId { get; } |
| 142 | + public int ChannelId { get; } |
| 143 | + public int RequestId { get; } |
| 144 | + public int AttemptNum { get; } |
| 145 | + |
| 146 | + public SpannerRequestIdParts(string requestId) |
| 147 | + { |
| 148 | + var parts = requestId.Split('.'); |
| 149 | + Assert.Equal(6, parts.Length); |
| 150 | + |
| 151 | + VersionId = int.Parse(parts[0]); |
| 152 | + ProcessId = ulong.Parse(parts[1]); |
| 153 | + ClientId = int.Parse(parts[2]); |
| 154 | + ChannelId = int.Parse(parts[3]); |
| 155 | + RequestId = int.Parse(parts[4]); |
| 156 | + AttemptNum = int.Parse(parts[5]); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments