Skip to content

Commit

Permalink
Try to avoid workers with the same name.
Browse files Browse the repository at this point in the history
During request_task, we check if there are active tasks
for workers with the same name, which have recently been
updated (i.e. they are not dead). If so then we return an
error.

Should fix official-stockfish#1360.
  • Loading branch information
vdbergh committed Aug 25, 2023
1 parent 7363126 commit 2a64e7b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
49 changes: 33 additions & 16 deletions server/fishtest/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,27 +769,44 @@ def priority(run): # lower is better

self.task_runs.sort(key=priority)

# We go through the list of unfinished runs to see if the worker
# get the list of active tasks

active_tasks = [
task for run in self.task_runs for task in run["tasks"] if task["active"]
]

# We go through the list of active tasks to see if a worker with the same
# name is already connected.

my_name = "-".join(worker_name(worker_info).split("-")[0:3])
now = datetime.utcnow()
for task in active_tasks:
task_name = "-".join(worker_name(task["worker_info"]).split("-")[0:3])
if my_name == task_name:
last_update = (now - task["last_updated"]).seconds
# 120 = period of heartbeat in worker.
if last_update <= 120:
error = 'Request_task: There is already a worker running with name "{}" which sent an update {} seconds ago'.format(
my_name,
last_update,
)
return {"task_waiting": False, "error": error}

# We go through the list of active tasks to see if the worker
# has reached the number of allowed connections from the same ip
# address.

connections = 0
connections_limit = self.userdb.get_machine_limit(worker_info["username"])
for run in self.task_runs:
for task in run["tasks"]:
if (
task["active"]
and task["worker_info"]["remote_addr"] == worker_info["remote_addr"]
):
connections += 1
if connections >= connections_limit:
error = (
"Request_task: Machine limit reached for user {}".format(
worker_info["username"]
)
)
print(error, flush=True)
return {"task_waiting": False, "error": error}
for task in active_tasks:
if task["worker_info"]["remote_addr"] == worker_info["remote_addr"]:
connections += 1
if connections >= connections_limit:
error = "Request_task: Machine limit reached for user {}".format(
worker_info["username"]
)
print(error, flush=True)
return {"task_waiting": False, "error": error}

# Now go through the sorted list of unfinished runs.
# We will add a task to the first run that is suitable.
Expand Down
22 changes: 11 additions & 11 deletions server/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,17 +521,6 @@ def test_auto_purge_runs(self):
task1 = self.rundb.get_run(run_id)["tasks"][0]
task_size1 = task1["num_games"]

# Request task 2 of 2
request = self.correct_password_request()
response = ApiView(request).request_task()
self.assertEqual(response["run"]["_id"], str(run["_id"]))
self.assertEqual(response["task_id"], 1)
task2 = self.rundb.get_run(run_id)["tasks"][1]
task_size2 = task2["num_games"]
task_start2 = task2["start"]

self.assertEqual(task_start2, task_size1)

# Finish task 1 of 2
n_wins = task_size1 // 5
n_losses = task_size1 // 5
Expand All @@ -556,6 +545,17 @@ def test_auto_purge_runs(self):
run = self.rundb.get_run(run_id)
self.assertFalse(run["finished"])

# Request task 2 of 2
request = self.correct_password_request()
response = ApiView(request).request_task()
self.assertEqual(response["run"]["_id"], str(run["_id"]))
self.assertEqual(response["task_id"], 1)
task2 = self.rundb.get_run(run_id)["tasks"][1]
task_size2 = task2["num_games"]
task_start2 = task2["start"]

self.assertEqual(task_start2, task_size1)

# Finish task 2 of 2
n_wins = task_size2 // 5
n_losses = task_size2 // 5
Expand Down

0 comments on commit 2a64e7b

Please sign in to comment.