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