google-cloud-spanner 0.34.5-preview

Google Cloud Client Libraries for Rust - Spanner
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
// 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.

//! Types and utilities for generating Spanner Request IDs (`x-goog-spanner-request-id`).
//!
//! Spanner Request IDs are structured strings sent in gRPC headers to uniquely identify
//! client instances, channels, requests, and retry attempts:
//! `<VERSION>.<RAND_PROCESS_ID>.<nthClientId>.<nthChannelId>.<nthRequest>.<attempt>`

use std::fmt::Write as _;
use std::sync::LazyLock;
use std::sync::atomic::{AtomicU64, Ordering};

/// The Spanner Request ID protocol version.
const VERSION: &str = "1";

/// A 64-bit random value formatted as 16 lowercase hexadecimal characters (`"%016x"`),
/// generated once per process lifetime.
pub(crate) static RAND_PROCESS_ID: LazyLock<String> =
    LazyLock::new(|| format!("{:016x}", rand::random::<u64>()));

static NEXT_CLIENT_ID: AtomicU64 = AtomicU64::new(1);

/// A generator for Spanner Request IDs (`x-goog-spanner-request-id`).
///
/// Each `RequestIdCreator` receives a unique `client_id` upon creation and pre-computes
/// the static prefix `<VERSION>.<RAND_PROCESS_ID>.<client_id>.` to minimize string formatting
/// overhead on RPC invocations.
#[derive(Debug)]
pub(crate) struct RequestIdCreator {
    client_prefix: String,
    next_request_id: AtomicU64,
}

impl RequestIdCreator {
    /// Constructs a new `RequestIdCreator` with an atomically incremented client ID.
    ///
    /// We use `Ordering::Relaxed` because client and request IDs only require atomic uniqueness
    /// and monotonic increments across threads without synchronizing other memory accesses.
    pub(crate) fn new() -> Self {
        let client_id = NEXT_CLIENT_ID.fetch_add(1, Ordering::Relaxed);
        let client_prefix = format!("{VERSION}.{}.{}.", *RAND_PROCESS_ID, client_id);
        Self {
            client_prefix,
            next_request_id: AtomicU64::new(1),
        }
    }
}

impl Default for RequestIdCreator {
    fn default() -> Self {
        Self::new()
    }
}

impl RequestIdCreator {
    /// Returns a Request ID prefix string for a new RPC, excluding the attempt suffix:
    /// `"1.<RAND_PROCESS_ID>.<client_id>.<channel_id>.<request_id>."`
    ///
    /// The returned string ends with a dot (`'.'`), ready for the attempt interceptor
    /// (`SpannerRequestIdInterceptor`) to append the 1-based attempt number on each retry.
    ///
    /// # Arguments
    /// * `channel_id` - The 1-based channel identifier (`1, 2, ...`), or `0` for an unknown channel.
    pub(crate) fn next_id_prefix(&self, channel_id: usize) -> String {
        let request_id = self.next_request_id.fetch_add(1, Ordering::Relaxed);
        let mut result = String::with_capacity(self.client_prefix.len() + 48);
        result.push_str(&self.client_prefix);
        let _ = write!(result, "{channel_id}.{request_id}.");
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::Spanner;
    use crate::observability::Observability;
    use gaxi::grpc::tonic::{Response, Status};
    use google_cloud_auth::credentials::anonymous::Builder as Anonymous;
    use google_cloud_test_macros::tokio_test_no_panics;
    use spanner_grpc_mock::google::spanner::v1 as mock_v1;
    use spanner_grpc_mock::{MockSpanner, start};
    use std::sync::{Arc, Mutex};

    #[test]
    fn traits() {
        static_assertions::assert_impl_all!(
            RequestIdCreator: Send,
            Sync,
            std::fmt::Debug,
            Default
        );
    }

    #[test]
    fn rand_process_id_format() {
        let process_id = &*RAND_PROCESS_ID;
        assert_eq!(process_id.len(), 16);
        assert!(
            process_id
                .chars()
                .all(|char| char.is_ascii_digit() || ('a'..='f').contains(&char)),
            "RAND_PROCESS_ID must be 16 lowercase hex characters, got {process_id}"
        );
    }

    #[test]
    fn request_id_creator_sequence() {
        let creator = RequestIdCreator::new();

        let base_id1 = creator.next_id_prefix(1);
        assert!(
            base_id1.starts_with(&format!("{VERSION}.{}.", *RAND_PROCESS_ID)),
            "prefix should start with VERSION and RAND_PROCESS_ID, got {base_id1}"
        );
        assert!(
            base_id1.ends_with(".1.1."),
            "first call on channel 1 should end with .1.1., got {base_id1}"
        );

        let base_id2 = creator.next_id_prefix(1);
        assert!(
            base_id2.ends_with(".1.2."),
            "second call on channel 1 should end with .1.2., got {base_id2}"
        );

        let base_id3 = creator.next_id_prefix(2);
        assert!(
            base_id3.ends_with(".2.3."),
            "third call on channel 2 should end with .2.3., got {base_id3}"
        );
    }

    #[test]
    fn multiple_creators_unique_client_ids() {
        let creator1 = RequestIdCreator::new();
        let creator2 = RequestIdCreator::new();
        assert_ne!(creator1.next_id_prefix(1), creator2.next_id_prefix(1));
    }

    #[tokio_test_no_panics]
    async fn request_id_header_sent_unary_rpc() {
        let captured = Arc::new(Mutex::new(Vec::new()));
        let mut mock = MockSpanner::new();
        let mut seq = mockall::Sequence::new();

        // 1. Initial attempt of first RPC -> records header and returns UNAVAILABLE to force retry
        let captured_clone = Arc::clone(&captured);
        mock.expect_create_session()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for unary RPC")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);
                Err(Status::unavailable("server is unavailable"))
            });

        // 2. Retry attempt of first RPC -> records header and returns Ok(Session)
        let captured_clone = Arc::clone(&captured);
        mock.expect_create_session()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for unary RPC")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);
                Ok(Response::new(mock_v1::Session {
                    name: "projects/p/instances/i/databases/d/sessions/s1".to_string(),
                    ..Default::default()
                }))
            });

        // 3. Second RPC -> records header and returns Ok(Session)
        let captured_clone = Arc::clone(&captured);
        mock.expect_create_session()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for unary RPC")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);
                Ok(Response::new(mock_v1::Session {
                    name: "projects/p/instances/i/databases/d/sessions/s2".to_string(),
                    ..Default::default()
                }))
            });

        let (address, _server) = start("127.0.0.1:0", mock)
            .await
            .expect("Failed to start mock server");
        let client = Spanner::builder()
            .with_endpoint(address)
            .with_credentials(Anonymous::new().build())
            .build()
            .await
            .expect("Failed to build Spanner client");

        let request = crate::model::CreateSessionRequest {
            database: "projects/p/instances/i/databases/d".to_string(),
            ..Default::default()
        };

        // Execute first RPC (attempt 1 -> UNAVAILABLE, attempt 2 -> success)
        let _session1 = client
            .create_session(
                request.clone(),
                crate::RequestOptions::default(),
                client.get_channel(0),
                &Observability::disabled_arc(),
            )
            .await
            .expect("first create_session should succeed after retry");

        // Execute second RPC (attempt 1 -> success)
        let _session2 = client
            .create_session(
                request,
                crate::RequestOptions::default(),
                client.get_channel(0),
                &Observability::disabled_arc(),
            )
            .await
            .expect("second create_session should succeed");

        let ids = captured.lock().expect("mutex lock should succeed");
        assert_eq!(ids.len(), 3, "should have captured 3 RPC attempt headers");

        let id_rpc1_attempt1 = &ids[0];
        let id_rpc1_attempt2 = &ids[1];
        let id_rpc2_attempt1 = &ids[2];

        assert!(
            id_rpc1_attempt1.starts_with("1."),
            "Request ID should start with version 1, got {id_rpc1_attempt1}"
        );

        let prefix1_attempt1 = id_rpc1_attempt1
            .rsplit_once('.')
            .expect("should have dot separator")
            .0;
        let prefix1_attempt2 = id_rpc1_attempt2
            .rsplit_once('.')
            .expect("should have dot separator")
            .0;
        assert_eq!(
            prefix1_attempt2, prefix1_attempt1,
            "Retry attempt should have exactly the same values as initial attempt"
        );
        assert!(
            id_rpc1_attempt1.ends_with(".1"),
            "Initial attempt should end with .1, got {id_rpc1_attempt1}"
        );
        assert!(
            id_rpc1_attempt2.ends_with(".2"),
            "Retry attempt should end with .2, got {id_rpc1_attempt2}"
        );

        let req_num_1: u64 = id_rpc1_attempt1
            .split('.')
            .nth(4)
            .expect("request_id field")
            .parse()
            .expect("numeric request_id");
        let req_num_2: u64 = id_rpc2_attempt1
            .split('.')
            .nth(4)
            .expect("request_id field")
            .parse()
            .expect("numeric request_id");
        assert_eq!(
            req_num_2,
            req_num_1 + 1,
            "Following request should use a Request ID that is 1 higher ({id_rpc2_attempt1} vs {id_rpc1_attempt1})"
        );
        assert!(
            id_rpc2_attempt1.ends_with(".1"),
            "Following request initial attempt should end with .1, got {id_rpc2_attempt1}"
        );
    }

    #[tokio_test_no_panics]
    async fn request_id_header_sent_streaming_rpc() {
        use crate::result_set::tests::adapt;

        let captured = Arc::new(Mutex::new(Vec::new()));
        let mut mock = MockSpanner::new();
        let mut seq = mockall::Sequence::new();

        // 1. Initial attempt of first streaming SQL -> records header and returns stream with 1 row + UNAVAILABLE
        let captured_clone = Arc::clone(&captured);
        mock.expect_execute_streaming_sql()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for streaming RPC")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);

                let prs1 = mock_v1::PartialResultSet {
                    metadata: Some(mock_v1::ResultSetMetadata {
                        row_type: Some(mock_v1::StructType {
                            fields: vec![mock_v1::struct_type::Field {
                                name: "col0".to_string(),
                                ..Default::default()
                            }],
                        }),
                        ..Default::default()
                    }),
                    values: vec![prost_types::Value {
                        kind: Some(prost_types::value::Kind::StringValue("row1".to_string())),
                    }],
                    resume_token: b"token1".to_vec(),
                    ..Default::default()
                };
                let stream = adapt([Ok(prs1), Err(Status::unavailable("server is unavailable"))]);
                Ok(Response::from(stream))
            });

        // 2. Retry attempt of first streaming SQL -> records header and returns stream with row2 (last)
        let captured_clone = Arc::clone(&captured);
        mock.expect_execute_streaming_sql()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for streaming RPC")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);

                let prs2 = mock_v1::PartialResultSet {
                    values: vec![prost_types::Value {
                        kind: Some(prost_types::value::Kind::StringValue("row2".to_string())),
                    }],
                    resume_token: b"token2".to_vec(),
                    last: true,
                    ..Default::default()
                };
                let stream = adapt([Ok(prs2)]);
                Ok(Response::from(stream))
            });

        // 3. Second streaming query -> records header and returns stream with row3 (last)
        let captured_clone = Arc::clone(&captured);
        mock.expect_execute_streaming_sql()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for streaming RPC")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);

                let prs3 = mock_v1::PartialResultSet {
                    metadata: Some(mock_v1::ResultSetMetadata {
                        row_type: Some(mock_v1::StructType {
                            fields: vec![mock_v1::struct_type::Field {
                                name: "col0".to_string(),
                                ..Default::default()
                            }],
                        }),
                        ..Default::default()
                    }),
                    values: vec![prost_types::Value {
                        kind: Some(prost_types::value::Kind::StringValue("row3".to_string())),
                    }],
                    resume_token: b"token3".to_vec(),
                    last: true,
                    ..Default::default()
                };
                let stream = adapt([Ok(prs3)]);
                Ok(Response::from(stream))
            });

        mock.expect_create_session().returning(|_| {
            Ok(Response::new(mock_v1::Session {
                name: "projects/p/instances/i/databases/d/sessions/s1".to_string(),
                multiplexed: true,
                ..Default::default()
            }))
        });

        let (address, _server) = start("127.0.0.1:0", mock)
            .await
            .expect("Failed to start mock server");
        let client = Spanner::builder()
            .with_endpoint(address)
            .with_credentials(Anonymous::new().build())
            .build()
            .await
            .expect("Failed to build Spanner client");

        let db_client = client
            .database_client("projects/p/instances/i/databases/d")
            .build()
            .await
            .expect("database client build should succeed");

        // Execute first streaming query via single_use().execute_query (attempt 1 -> row1 + UNAVAILABLE, attempt 2 -> row2)
        let mut rs1 = db_client
            .single_use()
            .build()
            .execute_query("SELECT 1")
            .await
            .expect("first execute_query should succeed");
        while let Some(row) = rs1.next().await {
            row.expect("row should succeed after stream retry");
        }

        // Execute second streaming query via single_use().execute_query (attempt 1 -> row3)
        let mut rs2 = db_client
            .single_use()
            .build()
            .execute_query("SELECT 2")
            .await
            .expect("second execute_query should succeed");
        while let Some(row) = rs2.next().await {
            row.expect("row should succeed");
        }

        let ids = captured.lock().expect("mutex lock should succeed");
        assert_eq!(ids.len(), 3, "should have captured 3 RPC attempt headers");

        let id_rpc1_attempt1 = &ids[0];
        let id_rpc1_attempt2 = &ids[1];
        let id_rpc2_attempt1 = &ids[2];

        assert!(
            id_rpc1_attempt1.starts_with("1."),
            "Request ID should start with version 1, got {id_rpc1_attempt1}"
        );

        let prefix1_attempt1 = id_rpc1_attempt1
            .rsplit_once('.')
            .expect("should have dot separator")
            .0;
        let prefix1_attempt2 = id_rpc1_attempt2
            .rsplit_once('.')
            .expect("should have dot separator")
            .0;
        assert_eq!(
            prefix1_attempt2, prefix1_attempt1,
            "Retry attempt should have exactly the same values as initial attempt"
        );
        assert!(
            id_rpc1_attempt1.ends_with(".1"),
            "Initial attempt should end with .1, got {id_rpc1_attempt1}"
        );
        assert!(
            id_rpc1_attempt2.ends_with(".2"),
            "Retry attempt should end with .2, got {id_rpc1_attempt2}"
        );

        let req_num_1: u64 = id_rpc1_attempt1
            .split('.')
            .nth(4)
            .expect("request_id field")
            .parse()
            .expect("numeric request_id");
        let req_num_2: u64 = id_rpc2_attempt1
            .split('.')
            .nth(4)
            .expect("request_id field")
            .parse()
            .expect("numeric request_id");
        assert_eq!(
            req_num_2,
            req_num_1 + 1,
            "Following request should use a Request ID that is 1 higher ({id_rpc2_attempt1} vs {id_rpc1_attempt1})"
        );
        assert!(
            id_rpc2_attempt1.ends_with(".1"),
            "Following request initial attempt should end with .1, got {id_rpc2_attempt1}"
        );
    }

    #[tokio_test_no_panics]
    async fn request_id_header_sent_read_write_transaction_aborted_retry() {
        use crate::transaction_retry_policy::tests::create_aborted_status;

        let captured = Arc::new(Mutex::new(Vec::new()));
        let mut mock = MockSpanner::new();
        let mut seq = mockall::Sequence::new();

        mock.expect_create_session()
            .once()
            .in_sequence(&mut seq)
            .returning(|_| {
                Ok(Response::new(mock_v1::Session {
                    name: "projects/p/instances/i/databases/d/sessions/s1".to_string(),
                    multiplexed: true,
                    ..Default::default()
                }))
            });

        // 1. First transaction attempt -> execute_sql fails with ABORTED
        let captured_clone = Arc::clone(&captured);
        mock.expect_execute_sql()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for execute_sql")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);
                Err(create_aborted_status(std::time::Duration::from_nanos(1)))
            });

        // 2. Second transaction attempt (after ABORTED retry) -> execute_sql succeeds
        let captured_clone = Arc::clone(&captured);
        mock.expect_execute_sql()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for execute_sql")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);
                Ok(Response::new(mock_v1::ResultSet {
                    metadata: Some(mock_v1::ResultSetMetadata {
                        transaction: Some(mock_v1::Transaction {
                            id: vec![8, 8, 8],
                            ..Default::default()
                        }),
                        ..Default::default()
                    }),
                    stats: Some(mock_v1::ResultSetStats {
                        row_count: Some(mock_v1::result_set_stats::RowCount::RowCountExact(1)),
                        ..Default::default()
                    }),
                    ..Default::default()
                }))
            });

        // 3. Second transaction attempt -> commit succeeds
        let captured_clone = Arc::clone(&captured);
        mock.expect_commit()
            .once()
            .in_sequence(&mut seq)
            .returning(move |req| {
                let request_id = req
                    .metadata()
                    .get("x-goog-spanner-request-id")
                    .expect("x-goog-spanner-request-id should be sent for commit")
                    .to_str()
                    .expect("should be valid ASCII")
                    .to_string();
                captured_clone
                    .lock()
                    .expect("mutex lock should succeed")
                    .push(request_id);
                Ok(Response::new(mock_v1::CommitResponse::default()))
            });

        let (address, _server) = start("127.0.0.1:0", mock)
            .await
            .expect("Failed to start mock server");
        let client = Spanner::builder()
            .with_endpoint(address)
            .with_credentials(Anonymous::new().build())
            .build()
            .await
            .expect("Failed to build Spanner client");

        let db_client = client
            .database_client("projects/p/instances/i/databases/d")
            .build()
            .await
            .expect("database client build should succeed");

        let count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let count_clone = Arc::clone(&count);

        let runner = db_client
            .read_write_transaction()
            .build()
            .await
            .expect("runner build");

        runner
            .run(async |tx| {
                count_clone.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                tx.execute_update("UPDATE foo SET bar = 1").await?;
                Ok(())
            })
            .await
            .expect("transaction should succeed after aborted retry");

        assert_eq!(
            count.load(std::sync::atomic::Ordering::Relaxed),
            2,
            "transaction closure should have run twice"
        );

        let ids = captured.lock().expect("mutex lock should succeed");
        assert_eq!(
            ids.len(),
            3,
            "should have captured 3 RPC attempt headers (execute_sql attempt 1, execute_sql attempt 2, commit)"
        );

        let id_exec1 = &ids[0];
        let id_exec2 = &ids[1];
        let id_commit = &ids[2];

        // 1. Verify that all 3 RPCs have attempt number .1 (retrying an aborted transaction does NOT bump the attempt number)
        assert!(
            id_exec1.ends_with(".1"),
            "First transaction execute_sql should end with .1, got {id_exec1}"
        );
        assert!(
            id_exec2.ends_with(".1"),
            "Retried transaction execute_sql should end with .1, got {id_exec2}"
        );
        assert!(
            id_commit.ends_with(".1"),
            "Commit should end with .1, got {id_commit}"
        );

        // 2. Verify that RequestIds are monotonically increasing across the aborted transaction retry
        let req_num_exec1: u64 = id_exec1
            .split('.')
            .nth(4)
            .expect("request_id field")
            .parse()
            .expect("numeric request_id");
        let req_num_exec2: u64 = id_exec2
            .split('.')
            .nth(4)
            .expect("request_id field")
            .parse()
            .expect("numeric request_id");
        let req_num_commit: u64 = id_commit
            .split('.')
            .nth(4)
            .expect("request_id field")
            .parse()
            .expect("numeric request_id");

        assert_eq!(
            req_num_exec2,
            req_num_exec1 + 1,
            "Retried transaction execute_sql should increment Request ID by 1 ({req_num_exec2} vs {req_num_exec1})"
        );
        assert_eq!(
            req_num_commit,
            req_num_exec2 + 1,
            "Commit should increment Request ID by 1 ({req_num_commit} vs {req_num_exec2})"
        );
    }
}