src/Pure/Tools/build_process.scala
author wenzelm
Wed, 14 Jun 2023 12:10:40 +0200
changeset 78154 8a7df40375ae
parent 78153 55a6aa77f3d8
child 78156 da5cc332ded3
permissions -rw-r--r--
tuned signature: more operations;
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(
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
    47
                build_uuid, name, deps, ancestors, info.session_prefs, sources_shasum,
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
    48
                info.timeout, store, 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 =
77646
c55443f9fedd tuned output;
wenzelm
parents: 77640
diff changeset
   108
      "Build_Process.Context(build_uuid = " + quote(build_uuid) +
c55443f9fedd tuned output;
wenzelm
parents: 77640
diff changeset
   109
        if_proper(master, ", master = true") + ")"
77561
a24d77f2cfe9 tuned output;
wenzelm
parents: 77552
diff changeset
   110
77470
38503d9ff2e5 clarified modules;
wenzelm
parents: 77469
diff changeset
   111
    def build_options: Options = store.options
38503d9ff2e5 clarified modules;
wenzelm
parents: 77469
diff changeset
   112
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
   113
    def sessions_structure: Sessions.Structure = build_deps.sessions_structure
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   114
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   115
    def sources_shasum(name: String): SHA1.Shasum = sessions(name).sources_shasum
77458
403748b23f13 clarified signature: prefer static data;
wenzelm
parents: 77457
diff changeset
   116
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   117
    def old_command_timings(name: String): List[Properties.T] =
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   118
      sessions.get(name) match {
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   119
        case Some(session_context) =>
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   120
          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
   121
        case None => Nil
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   122
      }
77439
d6bf9ec39d12 avoid premature Properties.uncompress: allow blob to be stored in another database;
wenzelm
parents: 77438
diff changeset
   123
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   124
    def prepare_database(): Unit = {
77629
979baa91da0f clarified modules;
wenzelm
parents: 77610
diff changeset
   125
      using_option(store.open_build_database()) { db =>
77678
e7d8e990d378 proper vacuum of session_info tables: only once per build process;
wenzelm
parents: 77675
diff changeset
   126
        val shared_db = db.is_postgresql
78154
8a7df40375ae tuned signature: more operations;
wenzelm
parents: 78153
diff changeset
   127
        db.transaction_lock(Data.all_tables, create = true) {
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
   128
          Data.clean_build(db)
77678
e7d8e990d378 proper vacuum of session_info tables: only once per build process;
wenzelm
parents: 77675
diff changeset
   129
          if (shared_db) store.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
   130
        }
77678
e7d8e990d378 proper vacuum of session_info tables: only once per build process;
wenzelm
parents: 77675
diff changeset
   131
        db.vacuum(Data.all_tables ::: (if (shared_db) store.all_tables else SQL.Tables.empty))
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   132
      }
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   133
    }
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   134
77463
4e8bec105ce5 tuned signature;
wenzelm
parents: 77462
diff changeset
   135
    def store_heap(name: String): Boolean =
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   136
      build_heap || Sessions.is_pure(name) ||
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   137
      sessions.valuesIterator.exists(_.ancestors.contains(name))
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
   138
77594
7723cf88658f proper test (amending 32f9e75c92e9);
wenzelm
parents: 77590
diff changeset
   139
    def worker_active: Boolean = max_jobs > 0
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   140
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   141
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   142
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   143
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   144
  /** dynamic state **/
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   145
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   146
  type Progress_Messages = SortedMap[Long, Progress.Message]
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   147
  val progress_messages_empty: Progress_Messages = SortedMap.empty
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   148
77660
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   149
  case class Build(
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   150
    build_uuid: String,
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   151
    ml_platform: String,
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   152
    options: String,
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   153
    start: Date,
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   154
    stop: Option[Date],
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   155
    progress_stopped: Boolean
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   156
  )
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   157
77631
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   158
  case class Worker(
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   159
    worker_uuid: String,
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   160
    build_uuid: String,
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   161
    hostname: String,
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   162
    java_pid: Long,
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   163
    java_start: Date,
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   164
    start: Date,
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   165
    stamp: Date,
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   166
    stop: Option[Date],
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   167
    serial: Long
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   168
  )
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   169
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   170
  case class Task(
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   171
    name: String,
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   172
    deps: List[String],
77661
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   173
    info: JSON.Object.T,
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   174
    build_uuid: String
77631
89fffc5f5728 tuned whitespace;
wenzelm
parents: 77630
diff changeset
   175
  ) {
77313
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   176
    def is_ready: Boolean = deps.isEmpty
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   177
    def resolve(dep: String): Task =
77313
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   178
      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
   179
  }
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   180
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   181
  case class Job(
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   182
    name: String,
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   183
    worker_uuid: String,
77634
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
   184
    build_uuid: String,
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   185
    node_info: Host.Node_Info,
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   186
    build: Option[Build_Job]
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   187
  ) extends Library.Named {
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   188
    def no_build: Job = copy(build = None)
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   189
  }
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   190
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   191
  case class Result(
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   192
    name: String,
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   193
    worker_uuid: String,
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   194
    build_uuid: String,
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   195
    node_info: Host.Node_Info,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   196
    process_result: Process_Result,
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   197
    output_shasum: SHA1.Shasum,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   198
    current: Boolean
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   199
  ) extends Library.Named {
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   200
    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
   201
  }
77312
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   202
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   203
  sealed case class Snapshot(
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   204
    progress_messages: Progress_Messages,
77660
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   205
    builds: List[Build],        // available build configurations
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   206
    workers: List[Worker],      // available worker processes
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   207
    sessions: State.Sessions,   // static build targets
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   208
    pending: State.Pending,     // dynamic build "queue"
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   209
    running: State.Running,     // presently running jobs
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   210
    results: State.Results)     // finished results
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   211
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   212
  object State {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   213
    type Sessions = Map[String, Build_Job.Session_Context]
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   214
    type Pending = List[Task]
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   215
    type Running = Map[String, Job]
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   216
    type Results = Map[String, Result]
77513
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   217
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   218
    def inc_serial(serial: Long): Long = {
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   219
      require(serial < java.lang.Long.MAX_VALUE, "serial overflow")
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   220
      serial + 1
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   221
    }
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   222
  }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   223
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   224
  sealed case class State(
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   225
    serial: Long = 0,
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   226
    progress_seen: Long = 0,
77525
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   227
    numa_next: Int = 0,
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   228
    sessions: State.Sessions = Map.empty,
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   229
    pending: State.Pending = Nil,
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   230
    running: State.Running = Map.empty,
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
   231
    results: State.Results = Map.empty
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   232
  ) {
77513
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   233
    require(serial >= 0, "serial underflow")
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   234
    def inc_serial: State = copy(serial = State.inc_serial(serial))
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   235
    def set_serial(i: Long): State = {
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   236
      require(serial <= i, "non-monotonic change of serial")
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   237
      copy(serial = i)
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   238
    }
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   239
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   240
    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
   241
      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
   242
      else error("Bad serial " + message_serial + " for progress output (already seen)")
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   243
77632
f7208db921c2 tuned signature;
wenzelm
parents: 77631
diff changeset
   244
    def next_numa_node(numa_nodes: List[Int]): (Option[Int], State) =
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   245
      if (numa_nodes.isEmpty) (None, this)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   246
      else {
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   247
        val available = numa_nodes.zipWithIndex
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   248
        val used =
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   249
          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
   250
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   251
        val numa_index = available.collectFirst({ case (n, i) if n == numa_next => i }).getOrElse(0)
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   252
        val candidates = available.drop(numa_index) ::: available.take(numa_index)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   253
        val (n, i) =
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   254
          candidates.find({ case (n, i) => i == numa_index && !used(n) }) orElse
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   255
          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
   256
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   257
        (Some(n), copy(numa_next = numa_nodes((i + 1) % numa_nodes.length)))
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   258
      }
77337
1ab68d4370ec tuned signature;
wenzelm
parents: 77336
diff changeset
   259
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   260
    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
   261
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   262
    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
   263
      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
   264
        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
   265
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   266
    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
   267
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   268
    def stop_running(): Unit =
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   269
      for (job <- running.valuesIterator; build <- job.build) build.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
   270
77649
739cb777cc75 clarified signature;
wenzelm
parents: 77648
diff changeset
   271
    def finished_running(): List[Job] =
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   272
      List.from(
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   273
        for (job <- running.valuesIterator; build <- job.build if build.is_finished)
77649
739cb777cc75 clarified signature;
wenzelm
parents: 77648
diff changeset
   274
        yield job)
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   275
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   276
    def add_running(job: Job): State =
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   277
      copy(running = running + (job.name -> job))
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   278
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   279
    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
   280
      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
   281
77336
e9beaaf955bf tuned signature;
wenzelm
parents: 77335
diff changeset
   282
    def make_result(
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   283
      result_name: (String, String, String),
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   284
      process_result: Process_Result,
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   285
      output_shasum: SHA1.Shasum,
77475
3bc611c80346 clarified modules;
wenzelm
parents: 77474
diff changeset
   286
      node_info: Host.Node_Info = Host.Node_Info.none,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   287
      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
   288
    ): State = {
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   289
      val (name, worker_uuid, build_uuid) = result_name
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   290
      val result =
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   291
        Result(name, worker_uuid, build_uuid, node_info, process_result, output_shasum, current)
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   292
      copy(results = results + (name -> 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
   293
    }
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   294
  }
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   295
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   296
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   297
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   298
  /** SQL data model **/
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   299
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   300
  object Data {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   301
    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
   302
      SQL.Table("isabelle_build" + if_proper(name, "_" + name), columns, body = body)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   303
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   304
    def pull_data[A <: Library.Named](
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   305
      data_domain: Set[String],
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   306
      data_iterator: Set[String] => Iterator[A],
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   307
      old_data: Map[String, A]
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   308
    ): Map[String, A] = {
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   309
      val dom = data_domain -- old_data.keysIterator
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   310
      val data = old_data -- old_data.keysIterator.filterNot(dom)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   311
      if (dom.isEmpty) data
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   312
      else data_iterator(dom).foldLeft(data) { case (map, a) => map + (a.name -> a) }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   313
    }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   314
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   315
    def pull0[A <: Library.Named](
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   316
      new_data: Map[String, A],
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   317
      old_data: Map[String, A]
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   318
    ): Map[String, A] = {
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   319
      pull_data(new_data.keySet, dom => new_data.valuesIterator.filter(a => dom(a.name)), old_data)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   320
    }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   321
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   322
    def pull1[A <: Library.Named](
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   323
      data_domain: Set[String],
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   324
      data_base: Set[String] => Map[String, A],
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   325
      old_data: Map[String, A]
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   326
    ): Map[String, A] = {
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   327
      pull_data(data_domain, dom => data_base(dom).valuesIterator, old_data)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   328
    }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   329
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   330
    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
   331
      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
   332
      val worker_uuid = SQL.Column.string("worker_uuid")
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   333
      val name = SQL.Column.string("name")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   334
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
   335
      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
   336
        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
   337
        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
   338
        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
   339
      ): SQL.Source =
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   340
        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
   341
          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
   342
          if_proper(worker_uuid, Generic.worker_uuid.equal(worker_uuid)),
77375
324f5821a4a4 clarified signature: more concise operations;
wenzelm
parents: 77374
diff changeset
   343
          if_proper(names, Generic.name.member(names)))
77662
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   344
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   345
      def sql_where(
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   346
        build_uuid: String = "",
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   347
        worker_uuid: String = "",
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   348
        names: Iterable[String] = Nil
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   349
      ): SQL.Source = {
77663
c82d49a56cf9 tuned signature: removed redundant argument;
wenzelm
parents: 77662
diff changeset
   350
        SQL.where(sql(build_uuid = build_uuid, worker_uuid = worker_uuid, names = names))
77662
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   351
      }
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   352
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   353
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   354
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   355
    /* base table */
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   356
77417
wenzelm
parents: 77416
diff changeset
   357
    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
   358
      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
   359
      val ml_platform = SQL.Column.string("ml_platform")
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   360
      val options = SQL.Column.string("options")
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   361
      val start = SQL.Column.date("start")
77545
4af88aca2a4f clarified database content;
wenzelm
parents: 77543
diff changeset
   362
      val stop = SQL.Column.date("stop")
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   363
      val progress_stopped = SQL.Column.bool("progress_stopped")
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   364
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   365
      val table =
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   366
        make_table("", List(build_uuid, ml_platform, options, start, stop, progress_stopped))
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   367
    }
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   368
77660
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   369
    def read_builds(db: SQL.Database, build_uuid: String = ""): List[Build] =
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   370
      db.execute_query_statement(
77662
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   371
        Base.table.select(sql = Generic.sql_where(build_uuid = build_uuid)),
77660
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   372
        List.from[Build],
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   373
        { res =>
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   374
          val build_uuid = res.string(Base.build_uuid)
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   375
          val ml_platform = res.string(Base.ml_platform)
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   376
          val options = res.string(Base.options)
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   377
          val start = res.date(Base.start)
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   378
          val stop = res.get_date(Base.stop)
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   379
          val progress_stopped = res.bool(Base.progress_stopped)
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   380
          Build(build_uuid, ml_platform, options, start, stop, progress_stopped)
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   381
        })
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
   382
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   383
    def start_build(
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   384
      db: SQL.Database,
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   385
      build_uuid: String,
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   386
      ml_platform: String,
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   387
      options: String,
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   388
      progress_stopped: Boolean
77536
7c7f1473e51a clarified database content and prepare/init stages;
wenzelm
parents: 77534
diff changeset
   389
    ): Unit = {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   390
      db.execute_statement(Base.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   391
        { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   392
          stmt.string(1) = build_uuid
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   393
          stmt.string(2) = ml_platform
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   394
          stmt.string(3) = options
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   395
          stmt.date(4) = db.now()
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   396
          stmt.date(5) = None
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   397
          stmt.bool(6) = progress_stopped
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   398
        })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   399
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   400
77545
4af88aca2a4f clarified database content;
wenzelm
parents: 77543
diff changeset
   401
    def stop_build(db: SQL.Database, build_uuid: String): Unit =
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   402
      db.execute_statement(
77636
dc96e6c56369 more robust SQL query for mandatory arguments;
wenzelm
parents: 77635
diff changeset
   403
        Base.table.update(List(Base.stop), sql = Base.build_uuid.where_equal(build_uuid)),
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   404
        body = { stmt => stmt.date(1) = db.now() })
77538
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
   405
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
   406
    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
   407
      val old =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   408
        db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   409
          Base.table.select(List(Base.build_uuid), sql = SQL.where(Base.stop.defined)),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   410
          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
   411
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
   412
      if (old.nonEmpty) {
77658
wenzelm
parents: 77657
diff changeset
   413
        for (table <- build_uuid_tables) {
77540
c537905c2125 tuned signature;
wenzelm
parents: 77539
diff changeset
   414
          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
   415
        }
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
   416
      }
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
   417
    }
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
   418
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   419
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   420
    /* sessions */
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   421
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   422
    object Sessions {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   423
      val name = Generic.name.make_primary_key
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   424
      val deps = SQL.Column.string("deps")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   425
      val ancestors = SQL.Column.string("ancestors")
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   426
      val options = SQL.Column.string("options")
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   427
      val sources = SQL.Column.string("sources")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   428
      val timeout = SQL.Column.long("timeout")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   429
      val old_time = SQL.Column.long("old_time")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   430
      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
   431
      val build_uuid = Generic.build_uuid
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   432
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   433
      val table = make_table("sessions",
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   434
        List(name, deps, ancestors, options, sources, timeout,
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   435
          old_time, old_command_timings, build_uuid))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   436
    }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   437
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   438
    def read_sessions_domain(db: SQL.Database): Set[String] =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   439
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   440
        Sessions.table.select(List(Sessions.name)),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   441
        Set.from[String], res => res.string(Sessions.name))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   442
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   443
    def read_sessions(db: SQL.Database, names: Iterable[String] = Nil): State.Sessions =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   444
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   445
        Sessions.table.select(sql = if_proper(names, Sessions.name.where_member(names))),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   446
        Map.from[String, Build_Job.Session_Context],
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   447
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   448
          val name = res.string(Sessions.name)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   449
          val deps = split_lines(res.string(Sessions.deps))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   450
          val ancestors = split_lines(res.string(Sessions.ancestors))
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   451
          val options = res.string(Sessions.options)
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   452
          val sources_shasum = SHA1.fake_shasum(res.string(Sessions.sources))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   453
          val timeout = Time.ms(res.long(Sessions.timeout))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   454
          val old_time = Time.ms(res.long(Sessions.old_time))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   455
          val old_command_timings_blob = res.bytes(Sessions.old_command_timings)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   456
          val build_uuid = res.string(Sessions.build_uuid)
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   457
          name -> Build_Job.Session_Context(name, deps, ancestors, options, sources_shasum,
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   458
            timeout, old_time, old_command_timings_blob, build_uuid)
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   459
        }
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   460
      )
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   461
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   462
    def update_sessions(db:SQL.Database, sessions: State.Sessions): Boolean = {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   463
      val old_sessions = read_sessions_domain(db)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   464
      val insert = sessions.iterator.filterNot(p => old_sessions.contains(p._1)).toList
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   465
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   466
      for ((name, session) <- insert) {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   467
        db.execute_statement(Sessions.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   468
          { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   469
            stmt.string(1) = name
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   470
            stmt.string(2) = cat_lines(session.deps)
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   471
            stmt.string(3) = cat_lines(session.ancestors)
77610
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   472
            stmt.string(4) = session.session_prefs
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   473
            stmt.string(5) = session.sources_shasum.toString
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   474
            stmt.long(6) = session.timeout.ms
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   475
            stmt.long(7) = session.old_time.ms
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   476
            stmt.bytes(8) = session.old_command_timings_blob
3b09ae9e40cb clarified session prefs (or "options" within the database);
wenzelm
parents: 77609
diff changeset
   477
            stmt.string(9) = session.build_uuid
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   478
          })
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   479
      }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   480
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   481
      insert.nonEmpty
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   482
    }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   483
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   484
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   485
    /* progress */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   486
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   487
    object Progress {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   488
      val serial = SQL.Column.long("serial").make_primary_key
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   489
      val kind = SQL.Column.int("kind")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   490
      val text = SQL.Column.string("text")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   491
      val verbose = SQL.Column.bool("verbose")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   492
      val build_uuid = Generic.build_uuid
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   493
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   494
      val table = make_table("progress", List(serial, kind, text, verbose, build_uuid))
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   495
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   496
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   497
    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
   498
      db.execute_query_statement(
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   499
        Progress.table.select(
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   500
          sql =
78153
55a6aa77f3d8 tuned signature: more operations;
wenzelm
parents: 78151
diff changeset
   501
            SQL.where_and(
55a6aa77f3d8 tuned signature: more operations;
wenzelm
parents: 78151
diff changeset
   502
              if (seen <= 0) "" else Progress.serial.ident + " > " + seen,
55a6aa77f3d8 tuned signature: more operations;
wenzelm
parents: 78151
diff changeset
   503
              Generic.sql(build_uuid = build_uuid))),
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   504
        SortedMap.from[Long, isabelle.Progress.Message],
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   505
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   506
          val serial = res.long(Progress.serial)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   507
          val kind = isabelle.Progress.Kind(res.int(Progress.kind))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   508
          val text = res.string(Progress.text)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   509
          val verbose = res.bool(Progress.verbose)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   510
          serial -> isabelle.Progress.Message(kind, text, verbose = verbose)
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   511
        }
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   512
      )
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   513
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   514
    def write_progress(
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   515
      db: SQL.Database,
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   516
      message_serial: Long,
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   517
      message: isabelle.Progress.Message,
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   518
      build_uuid: String
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   519
    ): Unit = {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   520
      db.execute_statement(Progress.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   521
        { stmt =>
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   522
          stmt.long(1) = message_serial
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   523
          stmt.int(2) = message.kind.id
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   524
          stmt.string(3) = message.text
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   525
          stmt.bool(4) = message.verbose
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   526
          stmt.string(5) = build_uuid
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   527
        })
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   528
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   529
77637
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   530
    def sync_progress(
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   531
      db: SQL.Database,
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   532
      seen: Long,
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   533
      build_uuid: String,
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   534
      build_progress: Progress
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   535
    ): (Progress_Messages, Boolean) = {
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   536
      require(build_uuid.nonEmpty)
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   537
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   538
      val messages = read_progress(db, seen = seen, build_uuid = build_uuid)
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   539
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   540
      val stopped_db =
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   541
        db.execute_query_statementO[Boolean](
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   542
          Base.table.select(List(Base.progress_stopped),
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   543
            sql = SQL.where(Base.build_uuid.equal(build_uuid))),
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   544
          res => res.bool(Base.progress_stopped)
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   545
        ).getOrElse(false)
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   546
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   547
      def stop_db(): Unit =
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   548
        db.execute_statement(
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   549
          Base.table.update(
77636
dc96e6c56369 more robust SQL query for mandatory arguments;
wenzelm
parents: 77635
diff changeset
   550
            List(Base.progress_stopped), sql = Base.build_uuid.where_equal(build_uuid)),
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   551
          body = { stmt => stmt.bool(1) = true })
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   552
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   553
      val stopped = build_progress.stopped
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   554
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   555
      if (stopped_db && !stopped) build_progress.stop()
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   556
      if (stopped && !stopped_db) stop_db()
77637
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   557
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   558
      (messages, messages.isEmpty && stopped_db == stopped)
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   559
    }
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   560
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   561
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   562
    /* workers */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   563
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   564
    object Workers {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   565
      val worker_uuid = Generic.worker_uuid.make_primary_key
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   566
      val build_uuid = Generic.build_uuid
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   567
      val hostname = SQL.Column.string("hostname")
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   568
      val java_pid = SQL.Column.long("java_pid")
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   569
      val java_start = SQL.Column.date("java_start")
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   570
      val start = SQL.Column.date("start")
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   571
      val stamp = SQL.Column.date("stamp")
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   572
      val stop = SQL.Column.date("stop")
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   573
      val serial = SQL.Column.long("serial")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   574
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   575
      val table = make_table("workers",
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   576
        List(worker_uuid, build_uuid, hostname, java_pid, java_start, start, stamp, stop, serial))
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   577
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   578
77655
136ab737a36d tuned signature;
wenzelm
parents: 77654
diff changeset
   579
    def read_serial(db: SQL.Database): Long =
136ab737a36d tuned signature;
wenzelm
parents: 77654
diff changeset
   580
      db.execute_query_statementO[Long](
78151
2fdf3d8a94e6 clarified signature;
wenzelm
parents: 77678
diff changeset
   581
        Workers.table.select(List(Workers.serial.max)), _.long(Workers.serial)).getOrElse(0L)
77655
136ab737a36d tuned signature;
wenzelm
parents: 77654
diff changeset
   582
77586
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   583
    def read_workers(
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   584
      db: SQL.Database,
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   585
      build_uuid: String = "",
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   586
      worker_uuid: String = ""
77657
a2a4adc268b8 removed redundant State.workers: directly maintained within the database, using with SQL update;
wenzelm
parents: 77656
diff changeset
   587
    ): List[Worker] = {
77586
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   588
      db.execute_query_statement(
77662
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   589
        Workers.table.select(
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   590
          sql = Generic.sql_where(build_uuid = build_uuid, worker_uuid = worker_uuid)),
77586
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   591
          List.from[Worker],
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   592
          { res =>
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   593
            Worker(
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   594
              worker_uuid = res.string(Workers.worker_uuid),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   595
              build_uuid = res.string(Workers.build_uuid),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   596
              hostname = res.string(Workers.hostname),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   597
              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
   598
              java_start = res.date(Workers.java_start),
77586
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   599
              start = res.date(Workers.start),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   600
              stamp = res.date(Workers.stamp),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   601
              stop = res.get_date(Workers.stop),
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   602
              serial = res.long(Workers.serial))
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   603
          })
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   604
    }
80021d645a01 tuned structure;
wenzelm
parents: 77585
diff changeset
   605
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   606
    def start_worker(
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   607
      db: SQL.Database,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   608
      worker_uuid: String,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   609
      build_uuid: String,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   610
      hostname: String,
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   611
      java_pid: Long,
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   612
      java_start: Date,
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   613
      serial: Long
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   614
    ): Unit = {
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   615
      def err(msg: String): Nothing =
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   616
        error("Cannot start worker " + worker_uuid + if_proper(msg, "\n" + msg))
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   617
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   618
      val build_stop =
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   619
        db.execute_query_statementO(
77636
dc96e6c56369 more robust SQL query for mandatory arguments;
wenzelm
parents: 77635
diff changeset
   620
          Base.table.select(List(Base.stop), sql = Base.build_uuid.where_equal(build_uuid)),
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   621
          res => res.get_date(Base.stop))
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   622
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   623
      build_stop match {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   624
        case Some(None) =>
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   625
        case Some(Some(_)) => err("for already stopped build process " + build_uuid)
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   626
        case None => err("for unknown build process " + build_uuid)
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   627
      }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   628
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   629
      db.execute_statement(Workers.table.insert(), body =
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   630
        { stmt =>
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   631
          val now = db.now()
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   632
          stmt.string(1) = worker_uuid
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   633
          stmt.string(2) = build_uuid
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   634
          stmt.string(3) = hostname
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   635
          stmt.long(4) = java_pid
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   636
          stmt.date(5) = java_start
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   637
          stmt.date(6) = now
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   638
          stmt.date(7) = now
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   639
          stmt.date(8) = None
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
   640
          stmt.long(9) = serial
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   641
        })
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   642
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   643
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   644
    def stamp_worker(
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   645
      db: SQL.Database,
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   646
      worker_uuid: String,
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   647
      serial: Long,
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   648
      stop: Boolean = false
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   649
    ): Unit = {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   650
      val sql =
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   651
        Workers.table.update(List(Workers.stamp, Workers.stop, Workers.serial),
77636
dc96e6c56369 more robust SQL query for mandatory arguments;
wenzelm
parents: 77635
diff changeset
   652
          sql = Workers.worker_uuid.where_equal(worker_uuid))
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   653
      db.execute_statement(sql, body =
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   654
        { stmt =>
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   655
          val now = db.now()
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   656
          stmt.date(1) = now
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   657
          stmt.date(2) = if (stop) Some(now) else None
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   658
          stmt.long(3) = serial
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   659
        })
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   660
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   661
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   662
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   663
    /* pending jobs */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   664
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   665
    object Pending {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   666
      val name = Generic.name.make_primary_key
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   667
      val deps = SQL.Column.string("deps")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   668
      val info = SQL.Column.string("info")
77661
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   669
      val build_uuid = Generic.build_uuid
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   670
77661
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   671
      val table = make_table("pending", List(name, deps, info, build_uuid))
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   672
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   673
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   674
    def read_pending(db: SQL.Database): List[Task] =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   675
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   676
        Pending.table.select(sql = SQL.order_by(List(Pending.name))),
77585
89c42ec77a84 tuned signature;
wenzelm
parents: 77584
diff changeset
   677
        List.from[Task],
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   678
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   679
          val name = res.string(Pending.name)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   680
          val deps = res.string(Pending.deps)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   681
          val info = res.string(Pending.info)
77661
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   682
          val build_uuid = res.string(Pending.build_uuid)
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   683
          Task(name, split_lines(deps), JSON.Object.parse(info), build_uuid)
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   684
        })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   685
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   686
    def update_pending(db: SQL.Database, pending: State.Pending): Boolean = {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   687
      val old_pending = read_pending(db)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   688
      val (delete, insert) = Library.symmetric_difference(old_pending, pending)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   689
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   690
      if (delete.nonEmpty) {
77540
c537905c2125 tuned signature;
wenzelm
parents: 77539
diff changeset
   691
        db.execute_statement(
77662
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   692
          Pending.table.delete(sql = Generic.sql_where(names = delete.map(_.name))))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   693
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   694
77661
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   695
      for (task <- insert) {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   696
        db.execute_statement(Pending.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   697
          { stmt =>
77661
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   698
            stmt.string(1) = task.name
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   699
            stmt.string(2) = cat_lines(task.deps)
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   700
            stmt.string(3) = JSON.Format(task.info)
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
   701
            stmt.string(4) = task.build_uuid
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   702
          })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   703
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   704
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   705
      delete.nonEmpty || insert.nonEmpty
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   706
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   707
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   708
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   709
    /* running jobs */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   710
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   711
    object Running {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   712
      val name = Generic.name.make_primary_key
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   713
      val worker_uuid = Generic.worker_uuid
77634
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
   714
      val build_uuid = Generic.build_uuid
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   715
      val hostname = SQL.Column.string("hostname")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   716
      val numa_node = SQL.Column.int("numa_node")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   717
77634
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
   718
      val table = make_table("running", List(name, worker_uuid, build_uuid, hostname, numa_node))
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   719
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   720
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   721
    def read_running(db: SQL.Database): State.Running =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   722
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   723
        Running.table.select(sql = SQL.order_by(List(Running.name))),
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   724
        Map.from[String, Job],
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   725
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   726
          val name = res.string(Running.name)
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   727
          val worker_uuid = res.string(Running.worker_uuid)
77634
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
   728
          val build_uuid = res.string(Running.build_uuid)
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   729
          val hostname = res.string(Running.hostname)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   730
          val numa_node = res.get_int(Running.numa_node)
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   731
          name -> Job(name, worker_uuid, build_uuid, Host.Node_Info(hostname, numa_node), None)
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   732
        }
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   733
      )
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   734
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   735
    def update_running(db: SQL.Database, running: State.Running): Boolean = {
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   736
      val running0 = read_running(db).valuesIterator.toList
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   737
      val running1 = running.valuesIterator.map(_.no_build).toList
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   738
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   739
      val (delete, insert) = Library.symmetric_difference(running0, running1)
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   740
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   741
      if (delete.nonEmpty) {
77540
c537905c2125 tuned signature;
wenzelm
parents: 77539
diff changeset
   742
        db.execute_statement(
77662
b45fc98d11ea tuned signature;
wenzelm
parents: 77661
diff changeset
   743
          Running.table.delete(sql = Generic.sql_where(names = delete.map(_.name))))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   744
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   745
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   746
      for (job <- insert) {
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   747
        db.execute_statement(Running.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   748
          { stmt =>
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
   749
            stmt.string(1) = job.name
77587
8036d5f12997 more database content, e.g. for monitoring;
wenzelm
parents: 77586
diff changeset
   750
            stmt.string(2) = job.worker_uuid
77634
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
   751
            stmt.string(3) = job.build_uuid
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
   752
            stmt.string(4) = job.node_info.hostname
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
   753
            stmt.int(5) = job.node_info.numa_node
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   754
          })
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
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   757
      delete.nonEmpty || insert.nonEmpty
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
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   760
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   761
    /* job results */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   762
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   763
    object Results {
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   764
      val name = Generic.name.make_primary_key
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   765
      val worker_uuid = Generic.worker_uuid
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   766
      val build_uuid = Generic.build_uuid
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   767
      val hostname = SQL.Column.string("hostname")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   768
      val numa_node = SQL.Column.string("numa_node")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   769
      val rc = SQL.Column.int("rc")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   770
      val out = SQL.Column.string("out")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   771
      val err = SQL.Column.string("err")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   772
      val timing_elapsed = SQL.Column.long("timing_elapsed")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   773
      val timing_cpu = SQL.Column.long("timing_cpu")
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   774
      val timing_gc = SQL.Column.long("timing_gc")
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   775
      val output_shasum = SQL.Column.string("output_shasum")
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   776
      val current = SQL.Column.bool("current")
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   777
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   778
      val table =
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   779
        make_table("results",
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   780
          List(name, worker_uuid, build_uuid, hostname, numa_node,
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   781
            rc, out, err, timing_elapsed, timing_cpu, timing_gc, output_shasum, current))
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   782
    }
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   783
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   784
    def read_results_domain(db: SQL.Database): Set[String] =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   785
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   786
        Results.table.select(List(Results.name)),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   787
        Set.from[String], res => res.string(Results.name))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   788
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   789
    def read_results(db: SQL.Database, names: Iterable[String] = Nil): State.Results =
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   790
      db.execute_query_statement(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   791
        Results.table.select(sql = if_proper(names, Results.name.where_member(names))),
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   792
        Map.from[String, Result],
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   793
        { res =>
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   794
          val name = res.string(Results.name)
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   795
          val worker_uuid = res.string(Results.worker_uuid)
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   796
          val build_uuid = res.string(Results.build_uuid)
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   797
          val hostname = res.string(Results.hostname)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   798
          val numa_node = res.get_int(Results.numa_node)
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   799
          val node_info = Host.Node_Info(hostname, numa_node)
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   800
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   801
          val rc = res.int(Results.rc)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   802
          val out = res.string(Results.out)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   803
          val err = res.string(Results.err)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   804
          val timing =
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   805
            res.timing(
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   806
              Results.timing_elapsed,
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   807
              Results.timing_cpu,
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   808
              Results.timing_gc)
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   809
          val process_result =
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   810
            Process_Result(rc,
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   811
              out_lines = split_lines(out),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   812
              err_lines = split_lines(err),
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   813
              timing = timing)
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   814
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   815
          val output_shasum = SHA1.fake_shasum(res.string(Results.output_shasum))
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   816
          val current = res.bool(Results.current)
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   817
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   818
          name ->
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   819
            Result(name, worker_uuid, build_uuid, node_info, process_result, output_shasum, current)
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   820
        }
77552
080422b3d914 clarified signature: reduce boilerplate;
wenzelm
parents: 77551
diff changeset
   821
      )
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   822
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   823
    def update_results(db: SQL.Database, results: State.Results): Boolean = {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   824
      val old_results = read_results_domain(db)
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   825
      val insert = results.valuesIterator.filterNot(res => old_results.contains(res.name)).toList
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   826
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   827
      for (result <- insert) {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   828
        val process_result = result.process_result
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   829
        db.execute_statement(Results.table.insert(), body =
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   830
          { stmt =>
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   831
            stmt.string(1) = result.name
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   832
            stmt.string(2) = result.worker_uuid
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   833
            stmt.string(3) = result.build_uuid
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   834
            stmt.string(4) = result.node_info.hostname
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   835
            stmt.int(5) = result.node_info.numa_node
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   836
            stmt.int(6) = process_result.rc
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   837
            stmt.string(7) = cat_lines(process_result.out_lines)
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   838
            stmt.string(8) = cat_lines(process_result.err_lines)
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   839
            stmt.long(9) = process_result.timing.elapsed.ms
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   840
            stmt.long(10) = process_result.timing.cpu.ms
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   841
            stmt.long(11) = process_result.timing.gc.ms
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   842
            stmt.string(12) = result.output_shasum.toString
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
   843
            stmt.bool(13) = result.current
77541
9d9b30741fc4 tuned signature: reduce boilerplate;
wenzelm
parents: 77540
diff changeset
   844
          })
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   845
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   846
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   847
      insert.nonEmpty
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   848
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   849
77531
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   850
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   851
    /* collective operations */
3481e54bd8f1 tuned comments;
wenzelm
parents: 77530
diff changeset
   852
77596
dd8b08729458 clarified signature;
wenzelm
parents: 77594
diff changeset
   853
    val all_tables: SQL.Tables =
dd8b08729458 clarified signature;
wenzelm
parents: 77594
diff changeset
   854
      SQL.Tables(
77534
wenzelm
parents: 77533
diff changeset
   855
        Base.table,
wenzelm
parents: 77533
diff changeset
   856
        Workers.table,
wenzelm
parents: 77533
diff changeset
   857
        Progress.table,
wenzelm
parents: 77533
diff changeset
   858
        Sessions.table,
wenzelm
parents: 77533
diff changeset
   859
        Pending.table,
wenzelm
parents: 77533
diff changeset
   860
        Running.table,
wenzelm
parents: 77533
diff changeset
   861
        Results.table,
wenzelm
parents: 77533
diff changeset
   862
        Host.Data.Node_Info.table)
wenzelm
parents: 77533
diff changeset
   863
77658
wenzelm
parents: 77657
diff changeset
   864
    val build_uuid_tables =
wenzelm
parents: 77657
diff changeset
   865
      all_tables.filter(table =>
wenzelm
parents: 77657
diff changeset
   866
        table.columns.exists(column => column.name == Generic.build_uuid.name))
wenzelm
parents: 77657
diff changeset
   867
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   868
    def pull_database(
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   869
      db: SQL.Database,
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   870
      worker_uuid: String,
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   871
      hostname: String,
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   872
      state: State
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   873
    ): State = {
77655
136ab737a36d tuned signature;
wenzelm
parents: 77654
diff changeset
   874
      val serial_db = read_serial(db)
136ab737a36d tuned signature;
wenzelm
parents: 77654
diff changeset
   875
      if (serial_db == state.serial) state
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   876
      else {
77655
136ab737a36d tuned signature;
wenzelm
parents: 77654
diff changeset
   877
        val serial = serial_db max state.serial
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   878
        stamp_worker(db, worker_uuid, serial)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   879
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   880
        val numa_next = Host.Data.read_numa_next(db, hostname)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   881
        val sessions = pull1(read_sessions_domain(db), read_sessions(db, _), state.sessions)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   882
        val pending = read_pending(db)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   883
        val running = pull0(read_running(db), state.running)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   884
        val results = pull1(read_results_domain(db), read_results(db, _), state.results)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   885
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   886
        state.copy(serial = serial, numa_next = numa_next, sessions = sessions,
77657
a2a4adc268b8 removed redundant State.workers: directly maintained within the database, using with SQL update;
wenzelm
parents: 77656
diff changeset
   887
          pending = pending, running = running, results = results)
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   888
      }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   889
    }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   890
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   891
    def update_database(
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   892
      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
   893
      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
   894
      build_uuid: String,
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   895
      hostname: String,
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   896
      state: State
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   897
    ): State = {
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   898
      val changed =
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   899
        List(
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   900
          update_sessions(db, state.sessions),
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   901
          update_pending(db, state.pending),
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   902
          update_running(db, state.running),
77476
5f6f567a2661 clarified modules;
wenzelm
parents: 77475
diff changeset
   903
          update_results(db, state.results),
77525
de6fb423fd4b clarified database content: store actual value instead of index;
wenzelm
parents: 77522
diff changeset
   904
          Host.Data.update_numa_next(db, hostname, state.numa_next))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   905
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   906
      val serial0 = state.serial
77513
43bfb65ee9b3 more robust: proper bound checks;
wenzelm
parents: 77511
diff changeset
   907
      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
   908
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
   909
      stamp_worker(db, worker_uuid, serial)
77657
a2a4adc268b8 removed redundant State.workers: directly maintained within the database, using with SQL update;
wenzelm
parents: 77656
diff changeset
   910
      state.set_serial(serial)
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   911
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   912
  }
77396
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
   913
}
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   914
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   915
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   916
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   917
/** main process **/
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   918
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   919
class Build_Process(
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   920
  protected final val build_context: Build_Process.Context,
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   921
  protected final val build_progress: Progress
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   922
)
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   923
extends AutoCloseable {
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   924
  /* context */
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   925
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   926
  protected final val store: Sessions.Store = build_context.store
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   927
  protected final val build_options: Options = store.options
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   928
  protected final val build_deps: Sessions.Deps = build_context.build_deps
77653
26bb79d17910 tuned signature;
wenzelm
parents: 77652
diff changeset
   929
  protected final val hostname: String = build_context.hostname
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   930
  protected final val build_uuid: String = build_context.build_uuid
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   931
  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
   932
77638
a4266d54ec35 tuned output;
wenzelm
parents: 77637
diff changeset
   933
  override def toString: String =
a4266d54ec35 tuned output;
wenzelm
parents: 77637
diff changeset
   934
    "Build_Process(worker_uuid = " + quote(worker_uuid) + ", build_uuid = " + quote(build_uuid) +
a4266d54ec35 tuned output;
wenzelm
parents: 77637
diff changeset
   935
      if_proper(build_context.master, ", master = true") + ")"
a4266d54ec35 tuned output;
wenzelm
parents: 77637
diff changeset
   936
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   937
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   938
  /* global state: internal var vs. external database */
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   939
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   940
  private var _state: Build_Process.State = Build_Process.State()
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   941
77629
979baa91da0f clarified modules;
wenzelm
parents: 77610
diff changeset
   942
  private val _database: Option[SQL.Database] = store.open_build_database()
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   943
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   944
  def close(): Unit = synchronized { _database.foreach(_.close()) }
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   945
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   946
  protected def synchronized_database[A](body: => A): A =
77438
0030eabbe6c3 more robust: synchronized access to database;
wenzelm
parents: 77437
diff changeset
   947
    synchronized {
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   948
      _database match {
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   949
        case None => body
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   950
        case Some(db) =>
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   951
          def pull_database(): Unit = {
77653
26bb79d17910 tuned signature;
wenzelm
parents: 77652
diff changeset
   952
            _state = Build_Process.Data.pull_database(db, worker_uuid, hostname, _state)
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   953
          }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   954
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   955
          def sync_database(): Unit = {
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   956
            _state =
77653
26bb79d17910 tuned signature;
wenzelm
parents: 77652
diff changeset
   957
              Build_Process.Data.update_database(db, worker_uuid, build_uuid, hostname, _state)
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   958
          }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   959
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   960
          def attempt(): Either[A, Build_Process.Progress_Messages] = {
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   961
            val (messages, sync) =
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   962
              Build_Process.Data.sync_progress(
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   963
                db, _state.progress_seen, build_uuid, build_progress)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   964
            if (sync) Left { pull_database(); val res = body; sync_database(); res }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   965
            else Right(messages)
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   966
          }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   967
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   968
          @tailrec def attempts(): A = {
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   969
            db.transaction_lock(Build_Process.Data.all_tables) { attempt() } match {
77637
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   970
              case Left(res) => res
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   971
              case Right(messages) =>
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   972
                for ((message_serial, message) <- messages) {
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   973
                  _state = _state.progress_serial(message_serial = message_serial)
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   974
                  if (build_progress.do_output(message)) build_progress.output(message)
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   975
                }
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   976
                attempts()
77637
afb1a19307c4 synchronize progress messages with database;
wenzelm
parents: 77636
diff changeset
   977
            }
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
   978
          }
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
   979
          attempts()
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   980
      }
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   981
    }
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   982
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   983
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   984
  /* progress backed by database */
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   985
77533
wenzelm
parents: 77531
diff changeset
   986
  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
   987
    synchronized_database {
77640
9ed8b85e7d67 more direct state update;
wenzelm
parents: 77639
diff changeset
   988
      _state = _state.inc_serial.progress_serial()
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   989
      for (db <- _database) {
77640
9ed8b85e7d67 more direct state update;
wenzelm
parents: 77639
diff changeset
   990
        Build_Process.Data.write_progress(db, _state.serial, message, build_uuid)
9ed8b85e7d67 more direct state update;
wenzelm
parents: 77639
diff changeset
   991
        Build_Process.Data.stamp_worker(db, worker_uuid, _state.serial)
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   992
      }
77533
wenzelm
parents: 77531
diff changeset
   993
      build_progress_output
77522
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   994
    }
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   995
  }
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
   996
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
   997
  protected object progress extends Progress {
77509
3bc49507bae5 clarified treatment of "verbose" messages, e.g. Progress.theory();
wenzelm
parents: 77505
diff changeset
   998
    override def verbose: Boolean = build_progress.verbose
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   999
77509
3bc49507bae5 clarified treatment of "verbose" messages, e.g. Progress.theory();
wenzelm
parents: 77505
diff changeset
  1000
    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
  1001
      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
  1002
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
  1003
    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
  1004
      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
  1005
a1d30297cd61 more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents: 77521
diff changeset
  1006
    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
  1007
      build_progress.nodes_status(nodes_status)
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
  1008
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
  1009
    override def stop(): Unit = build_progress.stop()
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
  1010
    override def stopped: Boolean = build_progress.stopped
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
  1011
  }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
  1012
77530
3362f84a300a tuned signature: avoid totally adhoc overriding;
wenzelm
parents: 77529
diff changeset
  1013
  protected val log: Logger = Logger.make_system_log(progress, build_options)
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
  1014
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
  1015
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
  1016
  /* policy operations */
77333
0bec014c0f9f tuned signature;
wenzelm
parents: 77332
diff changeset
  1017
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
  1018
  protected def init_state(state: Build_Process.State): Build_Process.State = {
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1019
    val sessions1 =
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1020
      build_context.sessions.foldLeft(state.sessions) { case (map, (name, session)) =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1021
        if (state.sessions.isDefinedAt(name)) map
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1022
        else map + (name -> session)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1023
      }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1024
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
  1025
    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
  1026
    val new_pending =
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
  1027
      List.from(
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
  1028
        for {
77449
wenzelm
parents: 77448
diff changeset
  1029
          (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
  1030
          if !old_pending(name)
77661
45bd5c26cbcc proper build_uuid for Build_Process.Task: thus old entries are removed via prepare_database/clean_build;
wenzelm
parents: 77660
diff changeset
  1031
        } yield Build_Process.Task(name, session_context.deps, JSON.Object.empty, build_uuid))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1032
    val pending1 = new_pending ::: state.pending
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1033
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
  1034
    state.copy(sessions = sessions1, pending = pending1)
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
  1035
  }
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
  1036
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
  1037
  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
  1038
    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
  1039
      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
  1040
        .sortBy(_.name)(build_context.ordering)
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
  1041
        .headOption.map(_.name)
77296
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
  1042
    }
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
  1043
    else None
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
  1044
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1045
  protected def start_session(state: Build_Process.State, session_name: String): Build_Process.State = {
77468
wenzelm
parents: 77467
diff changeset
  1046
    val ancestor_results =
wenzelm
parents: 77467
diff changeset
  1047
      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
  1048
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
  1049
    val input_shasum =
77650
b1ca8975490a clarified modules;
wenzelm
parents: 77649
diff changeset
  1050
      if (ancestor_results.isEmpty) ML_Process.bootstrap_shasum()
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
  1051
      else SHA1.flat_shasum(ancestor_results.map(_.output_shasum))
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1052
77463
4e8bec105ce5 tuned signature;
wenzelm
parents: 77462
diff changeset
  1053
    val store_heap = build_context.store_heap(session_name)
77469
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
  1054
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
  1055
    val (current, output_shasum) =
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
  1056
      store.check_output(session_name,
77675
9e5f8f6e58a0 more thorough treatment of build prefs, guarded by system option "build_through": avoid accidental rebuild of HOL etc.;
wenzelm
parents: 77664
diff changeset
  1057
        session_options = build_context.sessions_structure(session_name).options,
77469
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
  1058
        sources_shasum = build_context.sources_shasum(session_name),
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
  1059
        input_shasum = input_shasum,
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
  1060
        fresh_build = build_context.fresh_build,
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
  1061
        store_heap = store_heap)
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
  1062
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
  1063
    val all_current = current && ancestor_results.forall(_.current)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1064
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
  1065
    val result_name = (session_name, worker_uuid, build_uuid)
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
  1066
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
  1067
    if (all_current) {
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1068
      state
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1069
        .remove_pending(session_name)
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
  1070
        .make_result(result_name, Process_Result.ok, output_shasum, current = true)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
  1071
    }
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
  1072
    else if (build_context.no_build) {
77521
5642de4d225d clarified signature: manage "verbose" flag via "progress";
wenzelm
parents: 77514
diff changeset
  1073
      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
  1074
      state.
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1075
        remove_pending(session_name).
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
  1076
        make_result(result_name, Process_Result.error, output_shasum)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
  1077
    }
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
  1078
    else if (progress.stopped || !ancestor_results.forall(_.ok)) {
77452
wenzelm
parents: 77449
diff changeset
  1079
      progress.echo(session_name + " CANCELLED")
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1080
      state
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1081
        .remove_pending(session_name)
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
  1082
        .make_result(result_name, Process_Result.undefined, output_shasum)
77452
wenzelm
parents: 77449
diff changeset
  1083
    }
wenzelm
parents: 77449
diff changeset
  1084
    else {
77632
f7208db921c2 tuned signature;
wenzelm
parents: 77631
diff changeset
  1085
      val (numa_node, state1) = state.next_numa_node(build_context.numa_nodes)
77653
26bb79d17910 tuned signature;
wenzelm
parents: 77652
diff changeset
  1086
      val node_info = Host.Node_Info(hostname, numa_node)
77551
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
  1087
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
  1088
      progress.echo(
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
  1089
        (if (store_heap) "Building " else "Running ") + session_name +
ae6df8a8685a clarified messages;
wenzelm
parents: 77546
diff changeset
  1090
          if_proper(node_info.numa_node, " on " + node_info) + " ...")
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
  1091
77472
a073ac3f3b56 clarified modules;
wenzelm
parents: 77470
diff changeset
  1092
      store.init_output(session_name)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1093
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
  1094
      val build =
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
  1095
        Build_Job.start_session(build_context, progress, log,
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
  1096
          build_deps.background(session_name), input_shasum, node_info)
77630
86ef80d13544 clarified signature: avoid confusion due to object-orientation;
wenzelm
parents: 77629
diff changeset
  1097
77634
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
  1098
      val job = Build_Process.Job(session_name, worker_uuid, build_uuid, node_info, Some(build))
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
  1099
50fc9143ccfa more database content;
wenzelm
parents: 77633
diff changeset
  1100
      state1.add_running(job)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
  1101
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
  1102
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1103
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
  1104
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1105
  /* build process roles */
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1106
77654
78913f29fc21 tuned signature;
wenzelm
parents: 77653
diff changeset
  1107
  final def is_session_name(job_name: String): Boolean =
78913f29fc21 tuned signature;
wenzelm
parents: 77653
diff changeset
  1108
    !Long_Name.is_qualified(job_name)
77648
e79a5ce8a74c clarified modules;
wenzelm
parents: 77646
diff changeset
  1109
77654
78913f29fc21 tuned signature;
wenzelm
parents: 77653
diff changeset
  1110
  protected final def start_build(): Unit = synchronized_database {
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1111
    for (db <- _database) {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1112
      Build_Process.Data.start_build(db, build_uuid, build_context.ml_platform,
77635
dcd2c3bb4b68 synchronize progress stop/stopped with database;
wenzelm
parents: 77634
diff changeset
  1113
        build_context.sessions_structure.session_prefs, progress.stopped)
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1114
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1115
  }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1116
77654
78913f29fc21 tuned signature;
wenzelm
parents: 77653
diff changeset
  1117
  protected final def stop_build(): Unit = synchronized_database {
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1118
    for (db <- _database) {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1119
      Build_Process.Data.stop_build(db, build_uuid)
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1120
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1121
  }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1122
77654
78913f29fc21 tuned signature;
wenzelm
parents: 77653
diff changeset
  1123
  protected final def start_worker(): Unit = synchronized_database {
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1124
    for (db <- _database) {
77584
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
  1125
      val java = ProcessHandle.current()
42922317b676 more database content, e.g. for monitoring;
wenzelm
parents: 77583
diff changeset
  1126
      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
  1127
      val java_start = Date.instant(java.info.startInstant.get)
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
  1128
      _state = _state.inc_serial
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
  1129
      Build_Process.Data.start_worker(
77653
26bb79d17910 tuned signature;
wenzelm
parents: 77652
diff changeset
  1130
        db, worker_uuid, build_uuid, hostname, java_pid, java_start, _state.serial)
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1131
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1132
  }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1133
77654
78913f29fc21 tuned signature;
wenzelm
parents: 77653
diff changeset
  1134
  protected final def stop_worker(): Unit = synchronized_database {
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1135
    for (db <- _database) {
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1136
      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
  1137
    }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1138
  }
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1139
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1140
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
  1141
  /* run */
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
  1142
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
  1143
  def run(): Map[String, Process_Result] = {
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
  1144
    if (build_context.master) synchronized_database { _state = init_state(_state) }
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
  1145
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
  1146
    def finished(): Boolean = synchronized_database { _state.finished }
77400
f3e5b3fe230e clarified signature: more explicit "synchronized" regions;
wenzelm
parents: 77398
diff changeset
  1147
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
  1148
    def sleep(): Unit =
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
  1149
      Isabelle_Thread.interrupt_handler(_ => progress.stop()) {
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
  1150
        build_options.seconds("editor_input_delay").sleep()
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
  1151
      }
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
  1152
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1153
    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
  1154
      next_job(_state) match {
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1155
        case Some(name) =>
77648
e79a5ce8a74c clarified modules;
wenzelm
parents: 77646
diff changeset
  1156
          if (is_session_name(name)) {
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1157
            _state = start_session(_state, name)
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1158
            true
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1159
          }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1160
          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
  1161
        case None => false
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1162
      }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1163
    }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1164
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
  1165
    if (finished()) {
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
  1166
      progress.echo_warning("Nothing to build")
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
  1167
      Map.empty[String, Process_Result]
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
  1168
    }
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
  1169
    else {
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
  1170
      if (build_context.master) start_build()
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
  1171
77580
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
  1172
      start_worker()
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
  1173
      if (build_context.master && !build_context.worker_active) {
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
  1174
        progress.echo("Waiting for external workers ...")
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
  1175
      }
77578
149d48a4801b support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents: 77561
diff changeset
  1176
77538
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1177
      try {
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1178
        while (!finished()) {
77639
d5344cc1fae7 avoid too many synchronized_database;
wenzelm
parents: 77638
diff changeset
  1179
          synchronized_database {
d5344cc1fae7 avoid too many synchronized_database;
wenzelm
parents: 77638
diff changeset
  1180
            if (progress.stopped) _state.stop_running()
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
  1181
77649
739cb777cc75 clarified signature;
wenzelm
parents: 77648
diff changeset
  1182
            for (job <- _state.finished_running()) {
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
  1183
              val result_name = (job.name, worker_uuid, build_uuid)
77649
739cb777cc75 clarified signature;
wenzelm
parents: 77648
diff changeset
  1184
              val (process_result, output_shasum) = job.build.get.join
77538
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1185
              _state = _state.
77649
739cb777cc75 clarified signature;
wenzelm
parents: 77648
diff changeset
  1186
                remove_pending(job.name).
739cb777cc75 clarified signature;
wenzelm
parents: 77648
diff changeset
  1187
                remove_running(job.name).
77651
b7fe1d822dc1 more database content;
wenzelm
parents: 77650
diff changeset
  1188
                make_result(result_name, process_result, output_shasum, node_info = job.node_info)
77538
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1189
            }
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1190
          }
fcda9a009213 more database content: formal end_build;
wenzelm
parents: 77536
diff changeset
  1191
77652
5f706f7c624b more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents: 77651
diff changeset
  1192
          if (!start_job()) sleep()
77396
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
  1193
        }
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
  1194
      }
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1195
      finally {
77580
32f9e75c92e9 clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents: 77579
diff changeset
  1196
        stop_worker()
77579
69d3547206db clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents: 77578
diff changeset
  1197
        if (build_context.master) stop_build()
77546
9b9179cda155 clarified build process roles: "worker" vs. "build";
wenzelm
parents: 77545
diff changeset
  1198
      }
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
  1199
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
  1200
      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
  1201
        for ((name, result) <- _state.results) yield name -> result.process_result
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1202
      }
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1203
    }
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
  1204
  }
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1205
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1206
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1207
  /* snapshot */
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1208
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1209
  def snapshot(): Build_Process.Snapshot = synchronized_database {
77660
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
  1210
    val (progress_messages, builds, workers) =
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1211
      _database match {
77660
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
  1212
        case None => (Build_Process.progress_messages_empty, Nil, Nil)
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1213
        case Some(db) =>
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1214
          (Build_Process.Data.read_progress(db),
77660
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
  1215
           Build_Process.Data.read_builds(db),
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1216
           Build_Process.Data.read_workers(db))
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1217
      }
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1218
    Build_Process.Snapshot(
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1219
      progress_messages = progress_messages,
77660
57fdb6c846b0 more informative Build_Process.Snapshot;
wenzelm
parents: 77659
diff changeset
  1220
      builds = builds,
77659
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1221
      workers = workers,
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1222
      sessions = _state.sessions,
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1223
      pending = _state.pending,
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1224
      running = _state.running,
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1225
      results = _state.results)
d7eb6a4522b8 more explicit snapshot of "_state" and "_database";
wenzelm
parents: 77658
diff changeset
  1226
  }
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
  1227
}