Commit 5a0f7a5
committed
perf(spanner): optimize request ID header generation and retry closures
Optimize request ID generation and RPC retry hot paths across Database, Transaction, Snapshot, and Batch:
- Use `int.from_bytes(os.urandom(8), "big")` for process-level random ID generation and pre-format the static process prefix once at import time.
- Cache per-database request ID prefixes directly in `instance.__dict__` via a non-data descriptor (`_CachedPrefixDescriptor`), lazily computing on first access and invalidating when `_channel_id` changes.
- Inline `Database.with_error_augmentation` and construct gRPC metadata lists with unpacking (`[*prior_metadata, ...]`) instead of intermediate list copies and `.append()`.
- Replace thread-safe `AtomicCounter` instances with local integer counters (`attempt = 0`) inside sequential retry closures, and pass active OpenTelemetry spans directly to avoid repeated `contextvars` lookups.
- Eliminate transient `functools.partial` allocations and redundant `*args, **kwargs` unpacking inside retry closures.
- Make `wrap_with_request_id` idempotent on repeated wrapping and replace stale request IDs when retrying failed calls.1 parent 9a1fb24 commit 5a0f7a5
17 files changed
Lines changed: 854 additions & 277 deletions
File tree
- packages/google-cloud-spanner
- google/cloud/spanner_v1
- _async
- tests/unit
- _async
Lines changed: 9 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
| |||
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
28 | | - | |
29 | 27 | | |
30 | 28 | | |
31 | 29 | | |
| |||
341 | 339 | | |
342 | 340 | | |
343 | 341 | | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | 342 | | |
350 | | - | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
351 | 347 | | |
352 | 348 | | |
353 | 349 | | |
| |||
478 | 474 | | |
479 | 475 | | |
480 | 476 | | |
481 | | - | |
| 477 | + | |
482 | 478 | | |
483 | 479 | | |
484 | 480 | | |
| 481 | + | |
| 482 | + | |
485 | 483 | | |
486 | 484 | | |
487 | 485 | | |
488 | 486 | | |
489 | 487 | | |
490 | 488 | | |
491 | | - | |
492 | | - | |
| 489 | + | |
493 | 490 | | |
494 | 491 | | |
495 | 492 | | |
496 | | - | |
| 493 | + | |
497 | 494 | | |
498 | 495 | | |
499 | 496 | | |
500 | 497 | | |
501 | | - | |
502 | 498 | | |
503 | 499 | | |
504 | 500 | | |
| |||
Lines changed: 55 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
62 | | - | |
63 | 61 | | |
64 | 62 | | |
65 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
| |||
180 | 183 | | |
181 | 184 | | |
182 | 185 | | |
| 186 | + | |
| 187 | + | |
183 | 188 | | |
184 | 189 | | |
185 | 190 | | |
| |||
550 | 555 | | |
551 | 556 | | |
552 | 557 | | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
558 | 561 | | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
567 | 566 | | |
568 | 567 | | |
569 | | - | |
| 568 | + | |
570 | 569 | | |
571 | 570 | | |
572 | 571 | | |
| |||
585 | 584 | | |
586 | 585 | | |
587 | 586 | | |
588 | | - | |
589 | | - | |
590 | | - | |
591 | | - | |
592 | | - | |
593 | | - | |
594 | | - | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
595 | 592 | | |
596 | 593 | | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
597 | 617 | | |
598 | | - | |
| 618 | + | |
599 | 619 | | |
600 | 620 | | |
601 | 621 | | |
| |||
614 | 634 | | |
615 | 635 | | |
616 | 636 | | |
617 | | - | |
618 | | - | |
619 | | - | |
620 | | - | |
621 | | - | |
622 | | - | |
623 | | - | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
624 | 642 | | |
625 | 643 | | |
626 | | - | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
627 | 648 | | |
628 | 649 | | |
629 | 650 | | |
| |||
975 | 996 | | |
976 | 997 | | |
977 | 998 | | |
978 | | - | |
| 999 | + | |
979 | 1000 | | |
980 | 1001 | | |
981 | 1002 | | |
982 | 1003 | | |
983 | 1004 | | |
984 | | - | |
| 1005 | + | |
985 | 1006 | | |
986 | 1007 | | |
987 | 1008 | | |
| |||
Lines changed: 18 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | 34 | | |
36 | 35 | | |
37 | 36 | | |
| |||
760 | 759 | | |
761 | 760 | | |
762 | 761 | | |
763 | | - | |
| 762 | + | |
764 | 763 | | |
765 | 764 | | |
| 765 | + | |
| 766 | + | |
766 | 767 | | |
767 | | - | |
| 768 | + | |
768 | 769 | | |
769 | | - | |
770 | | - | |
| 770 | + | |
771 | 771 | | |
772 | 772 | | |
773 | 773 | | |
774 | 774 | | |
775 | 775 | | |
776 | | - | |
777 | 776 | | |
778 | 777 | | |
779 | 778 | | |
| |||
843 | 842 | | |
844 | 843 | | |
845 | 844 | | |
846 | | - | |
| 845 | + | |
847 | 846 | | |
848 | 847 | | |
| 848 | + | |
| 849 | + | |
849 | 850 | | |
850 | | - | |
| 851 | + | |
851 | 852 | | |
852 | | - | |
853 | | - | |
| 853 | + | |
854 | 854 | | |
855 | 855 | | |
856 | 856 | | |
857 | 857 | | |
858 | 858 | | |
859 | | - | |
860 | 859 | | |
861 | 860 | | |
862 | 861 | | |
| |||
917 | 916 | | |
918 | 917 | | |
919 | 918 | | |
920 | | - | |
| 919 | + | |
921 | 920 | | |
922 | 921 | | |
| 922 | + | |
| 923 | + | |
923 | 924 | | |
924 | 925 | | |
925 | 926 | | |
926 | 927 | | |
927 | | - | |
928 | | - | |
929 | | - | |
930 | | - | |
931 | | - | |
932 | | - | |
| 928 | + | |
933 | 929 | | |
934 | 930 | | |
935 | | - | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
936 | 935 | | |
937 | 936 | | |
938 | 937 | | |
| |||
0 commit comments