-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: bulk writer 555 rate_limiter (#368) * added 555 throttle utility * Update google/cloud/firestore_v1/throttle.py Co-authored-by: Tres Seaver <tseaver@palladion.com> * added ability to request a number of tokens * replaced Callable now parameter with module function * updated tests * renamed throttle -> ramp up * improved docstrings * linting * fixed test coverage * rename to RateLimiter and defer clock to first op * linting Co-authored-by: Tres Seaver <tseaver@palladion.com> * feat: added new batch class for BulkWriter (#397) * feat: added new batch class for BulkWriter * updated docstring to use less colloquial language * feat: BulkWriter implementation (#384) * feat: added `write` method to batch classes * added docstrings to all 3 batch classes instead of just the base * updated batch classes to remove control flag now branches logic via subclasses * fixed broken tests off abstract class * fixed docstring * refactored BulkWriteBatch this commit increases the distance between WriteBatch and BulkWriteBatch * began adding [Async]BulkWriter * continued implementation * working impl or BW * tidied up BW impl * beginning of unit tests for BW * fixed merge problem * initial set of BW unit tests * refactored bulkwriter sending mechanism now consumes off the queue and schedules on the main thread, only going async to actually send * final CI touch ups * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * moved BulkWriter parameters to options format * rebased off master * test fixes Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * feat: add retry support for BulkWriter errors (#413) * parent 0176cc7 author Craig Labenz <craig.labenz@gmail.com> 1623693904 -0700 committer Craig Labenz <craig.labenz@gmail.com> 1628617523 -0400 feat: add retries to bulk-writer * fixed rebase error Co-authored-by: Tres Seaver <tseaver@palladion.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
eb45a36
commit 98a7753
Showing
18 changed files
with
2,325 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2021 Google LLC All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Helpers for batch requests to the Google Cloud Firestore API.""" | ||
from google.api_core import gapic_v1 # type: ignore | ||
from google.api_core import retry as retries # type: ignore | ||
|
||
from google.cloud.firestore_v1 import _helpers | ||
from google.cloud.firestore_v1.base_batch import BaseBatch | ||
from google.cloud.firestore_v1.types.firestore import BatchWriteResponse | ||
|
||
|
||
class BulkWriteBatch(BaseBatch): | ||
"""Accumulate write operations to be sent in a batch. Use this over | ||
`WriteBatch` for higher volumes (e.g., via `BulkWriter`) and when the order | ||
of operations within a given batch is unimportant. | ||
Because the order in which individual write operations are applied to the database | ||
is not guaranteed, `batch_write` RPCs can never contain multiple operations | ||
to the same document. If calling code detects a second write operation to a | ||
known document reference, it should first cut off the previous batch and | ||
send it, then create a new batch starting with the latest write operation. | ||
In practice, the [Async]BulkWriter classes handle this. | ||
This has the same set of methods for write operations that | ||
:class:`~google.cloud.firestore_v1.document.DocumentReference` does, | ||
e.g. :meth:`~google.cloud.firestore_v1.document.DocumentReference.create`. | ||
Args: | ||
client (:class:`~google.cloud.firestore_v1.client.Client`): | ||
The client that created this batch. | ||
""" | ||
|
||
def __init__(self, client) -> None: | ||
super(BulkWriteBatch, self).__init__(client=client) | ||
|
||
def commit( | ||
self, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None | ||
) -> BatchWriteResponse: | ||
"""Writes the changes accumulated in this batch. | ||
Write operations are not guaranteed to be applied in order and must not | ||
contain multiple writes to any given document. Preferred over `commit` | ||
for performance reasons if these conditions are acceptable. | ||
Args: | ||
retry (google.api_core.retry.Retry): Designation of what errors, if any, | ||
should be retried. Defaults to a system-specified policy. | ||
timeout (float): The timeout for this request. Defaults to a | ||
system-specified value. | ||
Returns: | ||
:class:`google.cloud.proto.firestore.v1.write.BatchWriteResponse`: | ||
Container holding the write results corresponding to the changes | ||
committed, returned in the same order as the changes were applied to | ||
this batch. An individual write result contains an ``update_time`` | ||
field. | ||
""" | ||
request, kwargs = self._prep_commit(retry, timeout) | ||
|
||
_api = self._client._firestore_api | ||
save_response: BatchWriteResponse = _api.batch_write( | ||
request=request, metadata=self._client._rpc_metadata, **kwargs, | ||
) | ||
|
||
self._write_pbs = [] | ||
self.write_results = list(save_response.write_results) | ||
|
||
return save_response | ||
|
||
def _prep_commit(self, retry: retries.Retry, timeout: float): | ||
request = { | ||
"database": self._client._database_string, | ||
"writes": self._write_pbs, | ||
"labels": None, | ||
} | ||
kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout) | ||
return request, kwargs |
Oops, something went wrong.