Skip to content

Commit

Permalink
Extend get_run() to support secondary instances
Browse files Browse the repository at this point in the history
Simplify the coding of the APIs moving the logic of primary/secondary
instance in the RunDB class.
Drop old debug code.
  • Loading branch information
ppigazzini committed May 7, 2024
1 parent 791886a commit d28ec43
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 36 deletions.
20 changes: 9 additions & 11 deletions server/fishtest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from datetime import datetime, timezone
from urllib.parse import urlparse

from bson.objectid import ObjectId
from fishtest.schemas import api_access_schema, api_schema, gzip_data
from fishtest.stats.stat_util import SPRT_elo, get_elo
from fishtest.util import worker_name
Expand All @@ -23,13 +22,15 @@
Important note
==============
All APIs that rely on the `run_cache` of `rundb.get_run()`
must be served from the main Fishtest instance.
APIs hosted on the primary Fishtest instance have read and write access
to the `run_cache` via `rundb.get_run()` and `rundb.buffer()`
If other Fishtest instances need information about runs,
they should query the database directly.
However, keep in mind that this information might be slightly outdated.
This depends on how frequently the main instance flushes its `run_cache`.
APIs hosted on secondary instances have read access to the `run`
from the database via `rundb.get_run()`. However, they should not
attempt to write to `run_cache` using `rundb.buffer()`.
Proper configuration of `nginx` is crucial for this, and should be done
according to the route/URL mapping defined in `__init__.py`.
"""

WORKER_VERSION = 236
Expand Down Expand Up @@ -143,10 +144,7 @@ def validate_request(self):
# is a supplied run_id correct?
if "run_id" in self.request_body:
run_id = self.request_body["run_id"]
if self.request.rundb.is_primary_instance():
run = self.request.rundb.get_run(run_id)
else:
run = self.request.rundb.runs.find_one({"_id": ObjectId(run_id)})
run = self.request.rundb.get_run(run_id)
if run is None:
self.handle_error("Invalid run_id: {}".format(run_id))
self.__run = run
Expand Down
33 changes: 9 additions & 24 deletions server/fishtest/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
from pymongo import DESCENDING, MongoClient
from vtjson import ValidationError, validate

DEBUG = False

boot_time = datetime.now(timezone.utc)

last_rundb = None
Expand Down Expand Up @@ -367,25 +365,23 @@ def exit_run(signum, frame):
signal.signal(signal.SIGTERM, exit_run)

def get_run(self, r_id):
with self.run_cache_lock:
r_id = str(r_id)
if r_id in self.run_cache:
self.run_cache[r_id]["rtime"] = time.time()
return self.run_cache[r_id]["run"]
try:
if self.is_primary_instance():
with self.run_cache_lock:
r_id = str(r_id)
if r_id in self.run_cache:
self.run_cache[r_id]["rtime"] = time.time()
return self.run_cache[r_id]["run"]
run = self.runs.find_one({"_id": ObjectId(r_id)})
if DEBUG:
print("Load", r_id, flush=True)
if run:
if run is not None:
self.run_cache[r_id] = {
"rtime": time.time(),
"ftime": time.time(),
"run": run,
"dirty": False,
}
return run
except:
return None
else:
return self.runs.find_one({"_id": ObjectId(r_id)})

def start_timer(self):
self.timer = threading.Timer(1.0, self.flush_buffers)
Expand Down Expand Up @@ -1066,17 +1062,6 @@ def priority(run): # lower is better

self.worker_runs[unique_key]["last_run"] = run["_id"]

if DEBUG:
print(
"Allocate run: https://tests.stockfishchess.org/tests/view/{} task_id: {} to {}/{} Stats: {}".format(
run["_id"],
task_id,
worker_info["username"],
unique_key,
run["tasks"][task_id]["stats"],
),
flush=True,
)
return {"run": run, "task_id": task_id}

# Create a lock for each active run
Expand Down
2 changes: 1 addition & 1 deletion server/fishtest/templates/tests_run.mak
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@
if (test_signature) {
document.getElementById("test-signature").value = test_signature;
}

if (base_branch) {
document.getElementById("base-branch").value = base_branch;
}
Expand Down

0 comments on commit d28ec43

Please sign in to comment.