Skip to content

Commit abeeaff

Browse files
authored
add retries for subnetwork not ready errors (#9523)
* add retries for subnetwork not ready errors * fix lint * experiment: remove autouse * chain decorators * fix lint * refactor fixtures with inner functions
1 parent 72deeb8 commit abeeaff

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

dataproc/snippets/create_cluster_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import uuid
1717

1818
import backoff
19-
from google.api_core.exceptions import (InternalServerError, NotFound,
19+
from google.api_core.exceptions import (InternalServerError, InvalidArgument, NotFound,
2020
ServiceUnavailable)
2121
from google.cloud import dataproc_v1 as dataproc
2222
import pytest
@@ -50,7 +50,8 @@ def teardown():
5050
print("Cluster already deleted")
5151

5252

53-
@backoff.on_exception(backoff.expo, (InternalServerError, ServiceUnavailable), max_tries=5)
53+
# InvalidArgument is thrown when the subnetwork is not ready
54+
@backoff.on_exception(backoff.expo, (InternalServerError, ServiceUnavailable, InvalidArgument), max_tries=5)
5455
def test_cluster_create(capsys):
5556
# Wrapper function for client library function
5657
create_cluster.create_cluster(PROJECT_ID, REGION, CLUSTER_NAME)

dataproc/snippets/submit_job_test.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import uuid
1717

1818
import backoff
19-
from google.api_core.exceptions import (InternalServerError, NotFound,
19+
from google.api_core.exceptions import (InternalServerError, InvalidArgument, NotFound,
2020
ServiceUnavailable)
2121
from google.cloud import dataproc_v1 as dataproc
2222
import pytest
@@ -38,22 +38,22 @@
3838

3939
@pytest.fixture(autouse=True)
4040
def setup_teardown():
41-
try:
42-
cluster_client = dataproc.ClusterControllerClient(
43-
client_options={
44-
"api_endpoint": "{}-dataproc.googleapis.com:443".format(REGION)
45-
}
46-
)
41+
cluster_client = dataproc.ClusterControllerClient(
42+
client_options={
43+
"api_endpoint": "{}-dataproc.googleapis.com:443".format(REGION)
44+
}
45+
)
4746

47+
# Retry on InvalidArgument subnetwork not ready error
48+
@backoff.on_exception(backoff.expo, (InvalidArgument), max_tries=3)
49+
def setup():
4850
# Create the cluster.
4951
operation = cluster_client.create_cluster(
5052
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER}
5153
)
5254
operation.result()
5355

54-
yield
55-
56-
finally:
56+
def teardown():
5757
try:
5858
operation = cluster_client.delete_cluster(
5959
request={
@@ -66,6 +66,11 @@ def setup_teardown():
6666

6767
except NotFound:
6868
print("Cluster already deleted")
69+
try:
70+
setup()
71+
yield
72+
finally:
73+
teardown()
6974

7075

7176
@backoff.on_exception(backoff.expo, (InternalServerError, ServiceUnavailable), max_tries=5)

dataproc/snippets/update_cluster_test.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import uuid
2020

2121
import backoff
22-
from google.api_core.exceptions import (InternalServerError, NotFound,
22+
from google.api_core.exceptions import (InternalServerError, InvalidArgument, NotFound,
2323
ServiceUnavailable)
2424
from google.cloud.dataproc_v1.services.cluster_controller.client import \
2525
ClusterControllerClient
@@ -51,15 +51,16 @@ def cluster_client():
5151

5252
@pytest.fixture(autouse=True)
5353
def setup_teardown(cluster_client):
54-
try:
54+
# InvalidArgument is thrown when the subnetwork is not ready
55+
@backoff.on_exception(backoff.expo, (InvalidArgument), max_tries=3)
56+
def setup():
5557
# Create the cluster.
5658
operation = cluster_client.create_cluster(
5759
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER}
5860
)
5961
operation.result()
6062

61-
yield
62-
finally:
63+
def teardown():
6364
try:
6465
operation = cluster_client.delete_cluster(
6566
request={
@@ -71,6 +72,11 @@ def setup_teardown(cluster_client):
7172
operation.result()
7273
except NotFound:
7374
print("Cluster already deleted")
75+
try:
76+
setup()
77+
yield
78+
finally:
79+
teardown()
7480

7581

7682
@backoff.on_exception(backoff.expo, (InternalServerError, ServiceUnavailable), max_tries=5)

0 commit comments

Comments
 (0)