Skip to content

Commit df4be7f

Browse files
authored
refactor: remove deprecated resource based routing support (#73)
Co-authored-by: larkee <larkee@users.noreply.github.com>
1 parent c8c7723 commit df4be7f

File tree

4 files changed

+4
-345
lines changed

4 files changed

+4
-345
lines changed

google/cloud/spanner_v1/client.py

-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class Client(ClientWithProject):
158158

159159
_instance_admin_api = None
160160
_database_admin_api = None
161-
_endpoint_cache = {}
162161
user_agent = None
163162
_SET_PROJECT = True # Used by from_service_account_json()
164163

google/cloud/spanner_v1/database.py

-46
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717
import copy
1818
import functools
1919
import grpc
20-
import os
2120
import re
2221
import threading
23-
import warnings
2422

25-
from google.api_core.client_options import ClientOptions
2623
import google.auth.credentials
2724
from google.protobuf.struct_pb2 import Struct
2825
from google.cloud.exceptions import NotFound
29-
from google.api_core.exceptions import PermissionDenied
3026
import six
3127

3228
# pylint: disable=ungrouped-imports
@@ -67,18 +63,6 @@
6763

6864
_DATABASE_METADATA_FILTER = "name:{0}/operations/"
6965

70-
_RESOURCE_ROUTING_PERMISSIONS_WARNING = (
71-
"The client library attempted to connect to an endpoint closer to your Cloud Spanner data "
72-
"but was unable to do so. The client library will fall back and route requests to the endpoint "
73-
"given in the client options, which may result in increased latency. "
74-
"We recommend including the scope https://www.googleapis.com/auth/spanner.admin so that the "
75-
"client library can get an instance-specific endpoint and efficiently route requests."
76-
)
77-
78-
79-
class ResourceRoutingPermissionsWarning(Warning):
80-
pass
81-
8266

8367
class Database(object):
8468
"""Representation of a Cloud Spanner Database.
@@ -245,36 +229,6 @@ def spanner_api(self):
245229
credentials = self._instance._client.credentials
246230
if isinstance(credentials, google.auth.credentials.Scoped):
247231
credentials = credentials.with_scopes((SPANNER_DATA_SCOPE,))
248-
if (
249-
os.getenv("GOOGLE_CLOUD_SPANNER_ENABLE_RESOURCE_BASED_ROUTING")
250-
== "true"
251-
):
252-
endpoint_cache = self._instance._client._endpoint_cache
253-
if self._instance.name in endpoint_cache:
254-
client_options = ClientOptions(
255-
api_endpoint=endpoint_cache[self._instance.name]
256-
)
257-
else:
258-
try:
259-
api = self._instance._client.instance_admin_api
260-
resp = api.get_instance(
261-
self._instance.name,
262-
field_mask={"paths": ["endpoint_uris"]},
263-
metadata=_metadata_with_prefix(self.name),
264-
)
265-
endpoints = resp.endpoint_uris
266-
if endpoints:
267-
endpoint_cache[self._instance.name] = list(endpoints)[0]
268-
client_options = ClientOptions(
269-
api_endpoint=endpoint_cache[self._instance.name]
270-
)
271-
# If there are no endpoints, use default endpoint.
272-
except PermissionDenied:
273-
warnings.warn(
274-
_RESOURCE_ROUTING_PERMISSIONS_WARNING,
275-
ResourceRoutingPermissionsWarning,
276-
stacklevel=2,
277-
)
278232
self._spanner_api = SpannerClient(
279233
credentials=credentials,
280234
client_info=client_info,

tests/system/test_system.py

-58
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@
5656

5757
CREATE_INSTANCE = os.getenv("GOOGLE_CLOUD_TESTS_CREATE_SPANNER_INSTANCE") is not None
5858
USE_EMULATOR = os.getenv("SPANNER_EMULATOR_HOST") is not None
59-
USE_RESOURCE_ROUTING = (
60-
os.getenv("GOOGLE_CLOUD_SPANNER_ENABLE_RESOURCE_BASED_ROUTING") == "true"
61-
)
6259

6360
if CREATE_INSTANCE:
6461
INSTANCE_ID = "google-cloud" + unique_resource_id("-")
@@ -286,61 +283,6 @@ def tearDown(self):
286283
for doomed in self.to_delete:
287284
doomed.drop()
288285

289-
@unittest.skipUnless(USE_RESOURCE_ROUTING, "requires enabling resource routing")
290-
def test_spanner_api_use_user_specified_endpoint(self):
291-
# Clear cache.
292-
Client._endpoint_cache = {}
293-
api = Config.CLIENT.instance_admin_api
294-
resp = api.get_instance(
295-
Config.INSTANCE.name, field_mask={"paths": ["endpoint_uris"]}
296-
)
297-
if not resp or not resp.endpoint_uris:
298-
return # no resolved endpoint.
299-
resolved_endpoint = resp.endpoint_uris[0]
300-
301-
client = Client(client_options={"api_endpoint": resolved_endpoint})
302-
303-
instance = client.instance(Config.INSTANCE.instance_id)
304-
temp_db_id = "temp_db" + unique_resource_id("_")
305-
temp_db = instance.database(temp_db_id)
306-
temp_db.spanner_api
307-
308-
# No endpoint cache - Default endpoint used.
309-
self.assertEqual(client._endpoint_cache, {})
310-
311-
@unittest.skipUnless(USE_RESOURCE_ROUTING, "requires enabling resource routing")
312-
def test_spanner_api_use_resolved_endpoint(self):
313-
# Clear cache.
314-
Client._endpoint_cache = {}
315-
api = Config.CLIENT.instance_admin_api
316-
resp = api.get_instance(
317-
Config.INSTANCE.name, field_mask={"paths": ["endpoint_uris"]}
318-
)
319-
if not resp or not resp.endpoint_uris:
320-
return # no resolved endpoint.
321-
resolved_endpoint = resp.endpoint_uris[0]
322-
323-
client = Client(
324-
client_options=Config.CLIENT._client_options
325-
) # Use same endpoint as main client.
326-
327-
instance = client.instance(Config.INSTANCE.instance_id)
328-
temp_db_id = "temp_db" + unique_resource_id("_")
329-
temp_db = instance.database(temp_db_id)
330-
temp_db.spanner_api
331-
332-
# Endpoint is cached - resolved endpoint used.
333-
self.assertIn(Config.INSTANCE.name, client._endpoint_cache)
334-
self.assertEqual(
335-
client._endpoint_cache[Config.INSTANCE.name], resolved_endpoint
336-
)
337-
338-
# Endpoint is cached at a class level.
339-
self.assertIn(Config.INSTANCE.name, Config.CLIENT._endpoint_cache)
340-
self.assertEqual(
341-
Config.CLIENT._endpoint_cache[Config.INSTANCE.name], resolved_endpoint
342-
)
343-
344286
def test_list_databases(self):
345287
# Since `Config.INSTANCE` is newly created in `setUpModule`, the
346288
# database created in `setUpClass` here will be the only one.

0 commit comments

Comments
 (0)