|
18 | 18 | Helper classes for the dispatcher
|
19 | 19 | """
|
20 | 20 |
|
21 |
| -from .store import _DictStore, _KeyValueBase |
| 21 | +import json |
| 22 | +import os |
| 23 | +import tempfile |
22 | 24 |
|
| 25 | +from covalent_dispatcher._core.data_modules.utils import run_in_executor |
| 26 | +from covalent_dispatcher._dal.base import workflow_db |
| 27 | +from covalent_dispatcher._dal.dispatcher_state import TaskGroupState, WorkflowState |
| 28 | +from covalent_dispatcher._db.datastore import DataStore |
23 | 29 |
|
24 |
| -def _pending_parents_key(dispatch_id: str, node_id: int): |
25 |
| - return f"pending-parents-{dispatch_id}:{node_id}" |
26 | 30 |
|
| 31 | +class _WorkflowRunState: |
27 | 32 |
|
28 |
| -def _unresolved_tasks_key(dispatch_id: str): |
29 |
| - return f"unresolved-{dispatch_id}" |
| 33 | + def __init__(self, db: DataStore): |
| 34 | + self.db = db |
30 | 35 |
|
31 |
| - |
32 |
| -def _task_groups_key(dispatch_id: str, task_group_id: int): |
33 |
| - return f"task-groups-{dispatch_id}:{task_group_id}" |
34 |
| - |
35 |
| - |
36 |
| -class _UnresolvedTasksCache: |
37 |
| - def __init__(self, store: _KeyValueBase = _DictStore()): |
38 |
| - self._store = store |
| 36 | + def _get_unresolved(self, dispatch_id: str): |
| 37 | + with self.db.session() as session: |
| 38 | + records = WorkflowState.get( |
| 39 | + session, |
| 40 | + fields=["num_unresolved_tasks"], |
| 41 | + equality_filters={"dispatch_id": dispatch_id}, |
| 42 | + membership_filters={}, |
| 43 | + ) |
| 44 | + return records[0].num_unresolved_tasks |
39 | 45 |
|
40 | 46 | async def get_unresolved(self, dispatch_id: str):
|
41 |
| - key = _unresolved_tasks_key(dispatch_id) |
42 |
| - return await self._store.get(key) |
| 47 | + return await run_in_executor(self._get_unresolved, dispatch_id) |
| 48 | + |
| 49 | + def _set_unresolved(self, dispatch_id: str, val: int): |
| 50 | + with self.db.session() as session: |
| 51 | + WorkflowState.create( |
| 52 | + session, |
| 53 | + insert_kwargs={ |
| 54 | + "dispatch_id": dispatch_id, |
| 55 | + "num_unresolved_tasks": val, |
| 56 | + }, |
| 57 | + ) |
| 58 | + session.commit() |
43 | 59 |
|
44 | 60 | async def set_unresolved(self, dispatch_id: str, val: int):
|
45 |
| - key = _unresolved_tasks_key(dispatch_id) |
46 |
| - await self._store.insert(key, val) |
| 61 | + return await run_in_executor(self._set_unresolved, dispatch_id, val) |
| 62 | + |
| 63 | + def _increment(self, dispatch_id: str, interval: int = 1): |
| 64 | + with self.db.session() as session: |
| 65 | + WorkflowState.incr_bulk( |
| 66 | + session=session, |
| 67 | + increments={"num_unresolved_tasks": interval}, |
| 68 | + equality_filters={"dispatch_id": dispatch_id}, |
| 69 | + membership_filters={}, |
| 70 | + ) |
| 71 | + records = WorkflowState.get( |
| 72 | + session, |
| 73 | + fields=["num_unresolved_tasks"], |
| 74 | + equality_filters={"dispatch_id": dispatch_id}, |
| 75 | + membership_filters={}, |
| 76 | + ) |
| 77 | + session.commit() |
| 78 | + return records[0].num_unresolved_tasks |
47 | 79 |
|
48 | 80 | async def increment(self, dispatch_id: str, interval: int = 1):
|
49 |
| - key = _unresolved_tasks_key(dispatch_id) |
50 |
| - return await self._store.increment(key, interval) |
| 81 | + return await run_in_executor(self._increment, dispatch_id, interval) |
| 82 | + |
| 83 | + def _decrement(self, dispatch_id: str): |
| 84 | + with self.db.session() as session: |
| 85 | + WorkflowState.incr_bulk( |
| 86 | + session=session, |
| 87 | + increments={"num_unresolved_tasks": -1}, |
| 88 | + equality_filters={"dispatch_id": dispatch_id}, |
| 89 | + membership_filters={}, |
| 90 | + ) |
| 91 | + records = WorkflowState.get( |
| 92 | + session, |
| 93 | + fields=["num_unresolved_tasks"], |
| 94 | + equality_filters={"dispatch_id": dispatch_id}, |
| 95 | + membership_filters={}, |
| 96 | + ) |
| 97 | + session.commit() |
| 98 | + return records[0].num_unresolved_tasks |
51 | 99 |
|
52 | 100 | async def decrement(self, dispatch_id: str):
|
53 |
| - key = _unresolved_tasks_key(dispatch_id) |
54 |
| - return await self._store.increment(key, -1) |
| 101 | + return await run_in_executor(self._decrement, dispatch_id) |
| 102 | + |
| 103 | + def _remove(self, dispatch_id: str): |
| 104 | + with self.db.session() as session: |
| 105 | + WorkflowState.delete_bulk( |
| 106 | + session=session, |
| 107 | + equality_filters={"dispatch_id": dispatch_id}, |
| 108 | + membership_filters={}, |
| 109 | + ) |
| 110 | + session.commit() |
55 | 111 |
|
56 | 112 | async def remove(self, dispatch_id: str):
|
57 |
| - key = _unresolved_tasks_key(dispatch_id) |
58 |
| - await self._store.remove(key) |
| 113 | + await run_in_executor(self._remove, dispatch_id) |
59 | 114 |
|
60 | 115 |
|
61 |
| -class _PendingParentsCache: |
62 |
| - def __init__(self, store: _KeyValueBase = _DictStore()): |
63 |
| - self._store = store |
| 116 | +class TaskGroupRunState: |
64 | 117 |
|
65 |
| - async def get_pending(self, dispatch_id: str, task_group_id: int): |
66 |
| - key = _pending_parents_key(dispatch_id, task_group_id) |
67 |
| - return await self._store.get(key) |
| 118 | + def __init__(self, db): |
| 119 | + self.db = db |
| 120 | + |
| 121 | + def _get_pending(self, dispatch_id: str, task_group_id: int): |
| 122 | + with self.db.session() as session: |
| 123 | + records = TaskGroupState.get( |
| 124 | + session=session, |
| 125 | + fields=["num_pending_parents"], |
| 126 | + equality_filters={"dispatch_id": dispatch_id, "task_group_id": task_group_id}, |
| 127 | + membership_filters={}, |
| 128 | + ) |
| 129 | + return records[0].num_pending_parents |
68 | 130 |
|
69 |
| - async def set_pending(self, dispatch_id: str, task_group_id: int, val: int): |
70 |
| - key = _pending_parents_key(dispatch_id, task_group_id) |
71 |
| - await self._store.insert(key, val) |
| 131 | + async def get_pending(self, dispatch_id: str, task_group_id: int): |
| 132 | + return await run_in_executor(self._get_pending, dispatch_id, task_group_id) |
| 133 | + |
| 134 | + def _set(self, dispatch_id: str, task_group_id: int, num_pending: int, sorted_nodes): |
| 135 | + with self.db.session() as session: |
| 136 | + TaskGroupState.create( |
| 137 | + session=session, |
| 138 | + insert_kwargs={ |
| 139 | + "dispatch_id": dispatch_id, |
| 140 | + "task_group_id": task_group_id, |
| 141 | + "num_pending_parents": num_pending, |
| 142 | + "sorted_tasks": json.dumps(sorted_nodes), |
| 143 | + }, |
| 144 | + ) |
| 145 | + session.commit() |
| 146 | + |
| 147 | + async def set(self, dispatch_id: str, task_group_id: int, num_pending: int, sorted_nodes): |
| 148 | + return await run_in_executor( |
| 149 | + self._set, dispatch_id, task_group_id, num_pending, sorted_nodes |
| 150 | + ) |
| 151 | + |
| 152 | + def _decrement(self, dispatch_id: str, task_group_id): |
| 153 | + with self.db.session() as session: |
| 154 | + TaskGroupState.incr_bulk( |
| 155 | + session=session, |
| 156 | + increments={"num_pending_parents": -1}, |
| 157 | + equality_filters={"dispatch_id": dispatch_id, "task_group_id": task_group_id}, |
| 158 | + membership_filters={}, |
| 159 | + ) |
| 160 | + records = TaskGroupState.get( |
| 161 | + session, |
| 162 | + fields=["num_pending_parents"], |
| 163 | + equality_filters={"dispatch_id": dispatch_id, "task_group_id": task_group_id}, |
| 164 | + membership_filters={}, |
| 165 | + ) |
| 166 | + session.commit() |
| 167 | + return records[0].num_pending_parents |
72 | 168 |
|
73 | 169 | async def decrement(self, dispatch_id: str, task_group_id: int):
|
74 |
| - key = _pending_parents_key(dispatch_id, task_group_id) |
75 |
| - return await self._store.increment(key, -1) |
| 170 | + return await run_in_executor(self._decrement, dispatch_id, task_group_id) |
76 | 171 |
|
77 | 172 | async def remove(self, dispatch_id: str, task_group_id: int):
|
78 |
| - key = _pending_parents_key(dispatch_id, task_group_id) |
79 |
| - await self._store.remove(key) |
80 |
| - |
81 |
| - |
82 |
| -class _SortedTaskGroups: |
83 |
| - def __init__(self, store: _KeyValueBase = _DictStore()): |
84 |
| - self._store = store |
| 173 | + pass |
| 174 | + |
| 175 | + def _get_task_group(self, dispatch_id: str, task_group_id: int): |
| 176 | + with self.db.session() as session: |
| 177 | + records = TaskGroupState.get( |
| 178 | + session=session, |
| 179 | + fields=["sorted_tasks"], |
| 180 | + equality_filters={"dispatch_id": dispatch_id, "task_group_id": task_group_id}, |
| 181 | + membership_filters={}, |
| 182 | + ) |
| 183 | + return json.loads(records[0].sorted_tasks) |
85 | 184 |
|
86 | 185 | async def get_task_group(self, dispatch_id: str, task_group_id: int):
|
87 |
| - key = _task_groups_key(dispatch_id, task_group_id) |
88 |
| - return await self._store.get(key) |
| 186 | + return await run_in_executor(self._get_task_group, dispatch_id, task_group_id) |
89 | 187 |
|
90 |
| - async def set_task_group(self, dispatch_id: str, task_group_id: int, sorted_nodes: list): |
91 |
| - key = _task_groups_key(dispatch_id, task_group_id) |
92 |
| - await self._store.insert(key, sorted_nodes) |
| 188 | + def _remove(self, dispatch_id: str, task_group_id: int): |
| 189 | + with self.db.session() as session: |
| 190 | + TaskGroupState.delete_bulk( |
| 191 | + session=session, |
| 192 | + equality_filters={"dispatch_id": dispatch_id, "task_group_id": task_group_id}, |
| 193 | + membership_filters={}, |
| 194 | + ) |
| 195 | + session.commit() |
93 | 196 |
|
94 | 197 | async def remove(self, dispatch_id: str, task_group_id: int):
|
95 |
| - key = _task_groups_key(dispatch_id, task_group_id) |
96 |
| - await self._store.remove(key) |
| 198 | + await run_in_executor(self._remove, dispatch_id, task_group_id) |
| 199 | + |
| 200 | + |
| 201 | +# Default to tmpfs backed file |
| 202 | +cache_db_file = tempfile.NamedTemporaryFile( |
| 203 | + mode="w+b", prefix="covalent-dispatcher-cache-", suffix=".db" |
| 204 | +) |
| 205 | +cache_db_URL = os.environ.get("COVALENT_CACHE_DB_URL", f"sqlite+pysqlite:///{cache_db_file.name}") |
| 206 | +initialize_db = True |
| 207 | + |
| 208 | +# If we want to store dispatcher state in the main DB, let the alembic migrations |
| 209 | +# create the tables |
| 210 | +if cache_db_URL == workflow_db.db_URL: |
| 211 | + initialize_db = False |
97 | 212 |
|
| 213 | +cache_db = DataStore(db_URL=cache_db_URL, initialize_db=initialize_db) |
98 | 214 |
|
99 |
| -_pending_parents = _PendingParentsCache() |
100 |
| -_unresolved_tasks = _UnresolvedTasksCache() |
101 |
| -_sorted_task_groups = _SortedTaskGroups() |
| 215 | +_task_group_cache = TaskGroupRunState(db=cache_db) |
| 216 | +_workflow_run_cache = _WorkflowRunState(db=cache_db) |
0 commit comments