Skip to content

Commit 34fe8a3

Browse files
yoshi-automationtseaver
authored andcommitted
Add 'batch_create_sessions' method to generated client (via synth). (#9087)
1 parent ca275d8 commit 34fe8a3

File tree

8 files changed

+477
-98
lines changed

8 files changed

+477
-98
lines changed

spanner/google/cloud/spanner_v1/gapic/spanner_client.py

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,92 @@ def create_session(
307307
request, retry=retry, timeout=timeout, metadata=metadata
308308
)
309309

310+
def batch_create_sessions(
311+
self,
312+
database,
313+
session_template=None,
314+
session_count=None,
315+
retry=google.api_core.gapic_v1.method.DEFAULT,
316+
timeout=google.api_core.gapic_v1.method.DEFAULT,
317+
metadata=None,
318+
):
319+
"""
320+
Creates multiple new sessions.
321+
322+
This API can be used to initialize a session cache on the clients.
323+
See https://goo.gl/TgSFN2 for best practices on session cache management.
324+
325+
Example:
326+
>>> from google.cloud import spanner_v1
327+
>>>
328+
>>> client = spanner_v1.SpannerClient()
329+
>>>
330+
>>> database = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]')
331+
>>>
332+
>>> response = client.batch_create_sessions(database)
333+
334+
Args:
335+
database (str): Required. The database in which the new sessions are created.
336+
session_template (Union[dict, ~google.cloud.spanner_v1.types.Session]): Parameters to be applied to each created session.
337+
338+
If a dict is provided, it must be of the same form as the protobuf
339+
message :class:`~google.cloud.spanner_v1.types.Session`
340+
session_count (int): Required. The number of sessions to be created in this batch call. The
341+
API may return fewer than the requested number of sessions. If a
342+
specific number of sessions are desired, the client can make additional
343+
calls to BatchCreateSessions (adjusting ``session_count`` as necessary).
344+
retry (Optional[google.api_core.retry.Retry]): A retry object used
345+
to retry requests. If ``None`` is specified, requests will
346+
be retried using a default configuration.
347+
timeout (Optional[float]): The amount of time, in seconds, to wait
348+
for the request to complete. Note that if ``retry`` is
349+
specified, the timeout applies to each individual attempt.
350+
metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
351+
that is provided to the method.
352+
353+
Returns:
354+
A :class:`~google.cloud.spanner_v1.types.BatchCreateSessionsResponse` instance.
355+
356+
Raises:
357+
google.api_core.exceptions.GoogleAPICallError: If the request
358+
failed for any reason.
359+
google.api_core.exceptions.RetryError: If the request failed due
360+
to a retryable error and retry attempts failed.
361+
ValueError: If the parameters are invalid.
362+
"""
363+
# Wrap the transport method to add retry and timeout logic.
364+
if "batch_create_sessions" not in self._inner_api_calls:
365+
self._inner_api_calls[
366+
"batch_create_sessions"
367+
] = google.api_core.gapic_v1.method.wrap_method(
368+
self.transport.batch_create_sessions,
369+
default_retry=self._method_configs["BatchCreateSessions"].retry,
370+
default_timeout=self._method_configs["BatchCreateSessions"].timeout,
371+
client_info=self._client_info,
372+
)
373+
374+
request = spanner_pb2.BatchCreateSessionsRequest(
375+
database=database,
376+
session_template=session_template,
377+
session_count=session_count,
378+
)
379+
if metadata is None:
380+
metadata = []
381+
metadata = list(metadata)
382+
try:
383+
routing_header = [("database", database)]
384+
except AttributeError:
385+
pass
386+
else:
387+
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
388+
routing_header
389+
)
390+
metadata.append(routing_metadata)
391+
392+
return self._inner_api_calls["batch_create_sessions"](
393+
request, retry=retry, timeout=timeout, metadata=metadata
394+
)
395+
310396
def get_session(
311397
self,
312398
name,
@@ -602,10 +688,7 @@ def execute_sql(
602688
Args:
603689
session (str): Required. The session in which the SQL query should be performed.
604690
sql (str): Required. The SQL string.
605-
transaction (Union[dict, ~google.cloud.spanner_v1.types.TransactionSelector]): The transaction to use. If none is provided, the default is a
606-
temporary read-only transaction with strong concurrency.
607-
608-
The transaction to use.
691+
transaction (Union[dict, ~google.cloud.spanner_v1.types.TransactionSelector]): The transaction to use.
609692
610693
For queries, if none is provided, the default is a temporary read-only
611694
transaction with strong concurrency.
@@ -764,10 +847,7 @@ def execute_streaming_sql(
764847
Args:
765848
session (str): Required. The session in which the SQL query should be performed.
766849
sql (str): Required. The SQL string.
767-
transaction (Union[dict, ~google.cloud.spanner_v1.types.TransactionSelector]): The transaction to use. If none is provided, the default is a
768-
temporary read-only transaction with strong concurrency.
769-
770-
The transaction to use.
850+
transaction (Union[dict, ~google.cloud.spanner_v1.types.TransactionSelector]): The transaction to use.
771851
772852
For queries, if none is provided, the default is a temporary read-only
773853
transaction with strong concurrency.

spanner/google/cloud/spanner_v1/gapic/spanner_client_config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
"retry_codes_name": "idempotent",
4242
"retry_params_name": "default",
4343
},
44+
"BatchCreateSessions": {
45+
"timeout_millis": 60000,
46+
"retry_codes_name": "idempotent",
47+
"retry_params_name": "default",
48+
},
4449
"GetSession": {
4550
"timeout_millis": 30000,
4651
"retry_codes_name": "idempotent",

spanner/google/cloud/spanner_v1/gapic/transports/spanner_grpc_transport.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@ def create_session(self):
147147
"""
148148
return self._stubs["spanner_stub"].CreateSession
149149

150+
@property
151+
def batch_create_sessions(self):
152+
"""Return the gRPC stub for :meth:`SpannerClient.batch_create_sessions`.
153+
154+
Creates multiple new sessions.
155+
156+
This API can be used to initialize a session cache on the clients.
157+
See https://goo.gl/TgSFN2 for best practices on session cache management.
158+
159+
Returns:
160+
Callable: A callable which accepts the appropriate
161+
deserialized request object and returns a
162+
deserialized response object.
163+
"""
164+
return self._stubs["spanner_stub"].BatchCreateSessions
165+
150166
@property
151167
def get_session(self):
152168
"""Return the gRPC stub for :meth:`SpannerClient.get_session`.

spanner/google/cloud/spanner_v1/proto/spanner.proto

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Google LLC.
1+
// Copyright 2019 Google LLC.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -66,6 +66,18 @@ service Spanner {
6666
};
6767
}
6868

69+
// Creates multiple new sessions.
70+
//
71+
// This API can be used to initialize a session cache on the clients.
72+
// See https://goo.gl/TgSFN2 for best practices on session cache management.
73+
rpc BatchCreateSessions(BatchCreateSessionsRequest)
74+
returns (BatchCreateSessionsResponse) {
75+
option (google.api.http) = {
76+
post: "/v1/{database=projects/*/instances/*/databases/*}/sessions:batchCreate"
77+
body: "*"
78+
};
79+
}
80+
6981
// Gets a session. Returns `NOT_FOUND` if the session does not exist.
7082
// This is mainly useful for determining whether a session is still
7183
// alive.
@@ -129,8 +141,9 @@ service Spanner {
129141
//
130142
// Statements are executed in order, sequentially.
131143
// [ExecuteBatchDmlResponse][Spanner.ExecuteBatchDmlResponse] will contain a
132-
// [ResultSet][google.spanner.v1.ResultSet] for each DML statement that has successfully executed. If a
133-
// statement fails, its error status will be returned as part of the
144+
// [ResultSet][google.spanner.v1.ResultSet] for each DML statement that has
145+
// successfully executed. If a statement fails, its error status will be
146+
// returned as part of the
134147
// [ExecuteBatchDmlResponse][Spanner.ExecuteBatchDmlResponse]. Execution will
135148
// stop at the first failed statement; the remaining statements will not run.
136149
//
@@ -142,7 +155,8 @@ service Spanner {
142155
// See more details in
143156
// [ExecuteBatchDmlRequest][Spanner.ExecuteBatchDmlRequest] and
144157
// [ExecuteBatchDmlResponse][Spanner.ExecuteBatchDmlResponse].
145-
rpc ExecuteBatchDml(ExecuteBatchDmlRequest) returns (ExecuteBatchDmlResponse) {
158+
rpc ExecuteBatchDml(ExecuteBatchDmlRequest)
159+
returns (ExecuteBatchDmlResponse) {
146160
option (google.api.http) = {
147161
post: "/v1/{session=projects/*/instances/*/databases/*/sessions/*}:executeBatchDml"
148162
body: "*"
@@ -275,6 +289,31 @@ message CreateSessionRequest {
275289
Session session = 2;
276290
}
277291

292+
// The request for
293+
// [BatchCreateSessions][google.spanner.v1.Spanner.BatchCreateSessions].
294+
message BatchCreateSessionsRequest {
295+
// Required. The database in which the new sessions are created.
296+
string database = 1;
297+
298+
// Parameters to be applied to each created session.
299+
Session session_template = 2;
300+
301+
// Required. The number of sessions to be created in this batch call.
302+
// The API may return fewer than the requested number of sessions. If a
303+
// specific number of sessions are desired, the client can make additional
304+
// calls to BatchCreateSessions (adjusting
305+
// [session_count][google.spanner.v1.BatchCreateSessionsRequest.session_count]
306+
// as necessary).
307+
int32 session_count = 3;
308+
}
309+
310+
// The response for
311+
// [BatchCreateSessions][google.spanner.v1.Spanner.BatchCreateSessions].
312+
message BatchCreateSessionsResponse {
313+
// The freshly created sessions.
314+
repeated Session session = 1;
315+
}
316+
278317
// A session in the Cloud Spanner API.
279318
message Session {
280319
// The name of the session. This is always system-assigned; values provided
@@ -371,9 +410,6 @@ message ExecuteSqlRequest {
371410
// Required. The session in which the SQL query should be performed.
372411
string session = 1;
373412

374-
// The transaction to use. If none is provided, the default is a
375-
// temporary read-only transaction with strong concurrency.
376-
//
377413
// The transaction to use.
378414
//
379415
// For queries, if none is provided, the default is a temporary read-only
@@ -476,7 +512,9 @@ message ExecuteBatchDmlRequest {
476512

477513
// It is not always possible for Cloud Spanner to infer the right SQL type
478514
// from a JSON value. For example, values of type `BYTES` and values
479-
// of type `STRING` both appear in [params][google.spanner.v1.ExecuteBatchDmlRequest.Statement.params] as JSON strings.
515+
// of type `STRING` both appear in
516+
// [params][google.spanner.v1.ExecuteBatchDmlRequest.Statement.params] as
517+
// JSON strings.
480518
//
481519
// In these cases, `param_types` can be used to specify the exact
482520
// SQL type for some or all of the SQL statement parameters. See the
@@ -508,11 +546,13 @@ message ExecuteBatchDmlRequest {
508546
int64 seqno = 4;
509547
}
510548

511-
// The response for [ExecuteBatchDml][google.spanner.v1.Spanner.ExecuteBatchDml]. Contains a list
512-
// of [ResultSet][google.spanner.v1.ResultSet], one for each DML statement that has successfully executed.
513-
// If a statement fails, the error is returned as part of the response payload.
514-
// Clients can determine whether all DML statements have run successfully, or if
515-
// a statement failed, using one of the following approaches:
549+
// The response for
550+
// [ExecuteBatchDml][google.spanner.v1.Spanner.ExecuteBatchDml]. Contains a list
551+
// of [ResultSet][google.spanner.v1.ResultSet], one for each DML statement that
552+
// has successfully executed. If a statement fails, the error is returned as
553+
// part of the response payload. Clients can determine whether all DML
554+
// statements have run successfully, or if a statement failed, using one of the
555+
// following approaches:
516556
//
517557
// 1. Check if 'status' field is OkStatus.
518558
// 2. Check if result_sets_size() equals the number of statements in
@@ -529,9 +569,11 @@ message ExecuteBatchDmlRequest {
529569
// result_set_size() client can determine that the 3rd statement has failed.
530570
message ExecuteBatchDmlResponse {
531571
// ResultSets, one for each statement in the request that ran successfully, in
532-
// the same order as the statements in the request. Each [ResultSet][google.spanner.v1.ResultSet] will
533-
// not contain any rows. The [ResultSetStats][google.spanner.v1.ResultSetStats] in each [ResultSet][google.spanner.v1.ResultSet] will
534-
// contain the number of rows modified by the statement.
572+
// the same order as the statements in the request. Each
573+
// [ResultSet][google.spanner.v1.ResultSet] will not contain any rows. The
574+
// [ResultSetStats][google.spanner.v1.ResultSetStats] in each
575+
// [ResultSet][google.spanner.v1.ResultSet] will contain the number of rows
576+
// modified by the statement.
535577
//
536578
// Only the first ResultSet in the response contains a valid
537579
// [ResultSetMetadata][google.spanner.v1.ResultSetMetadata].

0 commit comments

Comments
 (0)