Skip to content

Commit 72a993a

Browse files
committedFeb 15, 2016
master: cache last RID. Closes #234
1 parent 649f3b9 commit 72a993a

File tree

4 files changed

+78
-48
lines changed

4 files changed

+78
-48
lines changed
 

‎.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ __pycache__/
1919
/artiq/test/results
2020
/artiq/test/h5types.h5
2121
/examples/master/results
22+
/examples/master/last_rid.pyon
2223
/examples/master/dataset_db.pyon
2324
/examples/sim/results
2425
/examples/sim/dataset_db.pyon
2526

2627
# recommended location for testbed
2728
/run
2829
# alternatively, when testing ad-hoc experiments at the root:
29-
/device_db.pyon
30-
/dataset_db.pyon
3130
/results
31+
/last_rid.pyon
32+
/dataset_db.pyon
33+
/device_db.pyon
3234
/h5types.h5
3335
/test*.py

‎artiq/frontend/artiq_master.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from artiq.master.log import log_args, init_log
1313
from artiq.master.databases import DeviceDB, DatasetDB
1414
from artiq.master.scheduler import Scheduler
15-
from artiq.master.worker_db import get_last_rid
15+
from artiq.master.worker_db import RIDCounter
1616
from artiq.master.experiments import FilesystemBackend, GitBackend, ExperimentDB
1717

1818

@@ -73,7 +73,7 @@ def main():
7373
"get_dataset": dataset_db.get,
7474
"update_dataset": dataset_db.update
7575
}
76-
scheduler = Scheduler(get_last_rid() + 1, worker_handlers, experiment_db)
76+
scheduler = Scheduler(RIDCounter(), worker_handlers, experiment_db)
7777
worker_handlers.update({
7878
"scheduler_submit": scheduler.submit,
7979
"scheduler_delete": scheduler.delete,

‎artiq/master/scheduler.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,6 @@ async def build(self):
116116
write_results = _mk_worker_method("write_results")
117117

118118

119-
class RIDCounter:
120-
def __init__(self, next_rid):
121-
self._next_rid = next_rid
122-
123-
def get(self):
124-
rid = self._next_rid
125-
self._next_rid += 1
126-
return rid
127-
128-
129119
class RunPool:
130120
def __init__(self, ridc, worker_handlers, notifier, experiment_db):
131121
self.runs = dict()
@@ -387,15 +377,15 @@ async def _do(self):
387377

388378

389379
class Scheduler:
390-
def __init__(self, next_rid, worker_handlers, experiment_db):
380+
def __init__(self, ridc, worker_handlers, experiment_db):
391381
self.notifier = Notifier(dict())
392382

393383
self._pipelines = dict()
394384
self._worker_handlers = worker_handlers
395385
self._experiment_db = experiment_db
396386
self._terminated = False
397387

398-
self._ridc = RIDCounter(next_rid)
388+
self._ridc = ridc
399389
self._deleter = Deleter(self._pipelines)
400390

401391
def start(self):

‎artiq/master/worker_db.py

+70-32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import importlib
33
import logging
44
import os
5+
import tempfile
56
import time
67
import re
78

@@ -15,6 +16,75 @@
1516
logger = logging.getLogger(__name__)
1617

1718

19+
class RIDCounter:
20+
def __init__(self, cache_filename="last_rid.pyon", results_dir="results"):
21+
self.cache_filename = cache_filename
22+
self.results_dir = results_dir
23+
self._next_rid = self._last_rid() + 1
24+
logger.debug("Next RID is %d", self._next_rid)
25+
26+
def get(self):
27+
rid = self._next_rid
28+
self._next_rid += 1
29+
self._update_cache(rid)
30+
return rid
31+
32+
def _last_rid(self):
33+
try:
34+
rid = self._last_rid_from_cache()
35+
except FileNotFoundError:
36+
logger.debug("Last RID cache not found, scanning results")
37+
rid = self._last_rid_from_results()
38+
self._update_cache(rid)
39+
return rid
40+
else:
41+
logger.debug("Using last RID from cache")
42+
return rid
43+
44+
def _update_cache(self, rid):
45+
contents = str(rid) + "\n"
46+
directory = os.path.abspath(os.path.dirname(self.cache_filename))
47+
with tempfile.NamedTemporaryFile("w", dir=directory, delete=False) as f:
48+
f.write(contents)
49+
tmpname = f.name
50+
os.replace(tmpname, self.cache_filename)
51+
52+
def _last_rid_from_cache(self):
53+
with open(self.cache_filename, "r") as f:
54+
return int(f.read())
55+
56+
def _last_rid_from_results(self):
57+
r = -1
58+
try:
59+
day_folders = os.listdir(self.results_dir)
60+
except:
61+
return r
62+
day_folders = filter(lambda x: re.fullmatch('\d\d\d\d-\d\d-\d\d', x),
63+
day_folders)
64+
for df in day_folders:
65+
day_path = os.path.join(self.results_dir, df)
66+
try:
67+
minute_folders = os.listdir(day_path)
68+
except:
69+
continue
70+
minute_folders = filter(lambda x: re.fullmatch('\d\d-\d\d', x),
71+
minute_folders)
72+
for mf in minute_folders:
73+
minute_path = os.path.join(day_path, mf)
74+
try:
75+
h5files = os.listdir(minute_path)
76+
except:
77+
continue
78+
for x in h5files:
79+
m = re.fullmatch('(\d\d\d\d\d\d\d\d\d)-.*\.h5', x)
80+
if m is None:
81+
continue
82+
rid = int(m.group(1))
83+
if rid > r:
84+
r = rid
85+
return r
86+
87+
1888
def _create_device(desc, device_mgr):
1989
ty = desc["type"]
2090
if ty == "local":
@@ -87,38 +157,6 @@ def get_hdf5_output(start_time, rid, name):
87157
return h5py.File(os.path.join(dirname, filename), "w")
88158

89159

90-
def get_last_rid():
91-
r = -1
92-
try:
93-
day_folders = os.listdir("results")
94-
except:
95-
return r
96-
day_folders = filter(lambda x: re.fullmatch('\d\d\d\d-\d\d-\d\d', x),
97-
day_folders)
98-
for df in day_folders:
99-
day_path = os.path.join("results", df)
100-
try:
101-
minute_folders = os.listdir(day_path)
102-
except:
103-
continue
104-
minute_folders = filter(lambda x: re.fullmatch('\d\d-\d\d', x),
105-
minute_folders)
106-
for mf in minute_folders:
107-
minute_path = os.path.join(day_path, mf)
108-
try:
109-
h5files = os.listdir(minute_path)
110-
except:
111-
continue
112-
for x in h5files:
113-
m = re.fullmatch('(\d\d\d\d\d\d\d\d\d)-.*\.h5', x)
114-
if m is None:
115-
continue
116-
rid = int(m.group(1))
117-
if rid > r:
118-
r = rid
119-
return r
120-
121-
122160
_type_to_hdf5 = {
123161
int: h5py.h5t.STD_I64BE,
124162
float: h5py.h5t.IEEE_F64BE,

0 commit comments

Comments
 (0)
Please sign in to comment.