From e89013075ea6e0c52b904966852f49b3619c7e4c Mon Sep 17 00:00:00 2001 From: Sangram Date: Wed, 31 Jul 2019 01:57:26 +0530 Subject: [PATCH 1/2] Fix 'deadline exceeded' error for create bigtable in create table snippet --- bigtable/docs/snippets.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bigtable/docs/snippets.py b/bigtable/docs/snippets.py index 7f4071b41fd8..a2b4898b2121 100644 --- a/bigtable/docs/snippets.py +++ b/bigtable/docs/snippets.py @@ -403,7 +403,18 @@ def test_bigtable_create_table(): table = instance.table("table_my") # Define the GC policy to retain only the most recent 2 versions. max_versions_rule = column_family.MaxVersionsGCRule(2) - table.create(column_families={"cf1": max_versions_rule}) + + RETRIES = 4 + # Retry if deadline exceed error. + for retry in range(RETRIES): + try: + table.create(column_families={"cf1": max_versions_rule}) + break + except Exception as e: + if retry == RETRIES - 1: + raise e + else: + time.sleep(2 ** (retry - 1)) # [END bigtable_create_table] try: From 9997628a2e7e600b1daedbd3427dfe6967629fec Mon Sep 17 00:00:00 2001 From: Sangram Date: Wed, 7 Aug 2019 16:26:45 +0530 Subject: [PATCH 2/2] Change retry logic to be exposed to user. --- bigtable/docs/snippets.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/bigtable/docs/snippets.py b/bigtable/docs/snippets.py index a2b4898b2121..1119b3b6bd90 100644 --- a/bigtable/docs/snippets.py +++ b/bigtable/docs/snippets.py @@ -395,6 +395,8 @@ def test_bigtable_update_cluster(): def test_bigtable_create_table(): # [START bigtable_create_table] + from google.api_core import exceptions + from google.api_core import retry from google.cloud.bigtable import Client from google.cloud.bigtable import column_family @@ -404,17 +406,12 @@ def test_bigtable_create_table(): # Define the GC policy to retain only the most recent 2 versions. max_versions_rule = column_family.MaxVersionsGCRule(2) - RETRIES = 4 - # Retry if deadline exceed error. - for retry in range(RETRIES): - try: - table.create(column_families={"cf1": max_versions_rule}) - break - except Exception as e: - if retry == RETRIES - 1: - raise e - else: - time.sleep(2 ** (retry - 1)) + # Could include other retriable exception types + # Could configure deadline, etc. + predicate_504 = retry.if_exception_type(exceptions.DeadlineExceeded) + retry_504 = retry.Retry(predicate_504) + + retry_504(table.create)(column_families={"cf1": max_versions_rule}) # [END bigtable_create_table] try: