src/Pure/Tools/build_process.scala
author wenzelm
Sat, 11 Mar 2023 11:14:24 +0100
changeset 77604 b4ef44ce08ed
parent 77596 dd8b08729458
child 77606 b0a4f8c29446
permissions -rw-r--r--
do not export connection details (password etc.);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/build_process.scala
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     3
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     4
Build process for sessions, with build database, optional heap, and
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     5
optional presentation.
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     6
*/
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     7
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     8
package isabelle
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     9
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    10
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
    11
import scala.collection.immutable.SortedMap
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    12
import scala.math.Ordering
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
    13
import scala.annotation.tailrec
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    14
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    15
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    16
object Build_Process {
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
    17
  /** static context **/
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    18
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    19
  object Context {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    20
    def apply(
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    21
      store: Sessions.Store,
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
    22
      build_deps: Sessions.Deps,
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    23
      progress: Progress = new Progress,
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
    24
      ml_platform: String = Isabelle_System.getenv("ML_PLATFORM"),
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
    25
      hostname: String = Isabelle_System.hostname(),
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
    26
      numa_shuffling: Boolean = false,
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    27
      build_heap: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    28
      max_jobs: Int = 1,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    29
      fresh_build: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    30
      no_build: Boolean = false,
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
    31
      session_setup: (String, Session) => Unit = (_, _) => (),
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
    32
      build_uuid: String = UUID.random().toString,
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
    33
      master: Boolean = false,
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    34
    ): Context = {
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
    35
      val sessions_structure = build_deps.sessions_structure
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    36
      val build_graph = sessions_structure.build_graph
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    37
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    38
      val sessions =
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    39
        Map.from(
77448
wenzelm
parents: 77447
diff changeset
    40
          for ((name, (info, _)) <- build_graph.iterator)
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    41
          yield {
77447
566e6e393126 proper deps from build_graph, not imports_graph (amending 0c704aba71e3);
wenzelm
parents: 77446
diff changeset
    42
            val deps = info.parent.toList
566e6e393126 proper deps from build_graph, not imports_graph (amending 0c704aba71e3);
wenzelm
parents: 77446
diff changeset
    43
            val ancestors = sessions_structure.build_requirements(deps)
77458
403748b23f13 clarified signature: prefer static data;
wenzelm
parents: 77457
diff changeset
    44
            val sources_shasum = build_deps.sources_shasum(name)
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
    45
            val session_context =
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
    46
              Build_Job.Session_Context.load(
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
    47
                build_uuid, name, deps, ancestors, sources_shasum, info.timeout, store,
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
    48
                progress = progress)
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
    49
            name -> session_context
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    50
          })
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    51
77248
wenzelm
parents: 77247
diff changeset
    52
      val sessions_time = {
wenzelm
parents: 77247
diff changeset
    53
        val maximals = build_graph.maximals.toSet
wenzelm
parents: 77247
diff changeset
    54
        def descendants_time(name: String): Double = {
wenzelm
parents: 77247
diff changeset
    55
          if (maximals.contains(name)) sessions(name).old_time.seconds
wenzelm
parents: 77247
diff changeset
    56
          else {
wenzelm
parents: 77247
diff changeset
    57
            val descendants = build_graph.all_succs(List(name)).toSet
wenzelm
parents: 77247
diff changeset
    58
            val g = build_graph.restrict(descendants)
wenzelm
parents: 77247
diff changeset
    59
            (0.0 :: g.maximals.flatMap { desc =>
wenzelm
parents: 77247
diff changeset
    60
              val ps = g.all_preds(List(desc))
wenzelm
parents: 77247
diff changeset
    61
              if (ps.exists(p => !sessions.isDefinedAt(p))) None
wenzelm
parents: 77247
diff changeset
    62
              else Some(ps.map(p => sessions(p).old_time.seconds).sum)
wenzelm
parents: 77247
diff changeset
    63
            }).max
wenzelm
parents: 77247
diff changeset
    64
          }
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    65
        }
77248
wenzelm
parents: 77247
diff changeset
    66
        Map.from(
wenzelm
parents: 77247
diff changeset
    67
          for (name <- sessions.keysIterator)
wenzelm
parents: 77247
diff changeset
    68
          yield name -> descendants_time(name)).withDefaultValue(0.0)
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    69
      }
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    70
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    71
      val ordering =
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    72
        new Ordering[String] {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    73
          def compare(name1: String, name2: String): Int =
77248
wenzelm
parents: 77247
diff changeset
    74
            sessions_time(name2) compare sessions_time(name1) match {
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    75
              case 0 =>
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    76
                sessions(name2).timeout compare sessions(name1).timeout match {
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    77
                  case 0 => name1 compare name2
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    78
                  case ord => ord
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    79
                }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    80
              case ord => ord
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    81
            }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    82
        }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    83
77477
f376aebca9c1 clarified modules;
wenzelm
parents: 77476
diff changeset
    84
      val numa_nodes = Host.numa_nodes(enabled = numa_shuffling)
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
    85
      new Context(store, build_deps, sessions, ordering, ml_platform, hostname, numa_nodes,
77317
b8ec3c0455db clarified modules: NUMA is managed by Build_Process;
wenzelm
parents: 77315
diff changeset
    86
        build_heap = build_heap, max_jobs = max_jobs, fresh_build = fresh_build,
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
    87
        no_build = no_build, session_setup, build_uuid = build_uuid, master = master)
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    88
    }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    89
  }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    90
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    91
  final class Context private(
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
    92
    val store: Sessions.Store,
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
    93
    val build_deps: Sessions.Deps,
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
    94
    val sessions: State.Sessions,
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
    95
    val ordering: Ordering[String],
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
    96
    val ml_platform: String,
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
    97
    val hostname: String,
77317
b8ec3c0455db clarified modules: NUMA is managed by Build_Process;
wenzelm
parents: 77315
diff changeset
    98
    val numa_nodes: List[Int],
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    99
    val build_heap: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   100
    val max_jobs: Int,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   101
    val fresh_build: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   102
    val no_build: Boolean,
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
   103
    val session_setup: (String, Session) => Unit,
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
   104
    val build_uuid: String,
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
   105
    val master: Boolean
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   106
  ) {
77561
a24d77f2cfe9 tuned output;
wenzelm
parents: 77552
diff changeset
   107
    override def toString: String =
a24d77f2cfe9 tuned output;
wenzelm
parents: 77552
diff changeset
   108
      "Build_Process.Context(build_uuid = " + quote(build_uuid) + ")"
a24d77f2cfe9 tuned output;
wenzelm
parents: 77552
diff changeset
   109
77470
38503d9ff2e5 clarified modules;
wenzelm
parents: 77469
diff changeset
   110
    def build_options: Options = store.options
38503d9ff2e5 clarified modules;
wenzelm
parents: 77469
diff changeset
   111
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
   112
    def sessions_structure: Sessions.Structure = build_deps.sessions_structure
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   113
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   114
    def sources_shasum(name: String): SHA1.Shasum = sessions(name).sources_shasum
77458
403748b23f13 clarified signature: prefer static data;
wenzelm
parents: 77457
diff changeset
   115
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   116
    def old_command_timings(name: String): List[Properties.T] =
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   117
      sessions.get(name) match {
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   118
        case Some(session_context) =>
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   119
          Properties.uncompress(session_context.old_command_timings_blob, cache = store.cache)
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   120
        case None => Nil
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   121
      }
77439
d6bf9ec39d12 avoid premature Properties.uncompress: allow blob to be stored in another database;
wenzelm
parents: 77438
diff changeset
   122
77514
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   123
    def open_database(): Option[SQL.Database] =
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   124
      if (!build_options.bool("build_database_test")) None
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   125
      else if (store.database_server) Some(store.open_database_server())
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   126
      else {
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   127
        val db = SQLite.open_database(Build_Process.Data.database)
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   128
        try { Isabelle_System.chmod("600", Build_Process.Data.database) }
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   129
        catch { case exn: Throwable => db.close(); throw exn }
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   130
        Some(db)
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   131
      }
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   132
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   133
    def prepare_database(): Unit = {
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   134
      using_option(open_database()) { db =>
77539
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   135
        db.transaction {
77596
dd8b08729458 clarified signature;
wenzelm
parents: 77594
diff changeset
   136
          Data.all_tables.create_lock(db)
77539
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   137
          Data.clean_build(db)
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   138
        }
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   139
        db.rebuild()
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   140
      }
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   141
    }
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   142
77463
4e8bec105ce5 tuned signature;
wenzelm
parents: 77462
diff changeset
   143
    def store_heap(name: String): Boolean =
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   144
      build_heap || Sessions.is_pure(name) ||
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   145
      sessions.valuesIterator.exists(_.ancestors.contains(name))
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
   146
77594
7723cf88658f proper test (amending 32f9e75c92e9);
wenzelm
parents: 77590
diff changeset
   147
    def worker_active: Boolean = max_jobs > 0
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   148
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   149
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   150
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   151
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   152
  /** dynamic state **/
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   153
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   154
  type Progress_Messages = SortedMap[Long, Progress.Message]
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   155
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   156
  case class Task(name: String, deps: List[String], info: JSON.Object.T = JSON.Object.empty) {
77313
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   157
    def is_ready: Boolean = deps.isEmpty
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   158
    def resolve(dep: String): Task =
77313
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   159
      if (deps.contains(dep)) copy(deps = deps.filterNot(_ == dep)) else this
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   160
  }
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   161
77583
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   162
  case class Worker(
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   163
    worker_uuid: String,
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   164
    build_uuid: String,
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   165
    hostname: String,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   166
    java_pid: Long,
77588
066d5df144f0 assume total operation: ProcessHandle.current().info.startInstant appears to work on all platforms;
wenzelm
parents: 77587
diff changeset
   167
    java_start: Date,
77583
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   168
    start: Date,
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   169
    stamp: Date,
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   170
    stop: Option[Date],
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   171
    serial: Long)
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   172
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   173
  case class Result(
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   174
    process_result: Process_Result,
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   175
    output_shasum: SHA1.Shasum,
77475
3bc611c80346 clarified modules;
wenzelm
parents: 77474
diff changeset
   176
    node_info: Host.Node_Info,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   177
    current: Boolean
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   178
  ) {
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   179
    def ok: Boolean = process_result.ok
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   180
  }
77312
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   181
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   182
  object State {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   183
    type Sessions = Map[String, Build_Job.Session_Context]
77583
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   184
    type Workers = List[Worker]
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   185
    type Pending = List[Task]
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   186
    type Running = Map[String, Build_Job]
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   187
    type Results = Map[String, Result]
77513
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   188
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   189
    def inc_serial(serial: Long): Long = {
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   190
      require(serial < java.lang.Long.MAX_VALUE, "serial overflow")
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   191
      serial + 1
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   192
    }
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   193
  }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   194
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   195
  sealed case class State(
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   196
    serial: Long = 0,
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   197
    progress_seen: Long = 0,
77525
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   198
    numa_next: Int = 0,
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   199
    sessions: State.Sessions = Map.empty,   // static build targets
77583
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   200
    workers: State.Workers = Nil,           // available worker processes
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   201
    pending: State.Pending = Nil,           // dynamic build "queue"
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   202
    running: State.Running = Map.empty,     // presently running jobs
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   203
    results: State.Results = Map.empty      // finished results
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   204
  ) {
77513
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   205
    require(serial >= 0, "serial underflow")
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   206
    def inc_serial: State = copy(serial = State.inc_serial(serial))
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   207
    def set_serial(i: Long): State = {
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   208
      require(serial <= i, "non-monotonic change of serial")
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   209
      copy(serial = i)
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   210
    }
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   211
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   212
    def progress_serial(message_serial: Long = serial): State =
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   213
      if (message_serial > progress_seen) copy(progress_seen = message_serial)
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   214
      else error("Bad serial " + message_serial + " for progress output (already seen)")
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   215
77583
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   216
    def set_workers(new_workers: State.Workers): State = copy(workers = new_workers)
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   217
77525
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   218
    def numa_next_node(numa_nodes: List[Int]): (Option[Int], State) =
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   219
      if (numa_nodes.isEmpty) (None, this)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   220
      else {
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   221
        val available = numa_nodes.zipWithIndex
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   222
        val used =
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   223
          Set.from(for (job <- running.valuesIterator; i <- job.node_info.numa_node) yield i)
77525
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   224
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   225
        val numa_index = available.collectFirst({ case (n, i) if n == numa_next => i }).getOrElse(0)
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   226
        val candidates = available.drop(numa_index) ::: available.take(numa_index)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   227
        val (n, i) =
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   228
          candidates.find({ case (n, i) => i == numa_index && !used(n) }) orElse
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   229
          candidates.find({ case (n, _) => !used(n) }) getOrElse candidates.head
77525
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   230
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   231
        (Some(n), copy(numa_next = numa_nodes((i + 1) % numa_nodes.length)))
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   232
      }
77337
1ab68d4370ec tuned signature;
wenzelm
parents: 77336
diff changeset
   233
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   234
    def finished: Boolean = pending.isEmpty
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   235
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   236
    def remove_pending(name: String): State =
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   237
      copy(pending = pending.flatMap(
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   238
        entry => if (entry.name == name) None else Some(entry.resolve(name))))
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   239
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   240
    def is_running(name: String): Boolean = running.isDefinedAt(name)
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   241
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77477
diff changeset
   242
    def stop_running(): Unit = running.valuesIterator.foreach(_.cancel())
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   243
77398
19e9cafaafc5 clarified signature: works for general Build_Job;
wenzelm
parents: 77396
diff changeset
   244
    def finished_running(): List[Build_Job] =
19e9cafaafc5 clarified signature: works for general Build_Job;
wenzelm
parents: 77396
diff changeset
   245
      List.from(running.valuesIterator.filter(_.is_finished))
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   246
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   247
    def add_running(name: String, job: Build_Job): State =
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   248
      copy(running = running + (name -> job))
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   249
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   250
    def remove_running(name: String): State =
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   251
      copy(running = running - name)
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   252
77336
e9beaaf955bf tuned signature;
wenzelm
parents: 77335
diff changeset
   253
    def make_result(
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   254
      name: String,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   255
      process_result: Process_Result,
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   256
      output_shasum: SHA1.Shasum,
77475
3bc611c80346 clarified modules;
wenzelm
parents: 77474
diff changeset
   257
      node_info: Host.Node_Info = Host.Node_Info.none,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   258
      current: Boolean = false
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   259
    ): State = {
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   260
      val entry = name -> Build_Process.Result(process_result, output_shasum, node_info, current)
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   261
      copy(results = results + entry)
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   262
    }
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   263
  }
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   264
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   265
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   266
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   267
  /** SQL data model **/
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   268
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   269
  object Data {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   270
    val database = Path.explode("$ISABELLE_HOME_USER/build.db")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   271
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   272
    def make_table(name: String, columns: List[SQL.Column], body: String = ""): SQL.Table =
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   273
      SQL.Table("isabelle_build" + if_proper(name, "_" + name), columns, body = body)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   274
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   275
    object Generic {
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   276
      val build_uuid = SQL.Column.string("build_uuid")
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   277
      val worker_uuid = SQL.Column.string("worker_uuid")
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   278
      val name = SQL.Column.string("name")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   279
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   280
      def sql(
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   281
        build_uuid: String = "",
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   282
        worker_uuid: String = "",
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   283
        name: String = "",
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   284
        names: Iterable[String] = Nil
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   285
      ): SQL.Source =
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   286
        SQL.and(
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   287
          if_proper(build_uuid, Generic.build_uuid.equal(build_uuid)),
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   288
          if_proper(worker_uuid, Generic.worker_uuid.equal(worker_uuid)),
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   289
          if_proper(name, Generic.name.equal(name)),
77375
324f5821a4a4 clarified signature: more concise operations;
wenzelm
parents: 77374
diff changeset
   290
          if_proper(names, Generic.name.member(names)))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   291
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   292
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   293
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   294
    /* base table */
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   295
77417
wenzelm
parents: 77416
diff changeset
   296
    object Base {
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   297
      val build_uuid = Generic.build_uuid.make_primary_key
77387
cd10b8edfdf5 clarified db content: avoid redundancy of historic ML_IDENTIFIER;
wenzelm
parents: 77385
diff changeset
   298
      val ml_platform = SQL.Column.string("ml_platform")
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   299
      val options = SQL.Column.string("options")
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   300
      val start = SQL.Column.date("start")
77545
4af88aca2a4f clarified database content;
wenzelm
parents: 77543
diff changeset
   301
      val stop = SQL.Column.date("stop")
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   302
77545
4af88aca2a4f clarified database content;
wenzelm
parents: 77543
diff changeset
   303
      val table = make_table("", List(build_uuid, ml_platform, options, start, stop))
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   304
    }
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   305
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   306
    def start_build(
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   307
      db: SQL.Database,
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   308
      build_uuid: String,
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   309
      ml_platform: String,
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   310
      options: String
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   311
    ): Unit = {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   312
      db.execute_statement(Base.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   313
        { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   314
          stmt.string(1) = build_uuid
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   315
          stmt.string(2) = ml_platform
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   316
          stmt.string(3) = options
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   317
          stmt.date(4) = db.now()
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   318
          stmt.date(5) = None
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   319
        })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   320
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   321
77545
4af88aca2a4f clarified database content;
wenzelm
parents: 77543
diff changeset
   322
    def stop_build(db: SQL.Database, build_uuid: String): Unit =
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   323
      db.execute_statement(
77545
4af88aca2a4f clarified database content;
wenzelm
parents: 77543
diff changeset
   324
        Base.table.update(List(Base.stop), sql = SQL.where(Generic.sql(build_uuid = build_uuid))),
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   325
        body = { stmt => stmt.date(1) = db.now() })
77538
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   326
77539
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   327
    def clean_build(db: SQL.Database): Unit = {
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   328
      val old =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   329
        db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   330
          Base.table.select(List(Base.build_uuid), sql = SQL.where(Base.stop.defined)),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   331
          List.from[String], res => res.string(Base.build_uuid))
77539
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   332
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   333
      if (old.nonEmpty) {
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   334
        for (table <- List(Base.table, Sessions.table, Progress.table, Workers.table)) {
77540
c537905c2125 tuned signature;
wenzelm
parents: 77539
diff changeset
   335
          db.execute_statement(table.delete(sql = Generic.build_uuid.where_member(old)))
77539
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   336
        }
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   337
      }
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   338
    }
2b996a0df1ce proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents: 77538
diff changeset
   339
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   340
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   341
    /* sessions */
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   342
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   343
    object Sessions {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   344
      val name = Generic.name.make_primary_key
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   345
      val deps = SQL.Column.string("deps")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   346
      val ancestors = SQL.Column.string("ancestors")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   347
      val sources = SQL.Column.string("sources")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   348
      val timeout = SQL.Column.long("timeout")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   349
      val old_time = SQL.Column.long("old_time")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   350
      val old_command_timings = SQL.Column.bytes("old_command_timings")
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   351
      val build_uuid = Generic.build_uuid
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   352
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   353
      val table = make_table("sessions",
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   354
        List(name, deps, ancestors, sources, timeout, old_time, old_command_timings, build_uuid))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   355
    }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   356
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   357
    def read_sessions_domain(db: SQL.Database): Set[String] =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   358
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   359
        Sessions.table.select(List(Sessions.name)),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   360
        Set.from[String], res => res.string(Sessions.name))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   361
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   362
    def read_sessions(db: SQL.Database, names: Iterable[String] = Nil): State.Sessions =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   363
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   364
        Sessions.table.select(sql = if_proper(names, Sessions.name.where_member(names))),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   365
        Map.from[String, Build_Job.Session_Context],
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   366
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   367
          val name = res.string(Sessions.name)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   368
          val deps = split_lines(res.string(Sessions.deps))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   369
          val ancestors = split_lines(res.string(Sessions.ancestors))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   370
          val sources_shasum = SHA1.fake_shasum(res.string(Sessions.sources))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   371
          val timeout = Time.ms(res.long(Sessions.timeout))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   372
          val old_time = Time.ms(res.long(Sessions.old_time))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   373
          val old_command_timings_blob = res.bytes(Sessions.old_command_timings)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   374
          val build_uuid = res.string(Sessions.build_uuid)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   375
          name -> Build_Job.Session_Context(name, deps, ancestors, sources_shasum,
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   376
            timeout, old_time, old_command_timings_blob, build_uuid)
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   377
        }
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   378
      )
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   379
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   380
    def update_sessions(db:SQL.Database, sessions: State.Sessions): Boolean = {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   381
      val old_sessions = read_sessions_domain(db)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   382
      val insert = sessions.iterator.filterNot(p => old_sessions.contains(p._1)).toList
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   383
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   384
      for ((name, session) <- insert) {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   385
        db.execute_statement(Sessions.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   386
          { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   387
            stmt.string(1) = name
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   388
            stmt.string(2) = cat_lines(session.deps)
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   389
            stmt.string(3) = cat_lines(session.ancestors)
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   390
            stmt.string(4) = session.sources_shasum.toString
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   391
            stmt.long(5) = session.timeout.ms
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   392
            stmt.long(6) = session.old_time.ms
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   393
            stmt.bytes(7) = session.old_command_timings_blob
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   394
            stmt.string(8) = session.build_uuid
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   395
          })
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   396
      }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   397
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   398
      insert.nonEmpty
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   399
    }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   400
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   401
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   402
    /* progress */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   403
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   404
    object Progress {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   405
      val serial = SQL.Column.long("serial").make_primary_key
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   406
      val kind = SQL.Column.int("kind")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   407
      val text = SQL.Column.string("text")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   408
      val verbose = SQL.Column.bool("verbose")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   409
      val build_uuid = Generic.build_uuid
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   410
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   411
      val table = make_table("progress", List(serial, kind, text, verbose, build_uuid))
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   412
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   413
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   414
    def read_progress(db: SQL.Database, seen: Long = 0, build_uuid: String = ""): Progress_Messages =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   415
      db.execute_query_statement(
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   416
        Progress.table.select(
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   417
          sql =
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   418
            SQL.where(
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   419
              SQL.and(
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   420
                if (seen <= 0) "" else Progress.serial.ident + " > " + seen,
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   421
                Generic.sql(build_uuid = build_uuid)))),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   422
        SortedMap.from[Long, isabelle.Progress.Message],
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   423
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   424
          val serial = res.long(Progress.serial)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   425
          val kind = isabelle.Progress.Kind(res.int(Progress.kind))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   426
          val text = res.string(Progress.text)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   427
          val verbose = res.bool(Progress.verbose)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   428
          serial -> isabelle.Progress.Message(kind, text, verbose = verbose)
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   429
        }
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   430
      )
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   431
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   432
    def write_progress(
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   433
      db: SQL.Database,
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   434
      message_serial: Long,
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   435
      message: isabelle.Progress.Message,
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   436
      build_uuid: String
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   437
    ): Unit = {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   438
      db.execute_statement(Progress.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   439
        { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   440
          stmt.long(1) = message_serial
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   441
          stmt.int(2) = message.kind.id
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   442
          stmt.string(3) = message.text
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   443
          stmt.bool(4) = message.verbose
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   444
          stmt.string(5) = build_uuid
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   445
        })
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   446
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   447
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   448
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   449
    /* workers */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   450
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   451
    object Workers {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   452
      val worker_uuid = Generic.worker_uuid.make_primary_key
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   453
      val build_uuid = Generic.build_uuid
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   454
      val hostname = SQL.Column.string("hostname")
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   455
      val java_pid = SQL.Column.long("java_pid")
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   456
      val java_start = SQL.Column.date("java_start")
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   457
      val start = SQL.Column.date("start")
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   458
      val stamp = SQL.Column.date("stamp")
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   459
      val stop = SQL.Column.date("stop")
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   460
      val serial = SQL.Column.long("serial")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   461
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   462
      val table = make_table("workers",
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   463
        List(worker_uuid, build_uuid, hostname, java_pid, java_start, start, stamp, stop, serial))
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   464
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   465
      val serial_max = serial.copy(expr = "MAX(" + serial.ident + ")")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   466
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   467
77586
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   468
    def read_workers(
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   469
      db: SQL.Database,
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   470
      build_uuid: String = "",
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   471
      worker_uuid: String = ""
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   472
    ): State.Workers = {
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   473
      db.execute_query_statement(
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   474
        Workers.table.select(sql =
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   475
          SQL.where(Generic.sql(build_uuid = build_uuid, worker_uuid = worker_uuid))),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   476
          List.from[Worker],
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   477
          { res =>
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   478
            Worker(
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   479
              worker_uuid = res.string(Workers.worker_uuid),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   480
              build_uuid = res.string(Workers.build_uuid),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   481
              hostname = res.string(Workers.hostname),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   482
              java_pid = res.long(Workers.java_pid),
77588
066d5df144f0 assume total operation: ProcessHandle.current().info.startInstant appears to work on all platforms;
wenzelm
parents: 77587
diff changeset
   483
              java_start = res.date(Workers.java_start),
77586
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   484
              start = res.date(Workers.start),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   485
              stamp = res.date(Workers.stamp),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   486
              stop = res.get_date(Workers.stop),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   487
              serial = res.long(Workers.serial))
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   488
          })
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   489
    }
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   490
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   491
    def serial_max(db: SQL.Database): Long =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   492
      db.execute_query_statementO[Long](
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   493
        Workers.table.select(List(Workers.serial_max)),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   494
        res => res.long(Workers.serial)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   495
      ).getOrElse(0L)
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   496
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   497
    def start_worker(
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   498
      db: SQL.Database,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   499
      worker_uuid: String,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   500
      build_uuid: String,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   501
      hostname: String,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   502
      java_pid: Long,
77588
066d5df144f0 assume total operation: ProcessHandle.current().info.startInstant appears to work on all platforms;
wenzelm
parents: 77587
diff changeset
   503
      java_start: Date
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   504
    ): Long = {
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   505
      def err(msg: String): Nothing =
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   506
        error("Cannot start worker " + worker_uuid + if_proper(msg, "\n" + msg))
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   507
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   508
      val build_stop =
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   509
        db.execute_query_statementO(
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   510
          Base.table.select(List(Base.stop),
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   511
            sql = SQL.where(Generic.sql(build_uuid = build_uuid))),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   512
          res => res.get_date(Base.stop))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   513
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   514
      build_stop match {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   515
        case Some(None) =>
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   516
        case Some(Some(_)) => err("for already stopped build process " + build_uuid)
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   517
        case None => err("for unknown build process " + build_uuid)
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   518
      }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   519
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   520
      val serial = serial_max(db)
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   521
      db.execute_statement(Workers.table.insert(), body =
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   522
        { stmt =>
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   523
          val now = db.now()
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   524
          stmt.string(1) = worker_uuid
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   525
          stmt.string(2) = build_uuid
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   526
          stmt.string(3) = hostname
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   527
          stmt.long(4) = java_pid
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   528
          stmt.date(5) = java_start
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   529
          stmt.date(6) = now
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   530
          stmt.date(7) = now
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   531
          stmt.date(8) = None
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   532
          stmt.long(9) = serial
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   533
        })
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   534
      serial
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   535
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   536
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   537
    def stamp_worker(
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   538
      db: SQL.Database,
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   539
      worker_uuid: String,
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   540
      serial: Long,
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   541
      stop: Boolean = false
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   542
    ): Unit = {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   543
      val sql =
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   544
        Workers.table.update(List(Workers.stamp, Workers.stop, Workers.serial),
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   545
          sql = SQL.where(Generic.sql(worker_uuid = worker_uuid)))
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   546
      db.execute_statement(sql, body =
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   547
        { stmt =>
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   548
          val now = db.now()
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   549
          stmt.date(1) = now
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   550
          stmt.date(2) = if (stop) Some(now) else None
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   551
          stmt.long(3) = serial
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   552
        })
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   553
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   554
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   555
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   556
    /* pending jobs */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   557
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   558
    object Pending {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   559
      val name = Generic.name.make_primary_key
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   560
      val deps = SQL.Column.string("deps")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   561
      val info = SQL.Column.string("info")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   562
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   563
      val table = make_table("pending", List(name, deps, info))
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   564
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   565
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   566
    def read_pending(db: SQL.Database): List[Task] =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   567
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   568
        Pending.table.select(sql = SQL.order_by(List(Pending.name))),
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   569
        List.from[Task],
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   570
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   571
          val name = res.string(Pending.name)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   572
          val deps = res.string(Pending.deps)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   573
          val info = res.string(Pending.info)
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   574
          Task(name, split_lines(deps), info = JSON.Object.parse(info))
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   575
        })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   576
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   577
    def update_pending(db: SQL.Database, pending: State.Pending): Boolean = {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   578
      val old_pending = read_pending(db)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   579
      val (delete, insert) = Library.symmetric_difference(old_pending, pending)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   580
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   581
      if (delete.nonEmpty) {
77540
c537905c2125 tuned signature;
wenzelm
parents: 77539
diff changeset
   582
        db.execute_statement(
c537905c2125 tuned signature;
wenzelm
parents: 77539
diff changeset
   583
          Pending.table.delete(sql = SQL.where(Generic.sql(names = delete.map(_.name)))))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   584
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   585
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   586
      for (entry <- insert) {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   587
        db.execute_statement(Pending.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   588
          { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   589
            stmt.string(1) = entry.name
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   590
            stmt.string(2) = cat_lines(entry.deps)
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   591
            stmt.string(3) = JSON.Format(entry.info)
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   592
          })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   593
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   594
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   595
      delete.nonEmpty || insert.nonEmpty
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   596
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   597
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   598
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   599
    /* running jobs */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   600
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   601
    object Running {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   602
      val name = Generic.name.make_primary_key
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   603
      val worker_uuid = Generic.worker_uuid
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   604
      val hostname = SQL.Column.string("hostname")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   605
      val numa_node = SQL.Column.int("numa_node")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   606
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   607
      val table = make_table("running", List(name, worker_uuid, hostname, numa_node))
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   608
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   609
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   610
    def read_running(db: SQL.Database): List[Build_Job.Abstract] =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   611
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   612
        Running.table.select(sql = SQL.order_by(List(Running.name))),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   613
        List.from[Build_Job.Abstract],
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   614
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   615
          val name = res.string(Running.name)
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   616
          val worker_uuid = res.string(Running.worker_uuid)
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   617
          val hostname = res.string(Running.hostname)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   618
          val numa_node = res.get_int(Running.numa_node)
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   619
          Build_Job.Abstract(name, worker_uuid, Host.Node_Info(hostname, numa_node))
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   620
        }
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   621
      )
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   622
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   623
    def update_running(db: SQL.Database, running: State.Running): Boolean = {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   624
      val old_running = read_running(db)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   625
      val abs_running = running.valuesIterator.map(_.make_abstract).toList
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   626
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   627
      val (delete, insert) = Library.symmetric_difference(old_running, abs_running)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   628
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   629
      if (delete.nonEmpty) {
77540
c537905c2125 tuned signature;
wenzelm
parents: 77539
diff changeset
   630
        db.execute_statement(
c537905c2125 tuned signature;
wenzelm
parents: 77539
diff changeset
   631
          Running.table.delete(sql = SQL.where(Generic.sql(names = delete.map(_.job_name)))))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   632
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   633
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   634
      for (job <- insert) {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   635
        db.execute_statement(Running.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   636
          { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   637
            stmt.string(1) = job.job_name
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   638
            stmt.string(2) = job.worker_uuid
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   639
            stmt.string(3) = job.node_info.hostname
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   640
            stmt.int(4) = job.node_info.numa_node
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   641
          })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   642
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   643
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   644
      delete.nonEmpty || insert.nonEmpty
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   645
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   646
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   647
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   648
    /* job results */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   649
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   650
    object Results {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   651
      val name = Generic.name.make_primary_key
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   652
      val hostname = SQL.Column.string("hostname")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   653
      val numa_node = SQL.Column.string("numa_node")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   654
      val rc = SQL.Column.int("rc")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   655
      val out = SQL.Column.string("out")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   656
      val err = SQL.Column.string("err")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   657
      val timing_elapsed = SQL.Column.long("timing_elapsed")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   658
      val timing_cpu = SQL.Column.long("timing_cpu")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   659
      val timing_gc = SQL.Column.long("timing_gc")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   660
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   661
      val table =
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   662
        make_table("results",
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   663
          List(name, hostname, numa_node, rc, out, err, timing_elapsed, timing_cpu, timing_gc))
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   664
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   665
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   666
    def read_results_domain(db: SQL.Database): Set[String] =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   667
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   668
        Results.table.select(List(Results.name)),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   669
        Set.from[String], res => res.string(Results.name))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   670
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   671
    def read_results(db: SQL.Database, names: List[String] = Nil): Map[String, Build_Job.Result] =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   672
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   673
        Results.table.select(sql = if_proper(names, Results.name.where_member(names))),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   674
        Map.from[String, Build_Job.Result],
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   675
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   676
          val name = res.string(Results.name)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   677
          val hostname = res.string(Results.hostname)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   678
          val numa_node = res.get_int(Results.numa_node)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   679
          val rc = res.int(Results.rc)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   680
          val out = res.string(Results.out)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   681
          val err = res.string(Results.err)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   682
          val timing =
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   683
            res.timing(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   684
              Results.timing_elapsed,
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   685
              Results.timing_cpu,
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   686
              Results.timing_gc)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   687
          val node_info = Host.Node_Info(hostname, numa_node)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   688
          val process_result =
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   689
            Process_Result(rc,
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   690
              out_lines = split_lines(out),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   691
              err_lines = split_lines(err),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   692
              timing = timing)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   693
          name -> Build_Job.Result(node_info, process_result)
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   694
        }
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   695
      )
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   696
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   697
    def update_results(db: SQL.Database, results: State.Results): Boolean = {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   698
      val old_results = read_results_domain(db)
77385
4a7c42c84743 proper filterNot, not filterNot-not;
wenzelm
parents: 77383
diff changeset
   699
      val insert = results.iterator.filterNot(p => old_results.contains(p._1)).toList
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   700
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   701
      for ((name, result) <- insert) {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   702
        val node_info = result.node_info
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   703
        val process_result = result.process_result
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   704
        db.execute_statement(Results.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   705
          { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   706
            stmt.string(1) = name
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   707
            stmt.string(2) = node_info.hostname
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   708
            stmt.int(3) = node_info.numa_node
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   709
            stmt.int(4) = process_result.rc
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   710
            stmt.string(5) = cat_lines(process_result.out_lines)
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   711
            stmt.string(6) = cat_lines(process_result.err_lines)
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   712
            stmt.long(7) = process_result.timing.elapsed.ms
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   713
            stmt.long(8) = process_result.timing.cpu.ms
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   714
            stmt.long(9) = process_result.timing.gc.ms
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   715
          })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   716
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   717
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   718
      insert.nonEmpty
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   719
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   720
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   721
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   722
    /* collective operations */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   723
77596
dd8b08729458 clarified signature;
wenzelm
parents: 77594
diff changeset
   724
    val all_tables: SQL.Tables =
dd8b08729458 clarified signature;
wenzelm
parents: 77594
diff changeset
   725
      SQL.Tables(
77534
wenzelm
parents: 77533
diff changeset
   726
        Base.table,
wenzelm
parents: 77533
diff changeset
   727
        Workers.table,
wenzelm
parents: 77533
diff changeset
   728
        Progress.table,
wenzelm
parents: 77533
diff changeset
   729
        Sessions.table,
wenzelm
parents: 77533
diff changeset
   730
        Pending.table,
wenzelm
parents: 77533
diff changeset
   731
        Running.table,
wenzelm
parents: 77533
diff changeset
   732
        Results.table,
wenzelm
parents: 77533
diff changeset
   733
        Host.Data.Node_Info.table)
wenzelm
parents: 77533
diff changeset
   734
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   735
    def update_database(
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   736
      db: SQL.Database,
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   737
      worker_uuid: String,
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   738
      build_uuid: String,
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   739
      hostname: String,
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   740
      state: State
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   741
    ): State = {
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   742
      val changed =
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   743
        List(
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   744
          update_sessions(db, state.sessions),
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   745
          update_pending(db, state.pending),
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   746
          update_running(db, state.running),
77476
5f6f567a2661 clarified modules;
wenzelm
parents: 77475
diff changeset
   747
          update_results(db, state.results),
77525
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   748
          Host.Data.update_numa_next(db, hostname, state.numa_next))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   749
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   750
      val serial0 = serial_max(db)
77513
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   751
      val serial = if (changed.exists(identity)) State.inc_serial(serial0) else serial0
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   752
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   753
      stamp_worker(db, worker_uuid, serial)
77583
9af2afc3f7b3 more explicit workers, e.g. for monitoring;
wenzelm
parents: 77580
diff changeset
   754
      state.set_serial(serial).set_workers(read_workers(db))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   755
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   756
  }
77396
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
   757
}
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   758
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   759
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   760
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   761
/** main process **/
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   762
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   763
class Build_Process(
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   764
  protected final val build_context: Build_Process.Context,
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   765
  protected final val build_progress: Progress
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   766
)
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   767
extends AutoCloseable {
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   768
  /* context */
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   769
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   770
  protected final val store: Sessions.Store = build_context.store
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   771
  protected final val build_options: Options = store.options
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   772
  protected final val build_deps: Sessions.Deps = build_context.build_deps
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   773
  protected final val build_uuid: String = build_context.build_uuid
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   774
  protected final val worker_uuid: String = UUID.random().toString
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   775
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   776
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   777
  /* global state: internal var vs. external database */
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   778
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   779
  private var _state: Build_Process.State = init_state(Build_Process.State())
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   780
77514
acaa89cb977b tuned signature;
wenzelm
parents: 77513
diff changeset
   781
  private val _database: Option[SQL.Database] = build_context.open_database()
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   782
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   783
  def close(): Unit = synchronized { _database.foreach(_.close()) }
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   784
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   785
  protected def synchronized_database[A](body: => A): A =
77438
0030eabbe6c3 more robust: synchronized access to database;
wenzelm
parents: 77437
diff changeset
   786
    synchronized {
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   787
      _database match {
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   788
        case None => body
77596
dd8b08729458 clarified signature;
wenzelm
parents: 77594
diff changeset
   789
        case Some(db) => db.transaction_lock(Build_Process.Data.all_tables)(body)
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   790
      }
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   791
    }
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   792
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   793
  private def sync_database(): Unit =
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   794
    synchronized_database {
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   795
      for (db <- _database) {
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   796
        _state =
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   797
          Build_Process.Data.update_database(
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   798
            db, worker_uuid, build_uuid, build_context.hostname, _state)
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   799
      }
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   800
    }
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   801
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   802
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   803
  /* progress backed by database */
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   804
77533
wenzelm
parents: 77531
diff changeset
   805
  private def progress_output(message: Progress.Message, build_progress_output: => Unit): Unit = {
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   806
    synchronized_database {
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   807
      val state1 = _state.inc_serial.progress_serial()
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   808
      for (db <- _database) {
77529
40ccee0fe19a separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents: 77527
diff changeset
   809
        Build_Process.Data.write_progress(db, state1.serial, message, build_uuid)
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   810
        Build_Process.Data.stamp_worker(db, worker_uuid, state1.serial)
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   811
      }
77533
wenzelm
parents: 77531
diff changeset
   812
      build_progress_output
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   813
      _state = state1
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   814
    }
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   815
  }
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   816
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   817
  protected object progress extends Progress {
77509
3bc49507bae5 clarified treatment of "verbose" messages, e.g. Progress.theory();
wenzelm
parents: 77505
diff changeset
   818
    override def verbose: Boolean = build_progress.verbose
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   819
77509
3bc49507bae5 clarified treatment of "verbose" messages, e.g. Progress.theory();
wenzelm
parents: 77505
diff changeset
   820
    override def output(message: Progress.Message): Unit =
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   821
      progress_output(message, if (do_output(message)) build_progress.output(message))
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   822
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   823
    override def theory(theory: Progress.Theory): Unit =
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   824
      progress_output(theory.message, build_progress.theory(theory))
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   825
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   826
    override def nodes_status(nodes_status: Document_Status.Nodes_Status): Unit =
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   827
      build_progress.nodes_status(nodes_status)
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   828
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   829
    override def stop(): Unit = build_progress.stop()
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   830
    override def stopped: Boolean = build_progress.stopped
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   831
  }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   832
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   833
  protected val log: Logger = Logger.make_system_log(progress, build_options)
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   834
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   835
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   836
  /* policy operations */
77333
0bec014c0f9f tuned signature;
wenzelm
parents: 77332
diff changeset
   837
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   838
  protected def init_state(state: Build_Process.State): Build_Process.State = {
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   839
    val sessions1 =
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   840
      build_context.sessions.foldLeft(state.sessions) { case (map, (name, session)) =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   841
        if (state.sessions.isDefinedAt(name)) map
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   842
        else map + (name -> session)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   843
      }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   844
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   845
    val old_pending = state.pending.iterator.map(_.name).toSet
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   846
    val new_pending =
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   847
      List.from(
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   848
        for {
77449
wenzelm
parents: 77448
diff changeset
   849
          (name, session_context) <- build_context.sessions.iterator
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   850
          if !old_pending(name)
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   851
        } yield Build_Process.Task(name, session_context.deps))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   852
    val pending1 = new_pending ::: state.pending
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   853
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   854
    state.copy(sessions = sessions1, pending = pending1)
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   855
  }
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   856
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   857
  protected def next_job(state: Build_Process.State): Option[String] =
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
   858
    if (progress.stopped || state.running.size < build_context.max_jobs) {
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   859
      state.pending.filter(entry => entry.is_ready && !state.is_running(entry.name))
77313
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   860
        .sortBy(_.name)(build_context.ordering)
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   861
        .headOption.map(_.name)
77296
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   862
    }
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   863
    else None
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   864
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   865
  protected def start_session(state: Build_Process.State, session_name: String): Build_Process.State = {
77468
wenzelm
parents: 77467
diff changeset
   866
    val ancestor_results =
wenzelm
parents: 77467
diff changeset
   867
      for (a <- build_context.sessions(session_name).ancestors) yield state.results(a)
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   868
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   869
    val input_shasum =
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   870
      if (ancestor_results.isEmpty) {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   871
        SHA1.shasum_meta_info(SHA1.digest(Path.explode("$POLYML_EXE")))
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   872
      }
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   873
      else SHA1.flat_shasum(ancestor_results.map(_.output_shasum))
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   874
77463
4e8bec105ce5 tuned signature;
wenzelm
parents: 77462
diff changeset
   875
    val store_heap = build_context.store_heap(session_name)
77469
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   876
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   877
    val (current, output_shasum) =
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   878
      store.check_output(session_name,
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   879
        sources_shasum = build_context.sources_shasum(session_name),
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   880
        input_shasum = input_shasum,
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   881
        fresh_build = build_context.fresh_build,
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   882
        store_heap = store_heap)
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   883
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   884
    val all_current = current && ancestor_results.forall(_.current)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   885
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   886
    if (all_current) {
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   887
      state
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   888
        .remove_pending(session_name)
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   889
        .make_result(session_name, Process_Result.ok, output_shasum, current = true)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   890
    }
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   891
    else if (build_context.no_build) {
77521
5642de4d225d clarified signature: manage "verbose" flag via "progress";
wenzelm
parents: 77514
diff changeset
   892
      progress.echo("Skipping " + session_name + " ...", verbose = true)
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   893
      state.
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   894
        remove_pending(session_name).
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   895
        make_result(session_name, Process_Result.error, output_shasum)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   896
    }
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
   897
    else if (progress.stopped || !ancestor_results.forall(_.ok)) {
77452
wenzelm
parents: 77449
diff changeset
   898
      progress.echo(session_name + " CANCELLED")
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   899
      state
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   900
        .remove_pending(session_name)
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   901
        .make_result(session_name, Process_Result.undefined, output_shasum)
77452
wenzelm
parents: 77449
diff changeset
   902
    }
wenzelm
parents: 77449
diff changeset
   903
    else {
77551
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
   904
      val (numa_node, state1) = state.numa_next_node(build_context.numa_nodes)
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
   905
      val node_info = Host.Node_Info(build_context.hostname, numa_node)
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
   906
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
   907
      progress.echo(
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
   908
        (if (store_heap) "Building " else "Running ") + session_name +
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
   909
          if_proper(node_info.numa_node, " on " + node_info) + " ...")
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   910
77472
a073ac3f3b56 clarified modules;
wenzelm
parents: 77470
diff changeset
   911
      store.init_output(session_name)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   912
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   913
      val job =
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   914
        Build_Job.start_session(build_context, worker_uuid, progress, log,
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   915
          build_deps.background(session_name), input_shasum, node_info)
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   916
      state1.add_running(session_name, job)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   917
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   918
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   919
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   920
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   921
  /* build process roles */
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   922
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   923
  final def start_build(): Unit = synchronized_database {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   924
    for (db <- _database) {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   925
      Build_Process.Data.start_build(db, build_uuid, build_context.ml_platform,
77604
b4ef44ce08ed do not export connection details (password etc.);
wenzelm
parents: 77596
diff changeset
   926
        store.options.make_prefs(Options.init(prefs = ""), filter = _.is_exported))
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   927
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   928
  }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   929
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   930
  final def stop_build(): Unit = synchronized_database {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   931
    for (db <- _database) {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   932
      Build_Process.Data.stop_build(db, build_uuid)
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   933
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   934
  }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   935
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   936
  final def start_worker(): Unit = synchronized_database {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   937
    for (db <- _database) {
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   938
      val java = ProcessHandle.current()
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   939
      val java_pid = java.pid
77588
066d5df144f0 assume total operation: ProcessHandle.current().info.startInstant appears to work on all platforms;
wenzelm
parents: 77587
diff changeset
   940
      val java_start = Date.instant(java.info.startInstant.get)
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   941
      val serial =
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   942
        Build_Process.Data.start_worker(
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   943
          db, worker_uuid, build_uuid, build_context.hostname, java_pid, java_start)
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   944
      _state = _state.set_serial(serial)
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   945
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   946
  }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   947
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   948
  final def stop_worker(): Unit = synchronized_database {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   949
    for (db <- _database) {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   950
      Build_Process.Data.stamp_worker(db, worker_uuid, _state.serial, stop = true)
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   951
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   952
  }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   953
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   954
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   955
  /* run */
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   956
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
   957
  def run(): Map[String, Process_Result] = {
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   958
    def finished(): Boolean = synchronized_database { _state.finished }
77400
f3e5b3fe230e clarified signature: more explicit "synchronized" regions;
wenzelm
parents: 77398
diff changeset
   959
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   960
    def sleep(): Unit =
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   961
      Isabelle_Thread.interrupt_handler(_ => progress.stop()) {
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   962
        build_options.seconds("editor_input_delay").sleep()
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   963
      }
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   964
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   965
    def start_job(): Boolean = synchronized_database {
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   966
      next_job(_state) match {
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   967
        case Some(name) =>
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   968
          if (Build_Job.is_session_name(name)) {
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   969
            _state = start_session(_state, name)
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   970
            true
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   971
          }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   972
          else error("Unsupported build job name " + quote(name))
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   973
        case None => false
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   974
      }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   975
    }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   976
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   977
    if (finished()) {
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   978
      progress.echo_warning("Nothing to build")
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   979
      Map.empty[String, Process_Result]
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   980
    }
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   981
    else {
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
   982
      if (build_context.master) start_build()
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
   983
77580
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
   984
      start_worker()
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
   985
      if (build_context.master && !build_context.worker_active) {
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
   986
        progress.echo("Waiting for external workers ...")
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
   987
      }
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
   988
77538
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   989
      try {
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   990
        while (!finished()) {
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   991
          if (progress.stopped) synchronized_database { _state.stop_running() }
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   992
77538
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   993
          for (job <- synchronized_database { _state.finished_running() }) {
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   994
            val job_name = job.job_name
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   995
            val (process_result, output_shasum) = job.join
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   996
            synchronized_database {
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   997
              _state = _state.
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   998
                remove_pending(job_name).
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   999
                remove_running(job_name).
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1000
                make_result(job_name, process_result, output_shasum, node_info = job.node_info)
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1001
            }
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1002
          }
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1003
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1004
          if (!start_job()) {
77538
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1005
            sync_database()
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1006
            sleep()
77396
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
  1007
          }
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
  1008
        }
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
  1009
      }
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1010
      finally {
77580
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
  1011
        stop_worker()
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
  1012
        if (build_context.master) stop_build()
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1013
      }
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1014
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
  1015
      synchronized_database {
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
  1016
        for ((name, result) <- _state.results) yield name -> result.process_result
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1017
      }
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1018
    }
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1019
  }
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
  1020
}