Skip to content

Commit 46be7c9

Browse files
authored
Add retry logic on HTTP 400 errors for parcel and host functions (#288)
* Add retry logic on HTTP 400 errors for parcel functions * Add retry logic to HTTP 400 errors for cluster assignment for a host Signed-off-by: Webster Mudge <[email protected]>
1 parent 1f782f7 commit 46be7c9

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

plugins/module_utils/parcel_utils.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
ParcelResourceApi,
2828
ParcelsResourceApi,
2929
)
30+
from cm_client.rest import ApiException
3031

3132
from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import (
3233
normalize_output,
@@ -89,11 +90,26 @@ def _wait(self, stage: STAGE) -> None:
8990
return Exception(f"Failed to reach {stage.name}: timeout ({self.timeout} secs)")
9091

9192
def _exec(self, stage: STAGE, func) -> None:
92-
func(
93-
cluster_name=self.cluster,
94-
product=self.product,
95-
version=self.version,
96-
)
93+
retries = 0
94+
95+
# Retry the function, i.e. start_distribution, if receiving a 400 error due to
96+
# potential "eventual consistency" issues with parcel torrents.
97+
while True:
98+
try:
99+
func(
100+
cluster_name=self.cluster,
101+
product=self.product,
102+
version=self.version,
103+
)
104+
break
105+
except ApiException as e:
106+
if retries < 4 and e.status == 400:
107+
retries += 1
108+
time.sleep(15)
109+
continue
110+
else:
111+
raise e
112+
97113
self._wait(stage)
98114

99115
def remove(self):

plugins/modules/host.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@
339339
returned: when supported
340340
"""
341341

342+
from time import sleep
342343

343344
from cm_client import (
344345
ApiHost,
@@ -626,18 +627,30 @@ def process(self):
626627
self.diff["after"].update(cluster=cluster.name)
627628

628629
if not self.module.check_mode:
629-
# Add the host to the cluster
630-
cluster_api.add_hosts(
631-
cluster_name=cluster.name,
632-
body=ApiHostRefList(
633-
items=[
634-
ApiHostRef(
635-
host_id=current.host_id,
636-
hostname=current.hostname,
637-
)
638-
]
639-
),
640-
)
630+
# Add the host to the cluster (with simple retry)
631+
add_retry = 0
632+
633+
while True:
634+
try:
635+
cluster_api.add_hosts(
636+
cluster_name=cluster.name,
637+
body=ApiHostRefList(
638+
items=[
639+
ApiHostRef(
640+
host_id=current.host_id,
641+
hostname=current.hostname,
642+
)
643+
]
644+
),
645+
)
646+
break
647+
except ApiException as ae:
648+
if add_retry < 4 and ae.status == 400:
649+
add_retry += 1
650+
sleep(10)
651+
continue
652+
else:
653+
raise ae
641654

642655
# parcel_api = ParcelResourceApi(self.api_client)
643656
# try:

0 commit comments

Comments
 (0)