Skip to content

Commit 225df82

Browse files
committed
reverted _is_legacy_client
1 parent d173efe commit 225df82

File tree

6 files changed

+86
-74
lines changed

6 files changed

+86
-74
lines changed

google/cloud/bigtable/client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* a :class:`~google.cloud.bigtable.table.Table` owns a
2828
:class:`~google.cloud.bigtable.row.Row` (and all the cells in the row)
2929
"""
30+
import copy
3031
import os
3132
import warnings
3233
import grpc # type: ignore
@@ -364,12 +365,14 @@ def instance_admin_client(self):
364365
def _veneer_data_client(self):
365366
"""Getter for the new Data Table API."""
366367
if self._table_data_client is None:
368+
client_info = copy.copy(self._client_info)
369+
client_info.client_library_version = f"{bigtable.__version__}-data-shim"
367370
self._table_data_client = BigtableDataClient(
368371
project=self.project,
369372
credentials=self._credentials,
370373
client_options=self._client_options,
371-
_client_info=self._client_info,
372-
_is_legacy_client=True,
374+
_client_info=client_info,
375+
_disable_background_refresh=True,
373376
)
374377
return self._table_data_client
375378

google/cloud/bigtable/data/_async/client.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ def __init__(
185185
if "pool_size" in kwargs:
186186
warnings.warn("pool_size no longer supported")
187187

188-
# Private argument, for internal use only
189-
self._is_legacy_client = bool(kwargs.get("_is_legacy_client", False))
190-
191188
# set up client info headers for veneer library. _client_info is for internal use only,
192189
# for the legacy client shim.
193-
self.client_info = kwargs.get("_client_info", DEFAULT_CLIENT_INFO)
194-
self.client_info.client_library_version = self._client_version()
190+
if kwargs.get("_client_info"):
191+
self.client_info = kwargs["_client_info"]
192+
else:
193+
self.client_info = DEFAULT_CLIENT_INFO
194+
self.client_info.client_library_version = self._client_version()
195195

196196
# parse client options
197197
if type(client_options) is dict:
@@ -242,6 +242,8 @@ def __init__(
242242
"is the default."
243243
)
244244
self._is_closed = CrossSync.Event()
245+
# Private argument, for internal use only
246+
self._disable_background_refresh = bool(kwargs.get("_disable_background_refresh", False))
245247
self.transport = cast(TransportType, self._gapic_client.transport)
246248
# keep track of active instances to for warmup on channel refresh
247249
self._active_instances: Set[_WarmedInstanceKey] = set()
@@ -306,13 +308,12 @@ def api_endpoint(self) -> str:
306308
"""
307309
return self._gapic_client.api_endpoint
308310

309-
def _client_version(self) -> str:
311+
@staticmethod
312+
def _client_version() -> str:
310313
"""
311314
Helper function to return the client version string for this client
312315
"""
313316
version_str = f"{google.cloud.bigtable.__version__}-data"
314-
if self._is_legacy_client:
315-
version_str += "-shim"
316317
if CrossSync.is_async:
317318
version_str += "-async"
318319
return version_str
@@ -336,7 +337,7 @@ def _start_background_channel_refresh(self) -> None:
336337
not self._channel_refresh_task
337338
and not self._emulator_host
338339
and not self._is_closed.is_set()
339-
and not self._is_legacy_client
340+
and not self._disable_background_refresh
340341
):
341342
# raise error if not in an event loop in async client
342343
CrossSync.verify_async_event_loop()

google/cloud/bigtable/data/_sync_autogen/client.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ def __init__(
127127
"""
128128
if "pool_size" in kwargs:
129129
warnings.warn("pool_size no longer supported")
130-
self._is_legacy_client = bool(kwargs.get("_is_legacy_client", False))
131-
self.client_info = kwargs.get("_client_info", DEFAULT_CLIENT_INFO)
132-
self.client_info.client_library_version = self._client_version()
130+
if kwargs.get("_client_info"):
131+
self.client_info = kwargs["_client_info"]
132+
else:
133+
self.client_info = DEFAULT_CLIENT_INFO
134+
self.client_info.client_library_version = self._client_version()
133135
if type(client_options) is dict:
134136
client_options = client_options_lib.from_dict(client_options)
135137
client_options = cast(
@@ -169,6 +171,9 @@ def __init__(
169171
f"The configured universe domain ({self.universe_domain}) does not match the universe domain found in the credentials ({self._credentials.universe_domain}). If you haven't configured the universe domain explicitly, `googleapis.com` is the default."
170172
)
171173
self._is_closed = CrossSync._Sync_Impl.Event()
174+
self._disable_background_refresh = bool(
175+
kwargs.get("_disable_background_refresh", False)
176+
)
172177
self.transport = cast(TransportType, self._gapic_client.transport)
173178
self._active_instances: Set[_WarmedInstanceKey] = set()
174179
self._instance_owners: dict[_WarmedInstanceKey, Set[int]] = {}
@@ -224,11 +229,10 @@ def api_endpoint(self) -> str:
224229
str: The API endpoint used by the client instance."""
225230
return self._gapic_client.api_endpoint
226231

227-
def _client_version(self) -> str:
232+
@staticmethod
233+
def _client_version() -> str:
228234
"""Helper function to return the client version string for this client"""
229235
version_str = f"{google.cloud.bigtable.__version__}-data"
230-
if self._is_legacy_client:
231-
version_str += "-shim"
232236
return version_str
233237

234238
def _start_background_channel_refresh(self) -> None:
@@ -240,7 +244,7 @@ def _start_background_channel_refresh(self) -> None:
240244
not self._channel_refresh_task
241245
and (not self._emulator_host)
242246
and (not self._is_closed.is_set())
243-
and (not self._is_legacy_client)
247+
and (not self._disable_background_refresh)
244248
):
245249
CrossSync._Sync_Impl.verify_async_event_loop()
246250
self._channel_refresh_task = CrossSync._Sync_Impl.create_task(

tests/unit/data/_async/test_client.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ async def test_ctor_legacy_client(self):
181181
from google.api_core import client_options as client_options_lib
182182
from google.api_core.gapic_v1.client_info import ClientInfo
183183
from google.cloud.bigtable import __version__ as bigtable_version
184-
import copy
185184

186185
project = "project-id"
187186
credentials = AnonymousCredentials()
@@ -192,14 +191,17 @@ async def test_ctor_legacy_client(self):
192191
CrossSync.GapicClient, "__init__"
193192
) as bigtable_client_init:
194193
try:
195-
self._make_client(
194+
client = self._make_client(
196195
project=project,
197196
credentials=credentials,
198197
client_options=options_parsed,
199198
use_emulator=False,
200199
_client_info=client_info,
201-
_is_legacy_client=True,
200+
_disable_background_refresh=True,
202201
)
202+
203+
assert client._disable_background_refresh
204+
assert client.client_info is client_info
203205
except TypeError:
204206
pass
205207

@@ -209,21 +211,6 @@ async def test_ctor_legacy_client(self):
209211
assert kwargs["credentials"] == credentials
210212
assert kwargs["client_options"] == options_parsed
211213

212-
expected_client_info = copy.copy(client_info)
213-
expected_client_info.client_library_version = (
214-
f"{bigtable_version}-data-shim"
215-
if not CrossSync.is_async
216-
else f"{bigtable_version}-data-shim-async"
217-
)
218-
assert (
219-
kwargs["client_info"].to_user_agent()
220-
== expected_client_info.to_user_agent()
221-
)
222-
assert (
223-
kwargs["client_info"].to_grpc_metadata()
224-
== expected_client_info.to_grpc_metadata()
225-
)
226-
227214
@CrossSync.pytest
228215
async def test_ctor_dict_options(self):
229216
from google.api_core.client_options import ClientOptions
@@ -316,10 +303,10 @@ async def test__start_background_channel_refresh(self):
316303
await client.close()
317304

318305
@CrossSync.pytest
319-
async def test__start_background_channel_refresh_legacy_client(self):
306+
async def test__start_background_channel_refresh_disable_background_refresh(self):
320307
client = self._make_client(
321308
project="project-id",
322-
_is_legacy_client=True,
309+
_disable_background_refresh=True,
323310
)
324311
# should create background tasks for each channel
325312
with mock.patch.object(

tests/unit/data/_sync_autogen/test_client.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ def test_ctor_super_inits(self):
144144
def test_ctor_legacy_client(self):
145145
from google.api_core import client_options as client_options_lib
146146
from google.api_core.gapic_v1.client_info import ClientInfo
147-
from google.cloud.bigtable import __version__ as bigtable_version
148-
import copy
149147

150148
project = "project-id"
151149
credentials = AnonymousCredentials()
@@ -156,34 +154,22 @@ def test_ctor_legacy_client(self):
156154
CrossSync._Sync_Impl.GapicClient, "__init__"
157155
) as bigtable_client_init:
158156
try:
159-
self._make_client(
157+
client = self._make_client(
160158
project=project,
161159
credentials=credentials,
162160
client_options=options_parsed,
163161
use_emulator=False,
164162
_client_info=client_info,
165-
_is_legacy_client=True,
163+
_disable_background_refresh=True,
166164
)
165+
assert client._disable_background_refresh
166+
assert client.client_info is client_info
167167
except TypeError:
168168
pass
169169
assert bigtable_client_init.call_count == 1
170170
kwargs = bigtable_client_init.call_args[1]
171171
assert kwargs["credentials"] == credentials
172172
assert kwargs["client_options"] == options_parsed
173-
expected_client_info = copy.copy(client_info)
174-
expected_client_info.client_library_version = (
175-
f"{bigtable_version}-data-shim"
176-
if not CrossSync._Sync_Impl.is_async
177-
else f"{bigtable_version}-data-shim-async"
178-
)
179-
assert (
180-
kwargs["client_info"].to_user_agent()
181-
== expected_client_info.to_user_agent()
182-
)
183-
assert (
184-
kwargs["client_info"].to_grpc_metadata()
185-
== expected_client_info.to_grpc_metadata()
186-
)
187173

188174
def test_ctor_dict_options(self):
189175
from google.api_core.client_options import ClientOptions
@@ -257,8 +243,10 @@ def test__start_background_channel_refresh(self):
257243
assert ping_and_warm.call_count == 1
258244
client.close()
259245

260-
def test__start_background_channel_refresh_legacy_client(self):
261-
client = self._make_client(project="project-id", _is_legacy_client=True)
246+
def test__start_background_channel_refresh_disable_background_refresh(self):
247+
client = self._make_client(
248+
project="project-id", _disable_background_refresh=True
249+
)
262250
with mock.patch.object(
263251
client, "_ping_and_warm_instances", CrossSync._Sync_Impl.Mock()
264252
) as ping_and_warm:

tests/unit/v2_client/test_client.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -391,32 +391,44 @@ def test_client_project_path():
391391
assert client.project_path == project_name
392392

393393

394-
def test_client_data_client_not_initialized():
394+
def test_client_veneer_data_client_not_initialized():
395395
from google.cloud.bigtable.data import BigtableDataClient
396+
from google.cloud.bigtable import __version__
396397

397398
credentials = _make_credentials()
398399
client = _make_client(project=PROJECT, credentials=credentials)
399400

400-
veneer_data_client = client._veneer_data_client
401-
assert isinstance(veneer_data_client, BigtableDataClient)
402-
assert client._veneer_data_client is veneer_data_client
403-
assert client._veneer_data_client._is_legacy_client
401+
with mock.patch("copy.copy") as copy_mock:
402+
data_client = client._veneer_data_client
403+
404+
assert isinstance(data_client, BigtableDataClient)
405+
assert client._table_data_client is data_client
406+
407+
assert client._table_data_client._disable_background_refresh
408+
assert client._table_data_client.client_info.client_library_version == f"{__version__}-data-shim"
409+
copy_mock.assert_called_once_with(client._client_info)
404410

405411

406412
def test_client_veneer_data_client_not_initialized_w_client_info():
407413
from google.api_core.gapic_v1.client_info import ClientInfo
414+
from google.cloud.bigtable import __version__
408415

409416
credentials = _make_credentials()
410417
client_info = ClientInfo(gapic_version="1.2.3", user_agent="test-client-")
411418
client = _make_client(
412419
project=PROJECT, credentials=credentials, client_info=client_info
413420
)
414-
data_client = client._veneer_data_client
421+
422+
with mock.patch("copy.copy") as copy_mock:
423+
data_client = client._veneer_data_client
424+
425+
assert client._table_data_client is data_client
415426

416427
assert client._client_info is client_info
417-
assert client._veneer_data_client is data_client
418-
assert client._veneer_data_client.client_info is client_info
419-
assert client._veneer_data_client._is_legacy_client
428+
assert client._table_data_client.client_info is copy_mock.return_value
429+
assert client._table_data_client._disable_background_refresh
430+
assert client._table_data_client.client_info.client_library_version == f"{__version__}-data-shim"
431+
copy_mock.assert_called_once_with(client_info)
420432

421433

422434
def test_client_veneer_data_client_not_initialized_w_client_options():
@@ -429,9 +441,9 @@ def test_client_veneer_data_client_not_initialized_w_client_options():
429441
)
430442

431443
data_client = client._veneer_data_client
432-
assert client._veneer_data_client is data_client
433-
assert client._veneer_data_client._is_legacy_client
434-
assert client._veneer_data_client._gapic_client._client_options == client_options
444+
assert client._table_data_client is data_client
445+
assert client._table_data_client._disable_background_refresh
446+
assert client._table_data_client._gapic_client._client_options == client_options
435447

436448

437449
def test_client_veneer_data_client_initialized():
@@ -462,11 +474,28 @@ def test_client_data_gapic_client_not_initialized_w_client_info():
462474
project=PROJECT, credentials=credentials, client_info=client_info
463475
)
464476

465-
table_data_client = client.table_data_client
477+
mock_gapic_client = mock.MagicMock(spec=BigtableClient)
478+
mock_gapic_client.universe_domain = BigtableClient._DEFAULT_UNIVERSE
479+
480+
with mock.patch(
481+
"google.cloud.bigtable.data._sync_autogen.client.GapicClient",
482+
return_value=mock_gapic_client,
483+
) as gapic_mock:
484+
with mock.patch("copy.copy") as copy_mock:
485+
table_data_client = client.table_data_client
486+
466487
assert isinstance(table_data_client, BigtableClient)
467488
assert client._client_info is client_info
468489
assert client._table_data_client._gapic_client is table_data_client
469490

491+
copy_mock.assert_called_once_with(client._client_info)
492+
gapic_mock.assert_called_once_with(
493+
client_info=copy_mock.return_value,
494+
credentials=mock.ANY,
495+
transport=mock.ANY,
496+
client_options=mock.ANY,
497+
)
498+
470499

471500
def test_client_data_gapic_client_not_initialized_w_client_options():
472501
from google.api_core.client_options import ClientOptions
@@ -484,13 +513,13 @@ def test_client_data_gapic_client_not_initialized_w_client_options():
484513
with mock.patch(
485514
"google.cloud.bigtable.data._sync_autogen.client.GapicClient",
486515
return_value=mock_gapic_client,
487-
) as mocked:
516+
) as gapic_mock:
488517
table_data_client = client.table_data_client
489518

490519
assert client._table_data_client._gapic_client is table_data_client
491520

492-
mocked.assert_called_once_with(
493-
client_info=client._client_info,
521+
gapic_mock.assert_called_once_with(
522+
client_info=mock.ANY,
494523
credentials=mock.ANY,
495524
transport=mock.ANY,
496525
client_options=client_options,

0 commit comments

Comments
 (0)