Abacn commented on code in PR #31584: URL: https://github.com/apache/beam/pull/31584#discussion_r1638738500
########## sdks/python/apache_beam/io/gcp/gcsio_retry.py: ########## @@ -0,0 +1,72 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# + +""" +Throttling Handler for GCSIO +""" + +import logging +import math +import random +import time + +from google.api_core import exceptions as api_exceptions +from google.api_core import retry +from google.cloud.storage.retry import DEFAULT_RETRY +from google.cloud.storage.retry import _should_retry # pylint: disable=protected-access + +from apache_beam.metrics.metric import Metrics +from apache_beam.options.pipeline_options import GoogleCloudOptions + +_LOGGER = logging.getLogger(__name__) + +__all__ = ['DEFAULT_RETRY_WITH_THROTTLING_COUNTERS'] + + +class ThrottlingHandler(object): + _THROTTLED_SECS = Metrics.counter('gcsio', "cumulativeThrottlingSeconds") + + def __init__(self, max_retries=10, max_retry_wait=600): + self._max_retries = max_retries + self._max_retry_wait = max_retry_wait + self._num_retries = 0 + self._total_retry_wait = 0 + + def _get_next_wait_time(self): + wait_time = 2**self._num_retries + max_jitter = wait_time / 4.0 + wait_time += random.uniform(-max_jitter, max_jitter) + return max(1, min(wait_time, self._max_retry_wait)) + + def __call__(self, exc): + if isinstance(exc, api_exceptions.TooManyRequests): + _LOGGER.debug('Caught GCS quota error (%s), retrying.', exc.reason) + self._num_retries += 1 + sleep_seconds = self._get_next_wait_time() + ThrottlingHandler._THROTTLED_SECS.inc(math.ceil(sleep_seconds)) + time.sleep(sleep_seconds) Review Comment: All we need is the actual sleep time at each retry. Is it possible to obtain from the call back (similar to here : https://github.com/apache/beam/blob/30cce4450e3ada1142c798adcff18733620ff9e6/sdks/python/apache_beam/io/gcp/bigquery.py#L1293) ########## sdks/python/apache_beam/io/gcp/gcsio_retry.py: ########## @@ -0,0 +1,72 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# + +""" +Throttling Handler for GCSIO +""" + +import logging +import math +import random +import time + +from google.api_core import exceptions as api_exceptions +from google.api_core import retry +from google.cloud.storage.retry import DEFAULT_RETRY +from google.cloud.storage.retry import _should_retry # pylint: disable=protected-access + +from apache_beam.metrics.metric import Metrics +from apache_beam.options.pipeline_options import GoogleCloudOptions + +_LOGGER = logging.getLogger(__name__) + +__all__ = ['DEFAULT_RETRY_WITH_THROTTLING_COUNTERS'] + + +class ThrottlingHandler(object): + _THROTTLED_SECS = Metrics.counter('gcsio', "cumulativeThrottlingSeconds") + + def __init__(self, max_retries=10, max_retry_wait=600): + self._max_retries = max_retries + self._max_retry_wait = max_retry_wait + self._num_retries = 0 + self._total_retry_wait = 0 + + def _get_next_wait_time(self): + wait_time = 2**self._num_retries + max_jitter = wait_time / 4.0 + wait_time += random.uniform(-max_jitter, max_jitter) + return max(1, min(wait_time, self._max_retry_wait)) + + def __call__(self, exc): + if isinstance(exc, api_exceptions.TooManyRequests): + _LOGGER.debug('Caught GCS quota error (%s), retrying.', exc.reason) + self._num_retries += 1 + sleep_seconds = self._get_next_wait_time() + ThrottlingHandler._THROTTLED_SECS.inc(math.ceil(sleep_seconds)) + time.sleep(sleep_seconds) + + +DEFAULT_RETRY_WITH_THROTTLING_COUNTERS = retry.Retry( + predicate=_should_retry, on_error=ThrottlingHandler()) + + +def get_retry(pipeline_options): Review Comment: I don't think we need branch here, and I don't think we need to introduce another pipeline option. If user want to opt out reactive throttling In general it's good to constraint the number of pipeline option especially the tuning ones. ########## sdks/python/apache_beam/io/gcp/gcsio_retry.py: ########## @@ -0,0 +1,72 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# + +""" +Throttling Handler for GCSIO +""" + +import logging +import math +import random +import time + +from google.api_core import exceptions as api_exceptions +from google.api_core import retry +from google.cloud.storage.retry import DEFAULT_RETRY +from google.cloud.storage.retry import _should_retry # pylint: disable=protected-access + +from apache_beam.metrics.metric import Metrics +from apache_beam.options.pipeline_options import GoogleCloudOptions + +_LOGGER = logging.getLogger(__name__) + +__all__ = ['DEFAULT_RETRY_WITH_THROTTLING_COUNTERS'] + + +class ThrottlingHandler(object): + _THROTTLED_SECS = Metrics.counter('gcsio', "cumulativeThrottlingSeconds") + + def __init__(self, max_retries=10, max_retry_wait=600): + self._max_retries = max_retries + self._max_retry_wait = max_retry_wait + self._num_retries = 0 + self._total_retry_wait = 0 + + def _get_next_wait_time(self): + wait_time = 2**self._num_retries + max_jitter = wait_time / 4.0 + wait_time += random.uniform(-max_jitter, max_jitter) + return max(1, min(wait_time, self._max_retry_wait)) + + def __call__(self, exc): + if isinstance(exc, api_exceptions.TooManyRequests): + _LOGGER.debug('Caught GCS quota error (%s), retrying.', exc.reason) + self._num_retries += 1 + sleep_seconds = self._get_next_wait_time() + ThrottlingHandler._THROTTLED_SECS.inc(math.ceil(sleep_seconds)) + time.sleep(sleep_seconds) Review Comment: `__call__` here is executed in a callback thread. If is not the place to handle the actual sleep The `Retry` object itself has already internal exponential backoff. See cloud-storage-client's DEFAULT_RETRY, it only assigned a predicator to Retry, and the later handles the actual backoff when retry happens. on_error is just a callback thread. If this is a blocking thread, current change will cause double slepp; if it is a non-blocking thread, it does not reflect the actual retry time. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
