Skip to content

Commit 2ea9b53

Browse files
chore: remove custom routing metadata (#1036)
* remove custom _make_metadata * remove gapic customizations * fixed lint
1 parent 76b03e2 commit 2ea9b53

File tree

11 files changed

+27
-171
lines changed

11 files changed

+27
-171
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from google.api_core import retry as retries
2323
import google.cloud.bigtable_v2.types.bigtable as types_pb
2424
import google.cloud.bigtable.data.exceptions as bt_exceptions
25-
from google.cloud.bigtable.data._helpers import _make_metadata
2625
from google.cloud.bigtable.data._helpers import _attempt_timeout_generator
2726
from google.cloud.bigtable.data._helpers import _retry_exception_factory
2827

@@ -84,14 +83,10 @@ def __init__(
8483
f"all entries. Found {total_mutations}."
8584
)
8685
# create partial function to pass to trigger rpc call
87-
metadata = _make_metadata(
88-
table.table_name, table.app_profile_id, instance_name=None
89-
)
9086
self._gapic_fn = functools.partial(
9187
gapic_client.mutate_rows,
9288
table_name=table.table_name,
9389
app_profile_id=table.app_profile_id,
94-
metadata=metadata,
9590
retry=None,
9691
)
9792
# create predicate for determining which errors are retryable

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from google.cloud.bigtable.data.exceptions import InvalidChunk
3434
from google.cloud.bigtable.data.exceptions import _RowSetComplete
3535
from google.cloud.bigtable.data._helpers import _attempt_timeout_generator
36-
from google.cloud.bigtable.data._helpers import _make_metadata
3736
from google.cloud.bigtable.data._helpers import _retry_exception_factory
3837

3938
from google.api_core import retry as retries
@@ -74,7 +73,6 @@ class _ReadRowsOperationAsync:
7473
"request",
7574
"table",
7675
"_predicate",
77-
"_metadata",
7876
"_last_yielded_row_key",
7977
"_remaining_count",
8078
)
@@ -101,9 +99,6 @@ def __init__(
10199
self.request = query._to_pb(table)
102100
self.table = table
103101
self._predicate = retries.if_exception_type(*retryable_exceptions)
104-
self._metadata = _make_metadata(
105-
table.table_name, table.app_profile_id, instance_name=None
106-
)
107102
self._last_yielded_row_key: bytes | None = None
108103
self._remaining_count: int | None = self.request.rows_limit or None
109104

@@ -152,7 +147,6 @@ def _read_rows_attempt(self) -> AsyncGenerator[Row, None]:
152147
gapic_stream = self.table.client._gapic_client.read_rows(
153148
self.request,
154149
timeout=next(self.attempt_timeout_gen),
155-
metadata=self._metadata,
156150
retry=None,
157151
)
158152
chunked_stream = self.chunk_stream(gapic_stream)

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

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
_get_error_type,
6161
_get_retryable_errors,
6262
_get_timeouts,
63-
_make_metadata,
6463
_retry_exception_factory,
6564
_validate_timeouts,
6665
_WarmedInstanceKey,
@@ -262,19 +261,18 @@ async def _ping_and_warm_instances(
262261
request_serializer=PingAndWarmRequest.serialize,
263262
)
264263
# prepare list of coroutines to run
265-
tasks = [
266-
ping_rpc(
267-
request={"name": instance_name, "app_profile_id": app_profile_id},
268-
metadata=[
269-
(
270-
"x-goog-request-params",
271-
f"name={instance_name}&app_profile_id={app_profile_id}",
272-
)
273-
],
274-
wait_for_ready=True,
264+
tasks = []
265+
for instance_name, table_name, app_profile_id in instance_list:
266+
metadata_str = f"name={instance_name}"
267+
if app_profile_id is not None:
268+
metadata_str = f"{metadata_str}&app_profile_id={app_profile_id}"
269+
tasks.append(
270+
ping_rpc(
271+
request={"name": instance_name, "app_profile_id": app_profile_id},
272+
metadata=[("x-goog-request-params", metadata_str)],
273+
wait_for_ready=True,
274+
)
275275
)
276-
for (instance_name, table_name, app_profile_id) in instance_list
277-
]
278276
# execute coroutines in parallel
279277
result_list = await asyncio.gather(*tasks, return_exceptions=True)
280278
# return None in place of empty successful responses
@@ -508,24 +506,14 @@ async def execute_query(
508506
"proto_format": {},
509507
}
510508

511-
# app_profile_id should be set to an empty string for ExecuteQueryRequest only
512-
app_profile_id_for_metadata = app_profile_id or ""
513-
514-
req_metadata = _make_metadata(
515-
table_name=None,
516-
app_profile_id=app_profile_id_for_metadata,
517-
instance_name=instance_name,
518-
)
519-
520509
return ExecuteQueryIteratorAsync(
521510
self,
522511
instance_id,
523512
app_profile_id,
524513
request_body,
525514
attempt_timeout,
526515
operation_timeout,
527-
req_metadata,
528-
retryable_excs,
516+
retryable_excs=retryable_excs,
529517
)
530518

531519
async def __aenter__(self):
@@ -1005,16 +993,11 @@ async def sample_row_keys(
1005993
sleep_generator = retries.exponential_sleep_generator(0.01, 2, 60)
1006994

1007995
# prepare request
1008-
metadata = _make_metadata(
1009-
self.table_name, self.app_profile_id, instance_name=None
1010-
)
1011-
1012996
async def execute_rpc():
1013997
results = await self.client._gapic_client.sample_row_keys(
1014998
table_name=self.table_name,
1015999
app_profile_id=self.app_profile_id,
10161000
timeout=next(attempt_timeout_gen),
1017-
metadata=metadata,
10181001
retry=None,
10191002
)
10201003
return [(s.row_key, s.offset_bytes) async for s in results]
@@ -1143,9 +1126,6 @@ async def mutate_row(
11431126
table_name=self.table_name,
11441127
app_profile_id=self.app_profile_id,
11451128
timeout=attempt_timeout,
1146-
metadata=_make_metadata(
1147-
self.table_name, self.app_profile_id, instance_name=None
1148-
),
11491129
retry=None,
11501130
)
11511131
return await retries.retry_target_async(
@@ -1263,17 +1243,13 @@ async def check_and_mutate_row(
12631243
):
12641244
false_case_mutations = [false_case_mutations]
12651245
false_case_list = [m._to_pb() for m in false_case_mutations or []]
1266-
metadata = _make_metadata(
1267-
self.table_name, self.app_profile_id, instance_name=None
1268-
)
12691246
result = await self.client._gapic_client.check_and_mutate_row(
12701247
true_mutations=true_case_list,
12711248
false_mutations=false_case_list,
12721249
predicate_filter=predicate._to_pb() if predicate is not None else None,
12731250
row_key=row_key.encode("utf-8") if isinstance(row_key, str) else row_key,
12741251
table_name=self.table_name,
12751252
app_profile_id=self.app_profile_id,
1276-
metadata=metadata,
12771253
timeout=operation_timeout,
12781254
retry=None,
12791255
)
@@ -1316,15 +1292,11 @@ async def read_modify_write_row(
13161292
rules = [rules]
13171293
if not rules:
13181294
raise ValueError("rules must contain at least one item")
1319-
metadata = _make_metadata(
1320-
self.table_name, self.app_profile_id, instance_name=None
1321-
)
13221295
result = await self.client._gapic_client.read_modify_write_row(
13231296
rules=[rule._to_pb() for rule in rules],
13241297
row_key=row_key.encode("utf-8") if isinstance(row_key, str) else row_key,
13251298
table_name=self.table_name,
13261299
app_profile_id=self.app_profile_id,
1327-
metadata=metadata,
13281300
timeout=operation_timeout,
13291301
retry=None,
13301302
)

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,6 @@ class TABLE_DEFAULT(enum.Enum):
5959
MUTATE_ROWS = "MUTATE_ROWS_DEFAULT"
6060

6161

62-
def _make_metadata(
63-
table_name: str | None, app_profile_id: str | None, instance_name: str | None
64-
) -> list[tuple[str, str]]:
65-
"""
66-
Create properly formatted gRPC metadata for requests.
67-
"""
68-
params = []
69-
70-
if table_name is not None and instance_name is not None:
71-
raise ValueError("metadata can't contain both instance_name and table_name")
72-
73-
if table_name is not None:
74-
params.append(f"table_name={table_name}")
75-
if instance_name is not None:
76-
params.append(f"name={instance_name}")
77-
if app_profile_id is not None:
78-
params.append(f"app_profile_id={app_profile_id}")
79-
if len(params) == 0:
80-
raise ValueError(
81-
"At least one of table_name and app_profile_id should be not None."
82-
)
83-
params_str = "&".join(params)
84-
return [("x-goog-request-params", params_str)]
85-
86-
8762
def _attempt_timeout_generator(
8863
per_request_timeout: float | None, operation_timeout: float
8964
):

packages/google-cloud-bigtable/google/cloud/bigtable/data/execute_query/_async/execute_query_iterator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
Any,
2020
AsyncIterator,
2121
Dict,
22-
List,
2322
Optional,
2423
Sequence,
2524
Tuple,
@@ -83,8 +82,8 @@ def __init__(
8382
request_body: Dict[str, Any],
8483
attempt_timeout: float | None,
8584
operation_timeout: float,
86-
req_metadata: Sequence[Tuple[str, str]],
87-
retryable_excs: List[type[Exception]],
85+
req_metadata: Sequence[Tuple[str, str]] = (),
86+
retryable_excs: Sequence[type[Exception]] = (),
8887
) -> None:
8988
self._table_name = None
9089
self._app_profile_id = app_profile_id
@@ -99,6 +98,7 @@ def __init__(
9998
self._attempt_timeout_gen = _attempt_timeout_generator(
10099
attempt_timeout, operation_timeout
101100
)
101+
retryable_excs = retryable_excs or []
102102
self._async_stream = retries.retry_target_stream_async(
103103
self._make_request_with_resume_token,
104104
retries.if_exception_type(*retryable_excs),

packages/google-cloud-bigtable/google/cloud/bigtable_v2/services/bigtable/async_client.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,13 +1286,11 @@ def generate_initial_change_stream_partitions(
12861286

12871287
# Certain fields should be provided within the metadata header;
12881288
# add these here.
1289-
metadata = tuple(metadata)
1290-
if all(m[0] != gapic_v1.routing_header.ROUTING_METADATA_KEY for m in metadata):
1291-
metadata += (
1292-
gapic_v1.routing_header.to_grpc_metadata(
1293-
(("table_name", request.table_name),)
1294-
),
1295-
)
1289+
metadata = tuple(metadata) + (
1290+
gapic_v1.routing_header.to_grpc_metadata(
1291+
(("table_name", request.table_name),)
1292+
),
1293+
)
12961294

12971295
# Validate the universe domain.
12981296
self._client._validate_universe_domain()
@@ -1390,13 +1388,11 @@ def read_change_stream(
13901388

13911389
# Certain fields should be provided within the metadata header;
13921390
# add these here.
1393-
metadata = tuple(metadata)
1394-
if all(m[0] != gapic_v1.routing_header.ROUTING_METADATA_KEY for m in metadata):
1395-
metadata += (
1396-
gapic_v1.routing_header.to_grpc_metadata(
1397-
(("table_name", request.table_name),)
1398-
),
1399-
)
1391+
metadata = tuple(metadata) + (
1392+
gapic_v1.routing_header.to_grpc_metadata(
1393+
(("table_name", request.table_name),)
1394+
),
1395+
)
14001396

14011397
# Validate the universe domain.
14021398
self._client._validate_universe_domain()

packages/google-cloud-bigtable/owlbot.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,6 @@ def insert(file, before_line, insert_line, after_line, escape=None):
143143
escape='"'
144144
)
145145

146-
# ----------------------------------------------------------------------------
147-
# Patch duplicate routing header: https://github.com/googleapis/gapic-generator-python/issues/2078
148-
# ----------------------------------------------------------------------------
149-
for file in ["async_client.py"]:
150-
s.replace(
151-
f"google/cloud/bigtable_v2/services/bigtable/{file}",
152-
"metadata \= tuple\(metadata\) \+ \(",
153-
"""metadata = tuple(metadata)
154-
if all(m[0] != gapic_v1.routing_header.ROUTING_METADATA_KEY for m in metadata):
155-
metadata += ("""
156-
)
157-
158146
# ----------------------------------------------------------------------------
159147
# Samples templates
160148
# ----------------------------------------------------------------------------

packages/google-cloud-bigtable/tests/unit/data/_async/test__mutate_rows.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,10 @@ def test_ctor(self):
101101
assert client.mutate_rows.call_count == 1
102102
# gapic_fn should call with table details
103103
inner_kwargs = client.mutate_rows.call_args[1]
104-
assert len(inner_kwargs) == 4
104+
assert len(inner_kwargs) == 3
105105
assert inner_kwargs["table_name"] == table.table_name
106106
assert inner_kwargs["app_profile_id"] == table.app_profile_id
107107
assert inner_kwargs["retry"] is None
108-
metadata = inner_kwargs["metadata"]
109-
assert len(metadata) == 1
110-
assert metadata[0][0] == "x-goog-request-params"
111-
assert str(table.table_name) in metadata[0][1]
112-
assert str(table.app_profile_id) in metadata[0][1]
113108
# entries should be passed down
114109
entries_w_pb = [_EntryWithProto(e, e._to_pb()) for e in entries]
115110
assert instance.mutations == entries_w_pb

packages/google-cloud-bigtable/tests/unit/data/_async/test__read_rows.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ def test_ctor(self):
7878
assert instance._remaining_count == row_limit
7979
assert instance.operation_timeout == expected_operation_timeout
8080
assert client.read_rows.call_count == 0
81-
assert instance._metadata == [
82-
(
83-
"x-goog-request-params",
84-
"table_name=test_table&app_profile_id=test_profile",
85-
)
86-
]
8781
assert instance.request.table_name == table.table_name
8882
assert instance.request.app_profile_id == table.app_profile_id
8983
assert instance.request.rows_limit == row_limit

packages/google-cloud-bigtable/tests/unit/data/_async/test_client.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,11 +2176,10 @@ async def test_sample_row_keys_gapic_params(self):
21762176
await table.sample_row_keys(attempt_timeout=expected_timeout)
21772177
args, kwargs = sample_row_keys.call_args
21782178
assert len(args) == 0
2179-
assert len(kwargs) == 5
2179+
assert len(kwargs) == 4
21802180
assert kwargs["timeout"] == expected_timeout
21812181
assert kwargs["app_profile_id"] == expected_profile
21822182
assert kwargs["table_name"] == table.table_name
2183-
assert kwargs["metadata"] is not None
21842183
assert kwargs["retry"] is None
21852184

21862185
@pytest.mark.parametrize(
@@ -2375,30 +2374,6 @@ async def test_mutate_row_non_retryable_errors(self, non_retryable_exception):
23752374
"row_key", mutation, operation_timeout=0.2
23762375
)
23772376

2378-
@pytest.mark.parametrize("include_app_profile", [True, False])
2379-
@pytest.mark.asyncio
2380-
async def test_mutate_row_metadata(self, include_app_profile):
2381-
"""request should attach metadata headers"""
2382-
profile = "profile" if include_app_profile else None
2383-
async with _make_client() as client:
2384-
async with client.get_table("i", "t", app_profile_id=profile) as table:
2385-
with mock.patch.object(
2386-
client._gapic_client, "mutate_row", AsyncMock()
2387-
) as read_rows:
2388-
await table.mutate_row("rk", mock.Mock())
2389-
kwargs = read_rows.call_args_list[0].kwargs
2390-
metadata = kwargs["metadata"]
2391-
goog_metadata = None
2392-
for key, value in metadata:
2393-
if key == "x-goog-request-params":
2394-
goog_metadata = value
2395-
assert goog_metadata is not None, "x-goog-request-params not found"
2396-
assert "table_name=" + table.table_name in goog_metadata
2397-
if include_app_profile:
2398-
assert "app_profile_id=profile" in goog_metadata
2399-
else:
2400-
assert "app_profile_id=" not in goog_metadata
2401-
24022377
@pytest.mark.parametrize("mutations", [[], None])
24032378
@pytest.mark.asyncio
24042379
async def test_mutate_row_no_mutations(self, mutations):

0 commit comments

Comments
 (0)