Skip to content

Commit c3bbaa4

Browse files
authored
Merge pull request #647 from FlorentinD/wait-with-exponential-retry
Use exponential backoff for waiting
2 parents 908defa + f849183 commit c3bbaa4

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

graphdatascience/session/aura_api.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ def list_sessions(self, dbid: str) -> List[SessionDetails]:
9797
return [SessionDetails.fromJson(s) for s in response.json()]
9898

9999
def wait_for_session_running(
100-
self, session_id: str, dbid: str, sleep_time: float = 0.2, max_sleep_time: float = 300
100+
self,
101+
session_id: str,
102+
dbid: str,
103+
sleep_time: float = 0.2,
104+
max_sleep_time: float = 10,
105+
max_wait_time: float = 300,
101106
) -> WaitResult:
102107
waited_time = 0.0
103-
while waited_time <= max_sleep_time:
108+
while waited_time < max_wait_time:
104109
session = self.list_session(session_id, dbid)
105110
if session is None:
106111
return WaitResult.from_error(f"Session `{session_id}` for database `{dbid}` not found -- please retry")
@@ -114,6 +119,7 @@ def wait_for_session_running(
114119
)
115120
waited_time += sleep_time
116121
time.sleep(sleep_time)
122+
sleep_time = min(sleep_time * 2, max_sleep_time, max_wait_time - waited_time)
117123

118124
return WaitResult.from_error(
119125
f"Session `{session_id}` for database `{dbid}` is not running after {waited_time} seconds"
@@ -204,10 +210,10 @@ def list_instance(self, instance_id: str) -> Optional[InstanceSpecificDetails]:
204210
return InstanceSpecificDetails.fromJson(raw_data)
205211

206212
def wait_for_instance_running(
207-
self, instance_id: str, sleep_time: float = 0.2, max_sleep_time: float = 300
213+
self, instance_id: str, sleep_time: float = 0.2, max_sleep_time: float = 10, max_wait_time: float = 300
208214
) -> WaitResult:
209215
waited_time = 0.0
210-
while waited_time <= max_sleep_time:
216+
while waited_time < max_wait_time:
211217
instance = self.list_instance(instance_id)
212218
if instance is None:
213219
return WaitResult.from_error("Instance is not found -- please retry")
@@ -223,6 +229,7 @@ def wait_for_instance_running(
223229
)
224230
waited_time += sleep_time
225231
time.sleep(sleep_time)
232+
sleep_time = min(sleep_time * 2, max_sleep_time, max_wait_time - waited_time)
226233

227234
return WaitResult.from_error(f"Instance is not running after waiting for {waited_time} seconds")
228235

graphdatascience/tests/unit/test_aura_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ def test_dont_wait_forever_for_session(requests_mock: Mocker, caplog: LogCapture
204204

205205
with caplog.at_level(logging.DEBUG):
206206
assert (
207-
"Session `id0` for database `dbid-1` is not running after 0.8 seconds"
208-
in api.wait_for_session_running("id0", "dbid-1", max_sleep_time=0.7).error
207+
"Session `id0` for database `dbid-1` is not running after 0.7 seconds"
208+
in api.wait_for_session_running("id0", "dbid-1", max_wait_time=0.7).error
209209
)
210210

211211
assert "Session `id0` is not yet running. Current status: Creating Host: foo.bar. Retrying in 0.2" in caplog.text
@@ -486,8 +486,8 @@ def test_dont_wait_forever(requests_mock: Mocker, caplog: LogCaptureFixture) ->
486486

487487
with caplog.at_level(logging.DEBUG):
488488
assert (
489-
"Instance is not running after waiting for 0.8"
490-
in api.wait_for_instance_running("id0", max_sleep_time=0.7).error
489+
"Instance is not running after waiting for 0.7"
490+
in api.wait_for_instance_running("id0", max_wait_time=0.7).error
491491
)
492492

493493
assert "Instance `id0` is not yet running. Current status: creating. Retrying in 0.2 seconds..." in caplog.text

graphdatascience/tests/unit/test_aurads_sessions.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ def list_instance(self, instance_id: str) -> Optional[InstanceSpecificDetails]:
7979
return None
8080

8181
def wait_for_instance_running(
82-
self, instance_id: str, sleep_time: float = 0.2, max_sleep_time: float = 300
82+
self,
83+
instance_id: str,
84+
sleep_time: float = 0.2,
85+
max_sleep_time: float = 5,
86+
max_wait_time: float = 5,
8387
) -> WaitResult:
84-
return super().wait_for_instance_running(instance_id, sleep_time=0.0001, max_sleep_time=0.001)
88+
return super().wait_for_instance_running(instance_id, sleep_time=0.0001, max_wait_time=0.001)
8589

8690
def tenant_details(self) -> TenantDetails:
8791
return TenantDetails(id=self._tenant_id, ds_type="fake-ds", regions_per_provider={"aws": {"leipzig-1"}})

graphdatascience/tests/unit/test_dedicated_sessions.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,27 @@ def list_instance(self, instance_id: str) -> Optional[InstanceSpecificDetails]:
119119
return None
120120

121121
def wait_for_session_running(
122-
self, session_id: str, dbid: str, sleep_time: float = 0.2, max_sleep_time: float = 5
122+
self,
123+
session_id: str,
124+
dbid: str,
125+
sleep_time: float = 0.2,
126+
max_sleep_time: float = 5,
127+
max_wait_time: float = 5,
123128
) -> WaitResult:
124-
return super().wait_for_session_running(session_id, dbid, sleep_time=0.0001, max_sleep_time=0.001)
129+
return super().wait_for_session_running(
130+
session_id, dbid, sleep_time=0.0001, max_wait_time=0.001, max_sleep_time=0.001
131+
)
125132

126133
def wait_for_instance_running(
127-
self, instance_id: str, sleep_time: float = 0.2, max_sleep_time: float = 300
134+
self,
135+
instance_id: str,
136+
sleep_time: float = 0.2,
137+
max_sleep_time: float = 5,
138+
max_wait_time: float = 5,
128139
) -> WaitResult:
129-
return super().wait_for_instance_running(instance_id, sleep_time=0.0001, max_sleep_time=0.001)
140+
return super().wait_for_instance_running(
141+
instance_id, sleep_time=0.0001, max_wait_time=0.001, max_sleep_time=0.001
142+
)
130143

131144
def tenant_details(self) -> TenantDetails:
132145
return TenantDetails(id=self._tenant_id, ds_type="fake-ds", regions_per_provider={"aws": {"leipzig-1"}})

0 commit comments

Comments
 (0)