Skip to content

Commit 92008e2

Browse files
daniel-sanchegkevinzhenggcf-owl-bot[bot]
authored
feat(bigtable): initialize internal data client in classic client (#18080)
Migrating over @gkevinzheng PR from bigtable monorepo googleapis/python-bigtable#1238 Original description: > Changes: > - Added a new property ~`new_table_data_client`~ `_veneer_data_client` in the classic client for the new table data client > - Added two hidden kwargs (`client_info` and `disable_background_channel_refresh`) to the new data client constructor to support the classic client. > > Fixes #15298 Note to reviewers: This PR has already been reviewed and merged to a staging branch, with the intention of doing a single merge to main. We are now planning to slowly rollout these changes back to the main branch. Minimal re-review should be necessary --------- Co-authored-by: Kevin Zheng <147537668+gkevinzheng@users.noreply.github.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent a3947d0 commit 92008e2

18 files changed

Lines changed: 470 additions & 132 deletions

File tree

packages/google-cloud-bigtable/google/cloud/bigtable/client.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
:class:`~google.cloud.bigtable.row.Row` (and all the cells in the row)
2929
"""
3030

31+
import copy
3132
import os
3233
import warnings
3334

@@ -37,7 +38,7 @@
3738
from google.cloud.client import ClientWithProject # type: ignore
3839
from google.cloud.environment_vars import BIGTABLE_EMULATOR # type: ignore
3940

40-
from google.cloud import bigtable, bigtable_admin_v2, bigtable_v2
41+
from google.cloud import bigtable, bigtable_admin_v2
4142
from google.cloud.bigtable.cluster import _CLUSTER_NAME_RE, Cluster
4243
from google.cloud.bigtable.instance import Instance
4344
from google.cloud.bigtable_admin_v2.services.bigtable_instance_admin.transports import (
@@ -47,7 +48,6 @@
4748
BigtableTableAdminGrpcTransport,
4849
)
4950
from google.cloud.bigtable_admin_v2.types import instance
50-
from google.cloud.bigtable_v2.services.bigtable.transports import BigtableGrpcTransport
5151

5252
INSTANCE_TYPE_PRODUCTION = instance.Instance.Type.PRODUCTION
5353
INSTANCE_TYPE_DEVELOPMENT = instance.Instance.Type.DEVELOPMENT
@@ -60,14 +60,15 @@
6060
READ_ONLY_SCOPE = "https://www.googleapis.com/auth/bigtable.data.readonly"
6161
"""Scope for reading table data."""
6262

63-
_DEFAULT_BIGTABLE_EMULATOR_CLIENT = "google-cloud-bigtable-emulator"
6463
_GRPC_CHANNEL_OPTIONS = (
6564
("grpc.max_send_message_length", -1),
6665
("grpc.max_receive_message_length", -1),
6766
("grpc.keepalive_time_ms", 30000),
6867
("grpc.keepalive_timeout_ms", 10000),
6968
)
7069

70+
_DEFAULT_BIGTABLE_EMULATOR_CLIENT = "google-cloud-bigtable-emulator"
71+
7172

7273
def _create_gapic_client(client_class, client_options=None, transport=None):
7374
def inner(self):
@@ -284,18 +285,7 @@ def table_data_client(self):
284285
:rtype: :class:`.bigtable_v2.BigtableClient`
285286
:returns: A BigtableClient object.
286287
"""
287-
if self._table_data_client is None:
288-
transport = self._create_gapic_client_channel(
289-
bigtable_v2.BigtableClient,
290-
BigtableGrpcTransport,
291-
)
292-
klass = _create_gapic_client(
293-
bigtable_v2.BigtableClient,
294-
client_options=self._client_options,
295-
transport=transport,
296-
)
297-
self._table_data_client = klass(self)
298-
return self._table_data_client
288+
return self._veneer_data_client._gapic_client
299289

300290
@property
301291
def table_admin_client(self):
@@ -363,6 +353,23 @@ def instance_admin_client(self):
363353
self._instance_admin_client = klass(self)
364354
return self._instance_admin_client
365355

356+
@property
357+
def _veneer_data_client(self):
358+
"""Getter for the new Data Table API."""
359+
if self._table_data_client is None:
360+
from google.cloud.bigtable.data import BigtableDataClient
361+
362+
client_info = copy.copy(self._client_info)
363+
client_info.client_library_version = f"{bigtable.__version__}-data-shim"
364+
self._table_data_client = BigtableDataClient(
365+
project=self.project,
366+
credentials=self._credentials,
367+
client_options=self._client_options,
368+
_client_info=client_info,
369+
_disable_background_refresh=True,
370+
)
371+
return self._table_data_client
372+
366373
def instance(self, instance_id, display_name=None, instance_type=None, labels=None):
367374
"""Factory to create a instance associated with this client.
368375

packages/google-cloud-bigtable/google/cloud/bigtable/data/_async/_mutate_rows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import google.cloud.bigtable_v2.types.bigtable as types_pb
2424
from google.cloud.bigtable.data._cross_sync import CrossSync
2525
from google.cloud.bigtable.data._helpers import _attempt_timeout_generator
26-
from google.cloud.bigtable.data._metrics import tracked_retry
26+
from google.cloud.bigtable.data._metrics.tracked_retry import tracked_retry
2727

2828
# mutate_rows requests are limited to this number of mutations
2929
from google.cloud.bigtable.data.mutations import (

packages/google-cloud-bigtable/google/cloud/bigtable/data/_async/_read_rows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from google.cloud.bigtable.data._helpers import (
2626
_attempt_timeout_generator,
2727
)
28-
from google.cloud.bigtable.data._metrics import tracked_retry
28+
from google.cloud.bigtable.data._metrics.tracked_retry import tracked_retry
2929
from google.cloud.bigtable.data.exceptions import (
3030
InvalidChunk,
3131
_ResetRow,

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
from google.protobuf.message import Message
5151
from grpc import Channel
5252

53-
from google.cloud.bigtable.client import _DEFAULT_BIGTABLE_EMULATOR_CLIENT
5453
from google.cloud.bigtable.data._cross_sync import CrossSync
5554
from google.cloud.bigtable.data._helpers import (
5655
_CONCURRENCY_LIMIT,
56+
_DEFAULT_BIGTABLE_EMULATOR_CLIENT,
5757
TABLE_DEFAULT,
5858
_align_timeouts,
5959
_attempt_timeout_generator,
@@ -68,8 +68,8 @@
6868
ActiveOperationMetric,
6969
BigtableClientSideMetricsController,
7070
OperationType,
71-
tracked_retry,
7271
)
72+
from google.cloud.bigtable.data._metrics.tracked_retry import tracked_retry
7373
from google.cloud.bigtable.data._metrics.handlers._base import MetricsHandler
7474
from google.cloud.bigtable.data._metrics.handlers.gcp_exporter import (
7575
BigtableMetricsExporter,
@@ -218,9 +218,15 @@ def __init__(
218218
"""
219219
if "pool_size" in kwargs:
220220
warnings.warn("pool_size no longer supported")
221-
# set up client info headers for veneer library
222-
self.client_info = DEFAULT_CLIENT_INFO
223-
self.client_info.client_library_version = self._client_version()
221+
222+
# set up client info headers for veneer library. _client_info is for internal use only,
223+
# for the legacy client shim.
224+
if kwargs.get("_client_info"):
225+
self.client_info = kwargs["_client_info"]
226+
else:
227+
self.client_info = DEFAULT_CLIENT_INFO
228+
self.client_info.client_library_version = self._client_version()
229+
224230
# parse client options
225231
if type(client_options) is dict:
226232
client_options = client_options_lib.from_dict(client_options)
@@ -271,6 +277,10 @@ def __init__(
271277
"is the default."
272278
)
273279
self._is_closed = CrossSync.Event()
280+
# Private argument, for internal use only
281+
self._disable_background_refresh = bool(
282+
kwargs.get("_disable_background_refresh", False)
283+
)
274284
handlers: list[MetricsHandler] = []
275285
if self._emulator_host is None:
276286
try:
@@ -402,6 +412,7 @@ def _start_background_channel_refresh(self) -> None:
402412
not self._channel_refresh_task
403413
and not self._emulator_host
404414
and not self._is_closed.is_set()
415+
and not self._disable_background_refresh
405416
):
406417
# raise error if not in an event loop in async client
407418
CrossSync.verify_async_event_loop()

packages/google-cloud-bigtable/google/cloud/bigtable/data/_helpers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
# used by read_rows_sharded to limit how many requests are attempted in parallel
4848
_CONCURRENCY_LIMIT = 10
4949

50+
# used by every data client as a default project name for testing on Bigtable emulator.
51+
_DEFAULT_BIGTABLE_EMULATOR_CLIENT = "google-cloud-bigtable-emulator"
52+
5053
# used to identify an active bigtable resource that needs to be warmed through PingAndWarm
5154
# each instance/app_profile_id pair needs to be individually tracked
5255
_WarmedInstanceKey = namedtuple(

packages/google-cloud-bigtable/google/cloud/bigtable/data/_metrics/handlers/opentelemetry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import socket
1818
import uuid
1919

20-
from google.cloud.bigtable import __version__ as bigtable_version
20+
from google.cloud.bigtable.gapic_version import __version__ as bigtable_version
2121
from google.cloud.bigtable.data._metrics.data_model import (
2222
DEFAULT_CLUSTER_ID,
2323
DEFAULT_ZONE,

packages/google-cloud-bigtable/google/cloud/bigtable/data/_metrics/tracked_retry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
from grpc import StatusCode
3030

3131
from google.cloud.bigtable.data._helpers import _retry_exception_factory
32-
from google.cloud.bigtable.data._metrics import ActiveOperationMetric, OperationState
32+
from google.cloud.bigtable.data._metrics.data_model import (
33+
ActiveOperationMetric,
34+
OperationState,
35+
)
3336
from google.cloud.bigtable.data.exceptions import _MutateRowsIncomplete
3437

3538
T = TypeVar("T")

packages/google-cloud-bigtable/google/cloud/bigtable/data/_sync_autogen/_mutate_rows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import google.cloud.bigtable_v2.types.bigtable as types_pb
2727
from google.cloud.bigtable.data._cross_sync import CrossSync
2828
from google.cloud.bigtable.data._helpers import _attempt_timeout_generator
29-
from google.cloud.bigtable.data._metrics import tracked_retry
29+
from google.cloud.bigtable.data._metrics.tracked_retry import tracked_retry
3030
from google.cloud.bigtable.data.mutations import (
3131
_MUTATE_ROWS_REQUEST_MUTATION_LIMIT,
3232
_EntryWithProto,

packages/google-cloud-bigtable/google/cloud/bigtable/data/_sync_autogen/_read_rows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from google.cloud.bigtable.data._cross_sync import CrossSync
2828
from google.cloud.bigtable.data._helpers import _attempt_timeout_generator
29-
from google.cloud.bigtable.data._metrics import tracked_retry
29+
from google.cloud.bigtable.data._metrics.tracked_retry import tracked_retry
3030
from google.cloud.bigtable.data.exceptions import (
3131
InvalidChunk,
3232
_ResetRow,

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
from google.protobuf.message import Message
4545
from grpc import Channel, insecure_channel, intercept_channel
4646

47-
from google.cloud.bigtable.client import _DEFAULT_BIGTABLE_EMULATOR_CLIENT
4847
from google.cloud.bigtable.data._cross_sync import CrossSync
4948
from google.cloud.bigtable.data._helpers import (
5049
_CONCURRENCY_LIMIT,
50+
_DEFAULT_BIGTABLE_EMULATOR_CLIENT,
5151
TABLE_DEFAULT,
5252
_align_timeouts,
5353
_attempt_timeout_generator,
@@ -62,8 +62,8 @@
6262
ActiveOperationMetric,
6363
BigtableClientSideMetricsController,
6464
OperationType,
65-
tracked_retry,
6665
)
66+
from google.cloud.bigtable.data._metrics.tracked_retry import tracked_retry
6767
from google.cloud.bigtable.data._metrics.handlers._base import MetricsHandler
6868
from google.cloud.bigtable.data._metrics.handlers.gcp_exporter import (
6969
BigtableMetricsExporter,
@@ -158,8 +158,11 @@ def __init__(
158158
"""
159159
if "pool_size" in kwargs:
160160
warnings.warn("pool_size no longer supported")
161-
self.client_info = DEFAULT_CLIENT_INFO
162-
self.client_info.client_library_version = self._client_version()
161+
if kwargs.get("_client_info"):
162+
self.client_info = kwargs["_client_info"]
163+
else:
164+
self.client_info = DEFAULT_CLIENT_INFO
165+
self.client_info.client_library_version = self._client_version()
163166
if type(client_options) is dict:
164167
client_options = client_options_lib.from_dict(client_options)
165168
client_options = cast(
@@ -201,6 +204,9 @@ def __init__(
201204
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."
202205
)
203206
self._is_closed = CrossSync._Sync_Impl.Event()
207+
self._disable_background_refresh = bool(
208+
kwargs.get("_disable_background_refresh", False)
209+
)
204210
handlers: list[MetricsHandler] = []
205211
if self._emulator_host is None:
206212
try:
@@ -297,6 +303,7 @@ def _start_background_channel_refresh(self) -> None:
297303
not self._channel_refresh_task
298304
and (not self._emulator_host)
299305
and (not self._is_closed.is_set())
306+
and (not self._disable_background_refresh)
300307
):
301308
CrossSync._Sync_Impl.verify_async_event_loop()
302309
self._channel_refresh_task = CrossSync._Sync_Impl.create_task(

0 commit comments

Comments
 (0)