Source code orco/runtime.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import logging
import pickle
import threading
import time

from .builder import Builder, BuilderProxy
from .internals.database import Database, JobState
from .internals.executor import Executor
from .internals.key import make_key
from .internals.plan import Plan
from .internals.runner import JobRunner
from .internals.utils import make_repr
from .job import Job

logger = logging.getLogger(__name__)


def _check_unattached_job(obj, reattach):
    if not isinstance(obj, Job):
        raise Exception("'{!r}' is not an job".format(obj))
    if reattach:
        obj.detach()
    elif obj.is_attached():
        raise Exception("'{!r}' is already attached".format(obj))


class Runtime:
    """
    Core class of ORCO.

    It manages database with results and starts computations

    For SQLite:

    >>> runtime = Runtime("sqlite:///path/to/dbfile.db")

    For Postgress:

    >>> runtime = Runtime("postgresql://<USERNAME>:<PASSWORD>@<HOSTNAME>/<DATABASE>")
    """

    def __init__(
        self, db_path: str, global_builders=True, executor_name=None, n_processes=None
    ):
        self.db = Database(db_path)
        self.db.init()

        self._builders = {}
        self._lock = threading.Lock()

        self.executor = None
        self.stopped = False
        self.executor_args = {
            "name": executor_name,
            "n_processes": n_processes,
        }
        self.runners = {}

        logging.debug("Starting runtime %s (db=%s)", self, db_path)

        if global_builders:
            from .globals import _get_global_builders

            for builder in _get_global_builders():
                logging.debug("Registering global builder %s", builder.name)
                self.register_builder(builder)

    def __enter__(self):
        self._check_stopped()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if not self.stopped:
            self.stop()

    def add_runner(self, name, runner):
        if self.executor:
            raise Exception("Runners cannot be added when the executor is started")
        assert isinstance(runner, JobRunner)
        assert isinstance(name, str)
        if name in self.runners:
            raise Exception("Runner under name '{}' is already registered".format(name))
        self.runners[name] = runner

    def stop(self):
        self._check_stopped()

        logger.debug("Stopping runtime %s", self)
        self.stop_executor()
        self.db.stop()
        self.stopped = True

    def start_executor(self):
        if self.executor:
            raise Exception("Executor is already dunning")
        logger.debug("Registering executor %s")
        executor = Executor(self, self.runners, **self.executor_args)
        executor.start()
        self.executor = executor
        return executor

    def stop_executor(self):
        if self.executor is not None:
            logger.debug("Unregistering executor %s", self.executor)
            self.executor.stop()
            self.executor = None

    def register_builder(self, builder: Builder):
        assert isinstance(builder, Builder)
        name = builder.name
        if name in self._builders:
            raise Exception("Builder '{}' is already registered".format(name))
        # self.db.ensure_builder(name)
        self._builders[name] = builder
        return builder.make_proxy()

    def serve(self, port=8550, debug=False, testing=False, nonblocking=False):
        print("Running ORCO browser on port {}".format(port))
        from .internals.browser import init_service

        app = init_service(self)
        if testing:
            app.testing = True
            return app
        else:

            def run_app():
                app.run(port=port, debug=debug, use_reloader=False)

            if nonblocking:
                t = threading.Thread(target=run_app)
                t.daemon = True
                t.start()
                return t
            else:
                run_app()

    def get_state(self, job):
        return self.db.get_active_state(job.key)

    def read(self, job, *, reattach=False):
        r = self.try_read(job, reattach)
        if r is None:
            raise Exception("No finished job for {}".format(job))
        return r

    def try_read(self, job, reattach=False):
        _check_unattached_job(job, reattach)
        job_id, state = self.db.get_active_job_id_and_state(job.key)
        if state != JobState.FINISHED:
            return None
        job.set_job_id(job_id, self.db, state)
        return job

    def read_jobs(self, job):
        return self.db.read_jobs(job.key, job.builder_name)

    def read_many(self, jobs, *, reattach=False, drop_missing=False):
        # TODO: Read from DB in one query
        results = []
        for job in jobs:
            if self.try_read(job, reattach):
                results.append(job)
            elif not drop_missing:
                results.append(None)
        return results

    def drop(self, job, drop_inputs=False):
        return self.drop_many([job], drop_inputs)

    def drop_many(self, jobs, drop_inputs=False):
        self.db.drop_jobs_by_key([job.key for job in jobs], drop_inputs)

    def archive(self, job, archive_inputs=False):
        self.archive_many([job], archive_inputs=archive_inputs)

    def archive_many(self, jobs, archive_inputs=False):
        for job in jobs:
            _check_unattached_job(job, False)
        self.db.archive_jobs_by_key([job.key for job in jobs], archive_inputs)

    def free(self, job):
        self.free_many([job])

    def free_many(self, jobs):
        for job in jobs:
            _check_unattached_job(job, False)
        self.db.free_jobs_by_key([job.key for job in jobs])

    def insert(self, job, value):
        r = self.db.create_job_with_value(
            job.builder_name, job.key, job.config, pickle.dumps(value), make_repr(value)
        )
        if not r:
            raise Exception("Job {} already exists".format(job))

    def drop_builder(self, builder_name, drop_inputs=False):
        assert isinstance(builder_name, str)
        self.db.drop_builder(builder_name, drop_inputs)

    def compute(self, job, *, reattach=False, continue_on_error=False):
        self._compute((job,), reattach, continue_on_error)
        return job

    def compute_many(self, jobs, *, reattach=False, continue_on_error=False):
        self._compute(jobs, reattach, continue_on_error)
        return jobs

    def has_builder(self, builder_name):
        return builder_name in self._builders

    def get_builder(self, builder_name):
        return self._builders[builder_name]

    def upgrade_builder(self, builder, upgrade_fn):
        if isinstance(builder, BuilderProxy) or isinstance(builder, BuilderProxy):
            builder_name = builder.name
        else:
            builder_name = builder
        configs = self.db.get_all_configs(builder_name)
        to_update = []
        keys = set()
        for key, config in configs:
            config = upgrade_fn(config)
            new_key = make_key(builder_name, config)
            if new_key in keys:
                raise Exception(
                    "Key collision in upgrade, config={}".format(repr(config))
                )
            if new_key != key:
                to_update.append({"key": key, "new_key": new_key, "config": config})
            keys.add(new_key)
        self.db.upgrade_builder(to_update)

    def _check_stopped(self):
        if self.stopped:
            raise Exception("Runtime was already stopped")

    def _run_computation(self, plan):
        executor = self.executor
        plan.create(self)
        if plan.is_finished():
            return "finished"
        if plan.need_wait():
            print("Waiting for computation on another executor ...")
            return "wait"
        logger.debug("Announcing jobs %s at executor %s", len(plan.nodes), executor.id)
        if not self.db.announce_jobs(plan):
            return "wait"
        try:
            if plan.conflicts:
                print(
                    "Some computation was temporarily skipped as they depends on jobs computed by another executor"
                )
            plan.print_report(self)
            executor.run(plan)
            if plan.is_finished():
                return "finished"
            else:
                return "next"
        except:
            self.db.unannounce_jobs(plan)
            plan.fill_job_ids(self, False)
            raise

    def _compute(self, jobs, reattach, continue_on_error):
        for job in jobs:
            _check_unattached_job(job, reattach)

        if self.executor is None:
            self.start_executor()

        plan = Plan(jobs, continue_on_error)

        while True:
            status = self._run_computation(plan)
            if status == "finished":
                break
            elif status == "next":
                continue
            elif status == "wait":
                time.sleep(1)
                continue
            else:
                assert 0
        plan.fill_job_ids(self, not continue_on_error)
        if plan.error_keys:
            print(
                "During computation, {} errors occured, see reports for details".format(
                    len(plan.error_keys)
                )
            )