Repository navigation
fix(spanner): preserve emulator_host and labels in Instance.copy() - #19302
Conversation
## Problem Statement - Calling `Instance.copy()` on either the synchronous or asynchronous Spanner `Instance` omits `emulator_host` and `labels`. - When using the Spanner emulator (`SPANNER_EMULATOR_HOST` or `AnonymousCredentials` with a custom `api_endpoint`), admin calls on the copied instance target the emulator while data-plane calls through `Database` fall back to `spanner.googleapis.com`. - Dropping `labels` on copy also causes `instance.copy().update()` to clear existing server-side labels and `instance.copy().create()` to omit the original labels. ## Solution - Preserve `emulator_host` and shallow-copy `labels` into a fresh `dict` in synchronous and asynchronous `Instance.copy()`, so copied instances keep emulator data-plane routing and retain non-aliased labels across `create()` and `update()` calls. - Behavior changes: - `instance.copy().update()` now preserves existing labels instead of clearing them. - `instance.copy().create()` now sends the original labels. - Under emulator configuration, data-plane RPCs on a copied instance now route to the emulator rather than `spanner.googleapis.com`. - Add regression coverage verifying sync and async `Client.copy()` and `Instance.copy()` preserve programmatic `universe_domain`, `emulator_host`, and `labels` without aliasing.
There was a problem hiding this comment.
Code Review
This pull request updates the copy() method in both the synchronous and asynchronous Instance classes to preserve the emulator_host and a shallow copy of labels. It also adds comprehensive unit tests to verify that the universe domain, emulator host, and labels are correctly preserved when copying clients and instances. The review feedback highlights a potential issue where converting None labels to an empty dictionary could unintentionally clear labels on the server during updates. The reviewer suggests preserving None values for labels in both Instance implementations and updating the corresponding test cases to verify this behavior.
olavloite
left a comment
There was a problem hiding this comment.
Remove the two duplicated tests, otherwise LGTM
Problem Statement
Instance.copy()on either the synchronous or asynchronous SpannerInstanceomitsemulator_hostandlabels.SPANNER_EMULATOR_HOSTorAnonymousCredentialswith a customapi_endpoint), admin calls on the copied instance target the emulator while data-plane calls throughDatabasefall back tospanner.googleapis.com.labelson copy also causesinstance.copy().update()to clear existing server-side labels andinstance.copy().create()to omit the original labels.Solution
emulator_hostand shallow-copylabelsinto a freshdictin synchronous and asynchronousInstance.copy(), so copied instances keep emulator data-plane routing and retain non-aliased labels acrosscreate()andupdate()calls.instance.copy().update()now preserves existing labels instead of clearing them.instance.copy().create()now sends the original labels.spanner.googleapis.com.Client.copy()andInstance.copy()preserve programmaticuniverse_domain,emulator_host, andlabelswithout aliasing.