Skip to content

Commit 137f22d

Browse files
authored
Issue 310: improving boot times (#311)
* Repaired the FSM Removed the navel gazing state because it was taking so long and tried to make it asynchronous. This causes problems elsewhere when states are changed by the user with a reset. So, back into the state machine. * Reduce processing time While processing time for navel gazing is fixed by the number of metric data values (50 ms per state vector or 14 values), it was possible to clean up the navel gazing to properly load the diary it has been keeping. * Reduce loaded metrics The next tall tent pole in the processing is retrieving millions of metrics when only a few hundred are new. By find the latest run ID in the diary, can have the DB retrieve just the newer run IDs and run ID 0 since they are overwritten.
1 parent 20d0b40 commit 137f22d

File tree

8 files changed

+76
-33
lines changed

8 files changed

+76
-33
lines changed

.github/workflows/pylint.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ contextmanager-decorators=contextlib.contextmanager
582582
generated-members=dawgie.context.fsm,
583583
logging.handlers.StreamHandler,
584584
self.archiving_trigger,
585+
self.contemplation_trigger,
585586
self.running_trigger,
586587
self.state,
587588
self.update_trigger,

Python/dawgie/db/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ def gather(anz, ans) -> dawgie.Aspect:
148148
return _db_in_use().gather(anz, ans)
149149

150150

151-
def metrics() -> [MetricData]:
152-
return _db_in_use().metrics()
151+
def metrics(after_runid: int = -1) -> [MetricData]:
152+
return _db_in_use().metrics(after_runid)
153153

154154

155155
def next():

Python/dawgie/db/post.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ def gather(anz, ans):
14991499
return Interface(anz, ans, '__all__')
15001500

15011501

1502-
def metrics() -> '[dawgie.db.MetricData]':
1502+
def metrics(after_runid: int = -1) -> '[dawgie.db.MetricData]':
15031503
if not dawgie.db.post._db:
15041504
raise RuntimeError('called metrics before open')
15051505

@@ -1508,7 +1508,15 @@ def metrics() -> '[dawgie.db.MetricData]':
15081508
cur = dawgie.db.post._cur(conn)
15091509
cur.execute('SELECT PK from StateVector WHERE name = %s;', ('__metric__',))
15101510
sv_IDs = [t[0] for t in cur.fetchall()]
1511-
cur.execute('SELECT * from Prime WHERE sv_ID = ANY(%s);', (sv_IDs,))
1511+
if after_runid is None or after_runid < 0:
1512+
cur.execute('SELECT * from Prime WHERE sv_ID = ANY(%s);', (sv_IDs,))
1513+
else:
1514+
cur.execute(
1515+
'SELECT * from Prime WHERE sv_ID = ANY(%s) '
1516+
'AND (run_ID > %s OR run_ID = %s);',
1517+
(sv_IDs, after_runid, 0),
1518+
)
1519+
15121520
rows = cur.fetchall()
15131521
md = dawgie.util.metrics.filled(-2)
15141522
svs = {

Python/dawgie/db/shelve/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ def gather(anz, ans) -> dawgie.Aspect:
211211
return Interface(anz, ans, '__all__')
212212

213213

214-
def metrics() -> [dawgie.db.MetricData]:
214+
def metrics(after_runid: int = -1) -> [dawgie.db.MetricData]:
215+
# pylint: disable=too-many-locals
215216
if not DBI().is_open:
216217
raise RuntimeError('called metrics before open')
217218
if DBI().is_reopened:
@@ -260,14 +261,17 @@ def metrics() -> [dawgie.db.MetricData]:
260261
mn = '.'.join([str(runid), target, task, alg[0], sv[0], val[0]])
261262
log.debug('metrics() - working on %s', mn)
262263

263-
if not result or any(
264-
[
265-
result[-1].run_id != runid,
266-
result[-1].target != target,
267-
result[-1].task != task,
268-
result[-1].alg_name != alg[0],
269-
result[-1].alg_ver != alg[1].version,
270-
]
264+
if runid > after_runid and (
265+
not result
266+
or any(
267+
[
268+
result[-1].run_id != runid,
269+
result[-1].target != target,
270+
result[-1].task != task,
271+
result[-1].alg_name != alg[0],
272+
result[-1].alg_ver != alg[1].version,
273+
]
274+
)
271275
):
272276
log.debug('metrics() - make new reuslt')
273277
msv = dawgie.util.MetricStateVector(

Python/dawgie/pl/resources.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _read_diary():
6666
):
6767
for timelines in past.values():
6868
if not all(
69-
isinstance(k, dict) and isinstance(v, (list, set))
69+
isinstance(k, str) and isinstance(v, (list, set))
7070
for k, v in timelines.items()
7171
):
7272
past = {}
@@ -128,6 +128,14 @@ def distribution(metric: [dawgie.db.MetricData]) -> {str: HINT}:
128128
return dst
129129

130130

131+
def last_runid() -> int:
132+
reg = _read_diary()
133+
runids = {-1}
134+
for val in reg.values():
135+
runids.update(val['rids'])
136+
return max(runids)
137+
138+
131139
def regress(metric: [dawgie.db.MetricData]) -> {str: [dawgie.db.MetricData]}:
132140
log.debug('regress() - regress back across metric data %d', len(metric))
133141
keys = [

Python/dawgie/pl/state.dot

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,30 @@ POSSIBILITY OF SUCH DAMAGE.
3838
NTR:
3939
*/
4040
node [shape = circle];
41-
starting[id=starting]
41+
starting[id=starting, label=start]
4242
loading[id=loading]
43+
contemplation[id=contemplation, label="navel\ngazing"]
4344
updating[id=updating]
4445
running[id=running]
4546
gitting[id=gitting]
4647
archiving[id=archiving];
47-
{ rank=same loading updating }
48+
{ rank=same contemplation loading updating }
4849
rankdir=TB;
49-
starting -> loading [label=start,
50+
starting -> loading [label=boot,
5051
trigger=starting_trigger,
5152
source=starting,
5253
dest=loading,
5354
before=start,
5455
after=load];
55-
loading -> running[label=run,
56-
trigger=running_trigger,
57-
source=loading,
58-
dest=running,
59-
before=navel_gaze];
56+
loading -> contemplation[label=introspect,
57+
trigger=contemplation_trigger,
58+
source=loading,
59+
dest=contemplation,
60+
after=navel_gaze];
61+
contemplation -> running[label=run,
62+
trigger=running_trigger,
63+
source=contemplation,
64+
dest=running];
6065
running -> gitting[label=submit,
6166
trigger=gitting_trigger,
6267
source=running,
@@ -74,14 +79,13 @@ NTR:
7479
archiving -> running[label=run,
7580
trigger=running_trigger,
7681
source=archiving,
77-
dest=running,
78-
before=navel_gaze];
82+
dest=running];
7983
running -> updating[label=update,
8084
trigger=update_trigger,
8185
source=running,
8286
dest=updating,
8387
after=reload];
84-
updating -> loading[label=load,
88+
updating -> loading[label=refresh,
8589
trigger=loading_trigger,
8690
source=updating,
8791
dest=loading,

Python/dawgie/pl/state.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class FSM:
142142
args = None
143143
states = [
144144
'archiving',
145+
'contemplation',
145146
'gitting',
146147
'loading',
147148
'running',
@@ -281,11 +282,13 @@ def _logging(self):
281282

282283
def _navel_gaze(self, *_args, **_kwds):
283284
log.info('entering state navel gaze')
284-
dawgie.pl.farm.insights = dawgie.pl.resources.distribution(
285-
dawgie.db.metrics()
286-
)
285+
if not self.__doctest:
286+
dawgie.pl.farm.insights = dawgie.pl.resources.distribution(
287+
dawgie.db.metrics(dawgie.pl.resources.last_runid())
288+
)
287289
log.info('exiting state navel gaze')
288290
self.transitioning = Status.active
291+
self.running_trigger()
289292
return
290293

291294
def _pipeline(self, *_args, **_kwds):
@@ -408,7 +411,7 @@ def is_todo_done(self):
408411
def load(self):
409412
def done(*_args, **_kwds):
410413
self.transitioning = Status.active
411-
self.running_trigger()
414+
self.contemplation_trigger()
412415

413416
log.info('entering state loading')
414417

@@ -434,9 +437,14 @@ def done(*_args, **_kwds):
434437
return
435438

436439
def navel_gaze(self):
437-
self.transitioning = Status.exiting
438-
d = twisted.internet.threads.deferToThread(self._navel_gaze, 2)
439-
d.addErrback(dawgie.pl.LogFailure('while navel gazing', __name__).log)
440+
self.transitioning = Status.entering
441+
if self.__doctest:
442+
self._navel_gaze()
443+
else:
444+
d = twisted.internet.threads.deferToThread(self._navel_gaze, 2)
445+
d.addErrback(
446+
dawgie.pl.LogFailure('while navel gazing', __name__).log
447+
)
440448
return
441449

442450
def reload(self):

Test/test_21.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def test_archiving(self):
6161
)
6262
with self.assertRaises(MachineError):
6363
dawgie.context.fsm.archiving_trigger()
64+
with self.assertRaises(MachineError):
65+
dawgie.context.fsm.contemplation_trigger()
6466
with self.assertRaises(MachineError):
6567
dawgie.context.fsm.gitting_trigger()
6668
with self.assertRaises(MachineError):
@@ -83,6 +85,8 @@ def test_gitting(self):
8385
)
8486
with self.assertRaises(MachineError):
8587
dawgie.context.fsm.archiving_trigger()
88+
with self.assertRaises(MachineError):
89+
dawgie.context.fsm.contemplation_trigger()
8690
with self.assertRaises(MachineError):
8791
dawgie.context.fsm.gitting_trigger()
8892
with self.assertRaises(MachineError):
@@ -106,13 +110,15 @@ def test_loading(self):
106110
dawgie.context.fsm.gitting_trigger()
107111
with self.assertRaises(MachineError):
108112
dawgie.context.fsm.loading_trigger()
113+
with self.assertRaises(MachineError):
114+
dawgie.context.fsm.running_trigger()
109115
with self.assertRaises(MachineError):
110116
dawgie.context.fsm.starting_trigger()
111117
with self.assertRaises(MachineError):
112118
dawgie.context.fsm.update_trigger()
113119
with self.assertRaises(MachineError):
114120
dawgie.context.fsm.updating_trigger()
115-
dawgie.context.fsm.running_trigger()
121+
dawgie.context.fsm.contemplation_trigger()
116122
self.assertEqual('running', dawgie.context.fsm.state)
117123

118124
def test_running(self):
@@ -146,6 +152,8 @@ def test_starting(self):
146152
)
147153
with self.assertRaises(MachineError):
148154
dawgie.context.fsm.archiving_trigger()
155+
with self.assertRaises(MachineError):
156+
dawgie.context.fsm.contemplation_trigger()
149157
with self.assertRaises(MachineError):
150158
dawgie.context.fsm.gitting_trigger()
151159
with self.assertRaises(MachineError):
@@ -163,6 +171,8 @@ def test_updating(self):
163171
dawgie.context.fsm = dawgie.pl.state.FSM(
164172
doctest_=True, initial_state='updating'
165173
)
174+
with self.assertRaises(MachineError):
175+
dawgie.context.fsm.contemplation_trigger()
166176
with self.assertRaises(MachineError):
167177
dawgie.context.fsm.gitting_trigger()
168178
with self.assertRaises(MachineError):

0 commit comments

Comments
 (0)