Skip to content

Commit

Permalink
Fix execution time checking by keeping lastUpdatedTime in db (#1573)
Browse files Browse the repository at this point in the history
Instead of relying on status to keep track of lastUpdatedTime, just keep
track of it in the db to ensure it is property updated each time. Fixes
issues on not counting execution time while avoiding previous issues of
double-counting
  • Loading branch information
ikreymer committed Mar 6, 2024
1 parent 5a1cb39 commit 0e0add6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
7 changes: 7 additions & 0 deletions backend/btrixcloud/crawls.py
Expand Up @@ -461,6 +461,13 @@ async def inc_crawl_exec_time(self, crawl_id, exec_time, last_updated_time):
},
)

async def get_crawl_exec_last_update_time(self, crawl_id):
"""get crawl last updated time"""
res = await self.crawls.find_one(
{"_id": crawl_id, "type": "crawl"}, projection=["_lut"]
)
return res and res.get("_lut")

async def get_crawl_state(self, crawl_id):
"""return current crawl state of a crawl"""
res = await self.crawls.find_one(
Expand Down
24 changes: 10 additions & 14 deletions backend/btrixcloud/operator.py
Expand Up @@ -515,7 +515,9 @@ async def sync_crawls(self, data: MCSyncData):

else:
status.scale = crawl.scale
status.lastUpdatedTime = to_k8s_date(dt_now())
now = dt_now()
await self.crawl_ops.inc_crawl_exec_time(crawl_id, 0, now)
status.lastUpdatedTime = to_k8s_date(now)

children = self._load_redis(params, status, data.children)

Expand Down Expand Up @@ -1107,12 +1109,15 @@ async def increment_pod_exec_time(
"""inc exec time tracking"""
now = dt_now()

if not status.lastUpdatedTime:
update_start_time = await self.crawl_ops.get_crawl_exec_last_update_time(
crawl_id
)

if not update_start_time:
await self.crawl_ops.inc_crawl_exec_time(crawl_id, 0, now)
status.lastUpdatedTime = to_k8s_date(now)
return

update_start_time = from_k8s_date(status.lastUpdatedTime)

reason = None
update_duration = (now - update_start_time).total_seconds()

Expand Down Expand Up @@ -1186,16 +1191,6 @@ async def increment_pod_exec_time(
max_duration = max(duration, max_duration)

if exec_time:
if not await self.crawl_ops.inc_crawl_exec_time(
crawl_id, exec_time, status.lastUpdatedTime
):
# if lastUpdatedTime is same as previous, something is wrong, don't update!
print(
"Already updated for lastUpdatedTime, skipping execTime update!",
flush=True,
)
return

await self.org_ops.inc_org_time_stats(oid, exec_time, True)
status.crawlExecTime += exec_time
status.elapsedCrawlTime += max_duration
Expand All @@ -1205,6 +1200,7 @@ async def increment_pod_exec_time(
flush=True,
)

await self.crawl_ops.inc_crawl_exec_time(crawl_id, exec_time, now)
status.lastUpdatedTime = to_k8s_date(now)

def should_mark_waiting(self, state, started):
Expand Down

0 comments on commit 0e0add6

Please sign in to comment.