src/Pure/Tools/build_process.scala
author wenzelm
Sat, 04 Mar 2023 16:45:21 +0100
changeset 77505 7ee426daafa3
parent 77496 f0d9f9196b9b
child 77509 3bc49507bae5
permissions -rw-r--r--
support progress backed by database; moved Build_Progress.Context.progress/log to class Build_Process: database is available here;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/build_process.scala
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     3
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     4
Build process for sessions, with build database, optional heap, and
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     5
optional presentation.
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     6
*/
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     7
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     8
package isabelle
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
     9
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    10
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
    11
import scala.collection.immutable.SortedMap
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    12
import scala.math.Ordering
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
    13
import scala.annotation.tailrec
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    14
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    15
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    16
object Build_Process {
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
    17
  /** static context **/
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    18
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    19
  object Context {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    20
    def apply(
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    21
      store: Sessions.Store,
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
    22
      build_deps: Sessions.Deps,
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    23
      progress: Progress = new Progress,
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
    24
      hostname: String = Isabelle_System.hostname(),
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
    25
      numa_shuffling: Boolean = false,
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    26
      build_heap: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    27
      max_jobs: Int = 1,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    28
      fresh_build: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    29
      no_build: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    30
      verbose: Boolean = false,
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
    31
      session_setup: (String, Session) => Unit = (_, _) => (),
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
    32
      uuid: String = UUID.random().toString
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    33
    ): Context = {
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
    34
      val sessions_structure = build_deps.sessions_structure
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    35
      val build_graph = sessions_structure.build_graph
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    36
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    37
      val sessions =
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    38
        Map.from(
77448
wenzelm
parents: 77447
diff changeset
    39
          for ((name, (info, _)) <- build_graph.iterator)
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    40
          yield {
77447
566e6e393126 proper deps from build_graph, not imports_graph (amending 0c704aba71e3);
wenzelm
parents: 77446
diff changeset
    41
            val deps = info.parent.toList
566e6e393126 proper deps from build_graph, not imports_graph (amending 0c704aba71e3);
wenzelm
parents: 77446
diff changeset
    42
            val ancestors = sessions_structure.build_requirements(deps)
77458
403748b23f13 clarified signature: prefer static data;
wenzelm
parents: 77457
diff changeset
    43
            val sources_shasum = build_deps.sources_shasum(name)
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
    44
            val session_context =
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
    45
              Build_Job.Session_Context.load(
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
    46
                uuid, name, deps, ancestors, sources_shasum, info.timeout, store,
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
    47
                progress = progress)
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
    48
            name -> session_context
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    49
          })
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    50
77248
wenzelm
parents: 77247
diff changeset
    51
      val sessions_time = {
wenzelm
parents: 77247
diff changeset
    52
        val maximals = build_graph.maximals.toSet
wenzelm
parents: 77247
diff changeset
    53
        def descendants_time(name: String): Double = {
wenzelm
parents: 77247
diff changeset
    54
          if (maximals.contains(name)) sessions(name).old_time.seconds
wenzelm
parents: 77247
diff changeset
    55
          else {
wenzelm
parents: 77247
diff changeset
    56
            val descendants = build_graph.all_succs(List(name)).toSet
wenzelm
parents: 77247
diff changeset
    57
            val g = build_graph.restrict(descendants)
wenzelm
parents: 77247
diff changeset
    58
            (0.0 :: g.maximals.flatMap { desc =>
wenzelm
parents: 77247
diff changeset
    59
              val ps = g.all_preds(List(desc))
wenzelm
parents: 77247
diff changeset
    60
              if (ps.exists(p => !sessions.isDefinedAt(p))) None
wenzelm
parents: 77247
diff changeset
    61
              else Some(ps.map(p => sessions(p).old_time.seconds).sum)
wenzelm
parents: 77247
diff changeset
    62
            }).max
wenzelm
parents: 77247
diff changeset
    63
          }
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    64
        }
77248
wenzelm
parents: 77247
diff changeset
    65
        Map.from(
wenzelm
parents: 77247
diff changeset
    66
          for (name <- sessions.keysIterator)
wenzelm
parents: 77247
diff changeset
    67
          yield name -> descendants_time(name)).withDefaultValue(0.0)
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    68
      }
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    69
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    70
      val ordering =
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    71
        new Ordering[String] {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    72
          def compare(name1: String, name2: String): Int =
77248
wenzelm
parents: 77247
diff changeset
    73
            sessions_time(name2) compare sessions_time(name1) match {
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    74
              case 0 =>
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    75
                sessions(name2).timeout compare sessions(name1).timeout match {
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    76
                  case 0 => name1 compare name2
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    77
                  case ord => ord
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    78
                }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    79
              case ord => ord
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    80
            }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    81
        }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    82
77477
f376aebca9c1 clarified modules;
wenzelm
parents: 77476
diff changeset
    83
      val numa_nodes = Host.numa_nodes(enabled = numa_shuffling)
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
    84
      new Context(store, build_deps, sessions, ordering, hostname, numa_nodes,
77317
b8ec3c0455db clarified modules: NUMA is managed by Build_Process;
wenzelm
parents: 77315
diff changeset
    85
        build_heap = build_heap, max_jobs = max_jobs, fresh_build = fresh_build,
77457
8c749bbf885c tuned signature;
wenzelm
parents: 77456
diff changeset
    86
        no_build = no_build, verbose = verbose, session_setup, uuid = uuid)
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    87
    }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    88
  }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    89
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
    90
  // static context of one particular instance, identified by uuid
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    91
  final class Context private(
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
    92
    val store: Sessions.Store,
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
    93
    val build_deps: Sessions.Deps,
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
    94
    val sessions: State.Sessions,
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
    95
    val ordering: Ordering[String],
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
    96
    val hostname: String,
77317
b8ec3c0455db clarified modules: NUMA is managed by Build_Process;
wenzelm
parents: 77315
diff changeset
    97
    val numa_nodes: List[Int],
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    98
    val build_heap: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    99
    val max_jobs: Int,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   100
    val fresh_build: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   101
    val no_build: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   102
    val verbose: Boolean,
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
   103
    val session_setup: (String, Session) => Unit,
77457
8c749bbf885c tuned signature;
wenzelm
parents: 77456
diff changeset
   104
    val uuid: String
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   105
  ) {
77470
38503d9ff2e5 clarified modules;
wenzelm
parents: 77469
diff changeset
   106
    def build_options: Options = store.options
38503d9ff2e5 clarified modules;
wenzelm
parents: 77469
diff changeset
   107
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
   108
    def sessions_structure: Sessions.Structure = build_deps.sessions_structure
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   109
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   110
    def sources_shasum(name: String): SHA1.Shasum = sessions(name).sources_shasum
77458
403748b23f13 clarified signature: prefer static data;
wenzelm
parents: 77457
diff changeset
   111
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   112
    def old_command_timings(name: String): List[Properties.T] =
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   113
      sessions.get(name) match {
77444
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   114
        case Some(session_context) =>
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   115
          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
   116
        case None => Nil
0c704aba71e3 clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents: 77443
diff changeset
   117
      }
77439
d6bf9ec39d12 avoid premature Properties.uncompress: allow blob to be stored in another database;
wenzelm
parents: 77438
diff changeset
   118
77463
4e8bec105ce5 tuned signature;
wenzelm
parents: 77462
diff changeset
   119
    def store_heap(name: String): Boolean =
77462
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   120
      build_heap || Sessions.is_pure(name) ||
b474d39ddfee tuned signature;
wenzelm
parents: 77461
diff changeset
   121
      sessions.valuesIterator.exists(_.ancestors.contains(name))
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   122
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   123
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   124
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   125
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   126
  /** dynamic state **/
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   127
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   128
  type Progress_Messages = SortedMap[Long, Progress.Message]
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   129
77344
de7eae726f8e allow arbitrary info, e.g. for custom scheduler;
wenzelm
parents: 77343
diff changeset
   130
  case class Entry(name: String, deps: List[String], info: JSON.Object.T = JSON.Object.empty) {
77313
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   131
    def is_ready: Boolean = deps.isEmpty
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   132
    def resolve(dep: String): Entry =
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   133
      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
   134
  }
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   135
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   136
  case class Result(
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   137
    process_result: Process_Result,
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   138
    output_shasum: SHA1.Shasum,
77475
3bc611c80346 clarified modules;
wenzelm
parents: 77474
diff changeset
   139
    node_info: Host.Node_Info,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   140
    current: Boolean
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   141
  ) {
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   142
    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
   143
  }
77312
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   144
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   145
  object State {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   146
    type Sessions = Map[String, Build_Job.Session_Context]
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   147
    type Pending = List[Entry]
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   148
    type Running = Map[String, Build_Job]
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   149
    type Results = Map[String, Result]
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   150
  }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   151
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   152
  // dynamic state of various instances, distinguished by uuid
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   153
  sealed case class State(
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   154
    serial: Long = 0,
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   155
    progress_seen: Long = 0,
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   156
    numa_index: Int = 0,
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   157
    sessions: State.Sessions = Map.empty,   // static build targets
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   158
    pending: State.Pending = Nil,           // dynamic build "queue"
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   159
    running: State.Running = Map.empty,     // presently running jobs
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   160
    results: State.Results = Map.empty      // finished results
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   161
  ) {
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   162
    def echo(progress: Progress, message_serial: Long, message: Progress.Message): State =
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   163
      if (message_serial > progress_seen) {
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   164
        progress.echo(message)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   165
        copy(progress_seen = message_serial)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   166
      }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   167
      else {
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   168
        error("Bad serial " + message_serial + " (already seen) for progress message:\n" + message)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   169
      }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   170
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   171
    def numa_next(numa_nodes: List[Int]): (Option[Int], State) =
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   172
      if (numa_nodes.isEmpty) (None, this)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   173
      else {
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   174
        val available = numa_nodes.zipWithIndex
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   175
        val used =
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   176
          Set.from(for (job <- running.valuesIterator; i <- job.node_info.numa_node) yield i)
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   177
        val candidates = available.drop(numa_index) ::: available.take(numa_index)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   178
        val (n, i) =
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   179
          candidates.find({ case (n, i) => i == numa_index && !used(n) }) orElse
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   180
          candidates.find({ case (n, _) => !used(n) }) getOrElse candidates.head
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   181
        (Some(n), copy(numa_index = (i + 1) % available.length))
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   182
      }
77337
1ab68d4370ec tuned signature;
wenzelm
parents: 77336
diff changeset
   183
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   184
    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
   185
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   186
    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
   187
      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
   188
        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
   189
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   190
    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
   191
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77477
diff changeset
   192
    def stop_running(): Unit = running.valuesIterator.foreach(_.cancel())
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   193
77398
19e9cafaafc5 clarified signature: works for general Build_Job;
wenzelm
parents: 77396
diff changeset
   194
    def finished_running(): List[Build_Job] =
19e9cafaafc5 clarified signature: works for general Build_Job;
wenzelm
parents: 77396
diff changeset
   195
      List.from(running.valuesIterator.filter(_.is_finished))
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   196
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   197
    def add_running(name: String, job: Build_Job): State =
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   198
      copy(running = running + (name -> job))
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   199
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   200
    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
   201
      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
   202
77336
e9beaaf955bf tuned signature;
wenzelm
parents: 77335
diff changeset
   203
    def make_result(
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   204
      name: String,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   205
      process_result: Process_Result,
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   206
      output_shasum: SHA1.Shasum,
77475
3bc611c80346 clarified modules;
wenzelm
parents: 77474
diff changeset
   207
      node_info: Host.Node_Info = Host.Node_Info.none,
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   208
      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
   209
    ): State = {
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   210
      val entry = name -> Build_Process.Result(process_result, output_shasum, node_info, current)
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   211
      copy(results = results + entry)
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   212
    }
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   213
  }
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   214
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   215
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   216
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   217
  /** SQL data model **/
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   218
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   219
  object Data {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   220
    val database = Path.explode("$ISABELLE_HOME_USER/build.db")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   221
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   222
    def make_table(name: String, columns: List[SQL.Column], body: String = ""): SQL.Table =
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   223
      SQL.Table("isabelle_build" + if_proper(name, "_" + name), columns, body = body)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   224
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   225
    object Generic {
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   226
      val uuid = SQL.Column.string("uuid")
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   227
      val name = SQL.Column.string("name")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   228
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   229
      def sql_equal(uuid: String = "", name: String = ""): SQL.Source =
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   230
        SQL.and(
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   231
          if_proper(uuid, Generic.uuid.equal(uuid)),
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   232
          if_proper(name, Generic.name.equal(name)))
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   233
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   234
      def sql_member(uuid: String = "", names: Iterable[String] = Nil): SQL.Source =
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   235
        SQL.and(
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   236
          if_proper(uuid, Generic.uuid.equal(uuid)),
77375
324f5821a4a4 clarified signature: more concise operations;
wenzelm
parents: 77374
diff changeset
   237
          if_proper(names, Generic.name.member(names)))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   238
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   239
77417
wenzelm
parents: 77416
diff changeset
   240
    object Base {
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   241
      val uuid = Generic.uuid.make_primary_key
77387
cd10b8edfdf5 clarified db content: avoid redundancy of historic ML_IDENTIFIER;
wenzelm
parents: 77385
diff changeset
   242
      val ml_platform = SQL.Column.string("ml_platform")
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   243
      val options = SQL.Column.string("options")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   244
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   245
      val table = make_table("", List(uuid, ml_platform, options))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   246
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   247
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   248
    object Serial {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   249
      val serial = SQL.Column.long("serial")
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   250
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   251
      val table = make_table("serial", List(serial))
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   252
    }
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   253
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   254
    object Progress {
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   255
      val serial = SQL.Column.long("serial").make_primary_key
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   256
      val kind = SQL.Column.int("kind")
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   257
      val text = SQL.Column.string("text")
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   258
      val uuid = Generic.uuid
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   259
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   260
      val table = make_table("progress", List(serial, kind, text, uuid))
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   261
    }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   262
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   263
    object Sessions {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   264
      val name = Generic.name.make_primary_key
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   265
      val deps = SQL.Column.string("deps")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   266
      val ancestors = SQL.Column.string("ancestors")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   267
      val sources = SQL.Column.string("sources")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   268
      val timeout = SQL.Column.long("timeout")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   269
      val old_time = SQL.Column.long("old_time")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   270
      val old_command_timings = SQL.Column.bytes("old_command_timings")
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   271
      val uuid = Generic.uuid
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   272
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   273
      val table = make_table("sessions",
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   274
        List(name, deps, ancestors, sources, timeout, old_time, old_command_timings, uuid))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   275
    }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   276
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   277
    object Pending {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   278
      val name = Generic.name.make_primary_key
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   279
      val deps = SQL.Column.string("deps")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   280
      val info = SQL.Column.string("info")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   281
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   282
      val table = make_table("pending", List(name, deps, info))
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   283
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   284
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   285
    object Running {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   286
      val name = Generic.name.make_primary_key
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   287
      val hostname = SQL.Column.string("hostname")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   288
      val numa_node = SQL.Column.int("numa_node")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   289
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   290
      val table = make_table("running", List(name, hostname, numa_node))
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   291
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   292
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   293
    object Results {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   294
      val name = Generic.name.make_primary_key
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   295
      val hostname = SQL.Column.string("hostname")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   296
      val numa_node = SQL.Column.string("numa_node")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   297
      val rc = SQL.Column.int("rc")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   298
      val out = SQL.Column.string("out")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   299
      val err = SQL.Column.string("err")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   300
      val timing_elapsed = SQL.Column.long("timing_elapsed")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   301
      val timing_cpu = SQL.Column.long("timing_cpu")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   302
      val timing_gc = SQL.Column.long("timing_gc")
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   303
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   304
      val table =
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   305
        make_table("results",
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   306
          List(name, hostname, numa_node, rc, out, err, timing_elapsed, timing_cpu, timing_gc))
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   307
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   308
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   309
    def get_serial(db: SQL.Database): Long =
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   310
      db.using_statement(Serial.table.select())(stmt =>
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   311
        stmt.execute_query().iterator(_.long(Serial.serial)).nextOption.getOrElse(0L))
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   312
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   313
    def set_serial(db: SQL.Database, serial: Long): Unit =
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   314
      if (get_serial(db) != serial) {
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   315
        db.using_statement(Serial.table.delete())(_.execute())
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   316
        db.using_statement(Serial.table.insert()) { stmt =>
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   317
          stmt.long(1) = serial
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   318
          stmt.execute()
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   319
        }
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   320
      }
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   321
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   322
    def read_progress(db: SQL.Database, seen: Long = 0, uuid: String = ""): Progress_Messages =
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   323
      db.using_statement(
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   324
        Progress.table.select(
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   325
          sql =
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   326
            SQL.where(
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   327
              SQL.and(
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   328
                if (seen <= 0) "" else Progress.serial.ident + " > " + seen,
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   329
                Generic.sql_equal(uuid = uuid))))
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   330
      ) { stmt =>
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   331
          SortedMap.from(stmt.execute_query().iterator { res =>
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   332
            val serial = res.long(Progress.serial)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   333
            val kind = isabelle.Progress.Kind(res.int(Progress.kind))
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   334
            val text = res.string(Progress.text)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   335
            serial -> isabelle.Progress.Message(kind, text)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   336
          })
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   337
        }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   338
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   339
    def write_progress(
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   340
      db: SQL.Database,
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   341
      message_serial: Long,
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   342
      message: isabelle.Progress.Message,
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   343
      uuid: String
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   344
    ): Unit = {
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   345
      db.using_statement(Progress.table.insert()) { stmt =>
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   346
        stmt.long(1) = message_serial
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   347
        stmt.int(2) = message.kind.id
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   348
        stmt.string(3) = message.text
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   349
        stmt.string(4) = uuid
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   350
        stmt.execute()
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   351
      }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   352
    }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   353
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   354
    def read_sessions_domain(db: SQL.Database): Set[String] =
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   355
      db.using_statement(Sessions.table.select(List(Sessions.name)))(stmt =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   356
        Set.from(stmt.execute_query().iterator(_.string(Sessions.name))))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   357
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   358
    def read_sessions(db: SQL.Database, names: Iterable[String] = Nil): State.Sessions =
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   359
      db.using_statement(
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   360
        Sessions.table.select(sql = if_proper(names, Sessions.name.where_member(names)))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   361
      ) { stmt =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   362
          Map.from(stmt.execute_query().iterator { res =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   363
            val name = res.string(Sessions.name)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   364
            val deps = split_lines(res.string(Sessions.deps))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   365
            val ancestors = split_lines(res.string(Sessions.ancestors))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   366
            val sources_shasum = SHA1.fake_shasum(res.string(Sessions.sources))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   367
            val timeout = Time.ms(res.long(Sessions.timeout))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   368
            val old_time = Time.ms(res.long(Sessions.old_time))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   369
            val old_command_timings_blob = res.bytes(Sessions.old_command_timings)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   370
            val uuid = res.string(Sessions.uuid)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   371
            name -> Build_Job.Session_Context(name, deps, ancestors, sources_shasum,
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   372
              timeout, old_time, old_command_timings_blob, uuid)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   373
          })
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   374
        }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   375
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   376
    def update_sessions(db:SQL.Database, sessions: State.Sessions): Boolean = {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   377
      val old_sessions = read_sessions_domain(db)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   378
      val insert = sessions.iterator.filterNot(p => old_sessions.contains(p._1)).toList
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   379
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   380
      for ((name, session) <- insert) {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   381
        db.using_statement(Sessions.table.insert()) { stmt =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   382
          stmt.string(1) = name
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   383
          stmt.string(2) = cat_lines(session.deps)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   384
          stmt.string(3) = cat_lines(session.ancestors)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   385
          stmt.string(4) = session.sources_shasum.toString
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   386
          stmt.long(5) = session.timeout.ms
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   387
          stmt.long(6) = session.old_time.ms
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   388
          stmt.bytes(7) = session.old_command_timings_blob
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   389
          stmt.string(8) = session.uuid
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   390
          stmt.execute()
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   391
        }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   392
      }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   393
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   394
      insert.nonEmpty
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   395
    }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   396
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   397
    def read_pending(db: SQL.Database): List[Entry] =
77381
a86e346b20d8 misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents: 77380
diff changeset
   398
      db.using_statement(Pending.table.select(sql = SQL.order_by(List(Pending.name)))) { stmt =>
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   399
        List.from(
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   400
          stmt.execute_query().iterator { res =>
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   401
            val name = res.string(Pending.name)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   402
            val deps = res.string(Pending.deps)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   403
            val info = res.string(Pending.info)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   404
            Entry(name, split_lines(deps), info = JSON.Object.parse(info))
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   405
          })
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   406
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   407
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   408
    def update_pending(db: SQL.Database, pending: State.Pending): Boolean = {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   409
      val old_pending = read_pending(db)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   410
      val (delete, insert) = Library.symmetric_difference(old_pending, pending)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   411
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   412
      if (delete.nonEmpty) {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   413
        db.using_statement(
77381
a86e346b20d8 misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents: 77380
diff changeset
   414
          Pending.table.delete(
a86e346b20d8 misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents: 77380
diff changeset
   415
            sql = SQL.where(Generic.sql_member(names = delete.map(_.name)))))(_.execute())
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   416
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   417
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   418
      for (entry <- insert) {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   419
        db.using_statement(Pending.table.insert()) { stmt =>
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   420
          stmt.string(1) = entry.name
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   421
          stmt.string(2) = cat_lines(entry.deps)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   422
          stmt.string(3) = JSON.Format(entry.info)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   423
          stmt.execute()
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   424
        }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   425
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   426
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   427
      delete.nonEmpty || insert.nonEmpty
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   428
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   429
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   430
    def read_running(db: SQL.Database): List[Build_Job.Abstract] =
77381
a86e346b20d8 misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents: 77380
diff changeset
   431
      db.using_statement(Running.table.select(sql = SQL.order_by(List(Running.name)))) { stmt =>
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   432
        List.from(
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   433
          stmt.execute_query().iterator { res =>
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   434
            val name = res.string(Running.name)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   435
            val hostname = res.string(Running.hostname)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   436
            val numa_node = res.get_int(Running.numa_node)
77475
3bc611c80346 clarified modules;
wenzelm
parents: 77474
diff changeset
   437
            Build_Job.Abstract(name, Host.Node_Info(hostname, numa_node))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   438
          })
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   439
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   440
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   441
    def update_running(db: SQL.Database, running: State.Running): Boolean = {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   442
      val old_running = read_running(db)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   443
      val abs_running = running.valuesIterator.map(_.make_abstract).toList
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   444
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   445
      val (delete, insert) = Library.symmetric_difference(old_running, abs_running)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   446
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   447
      if (delete.nonEmpty) {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   448
        db.using_statement(
77381
a86e346b20d8 misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents: 77380
diff changeset
   449
          Running.table.delete(
a86e346b20d8 misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents: 77380
diff changeset
   450
            sql = SQL.where(Generic.sql_member(names = delete.map(_.job_name)))))(_.execute())
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   451
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   452
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   453
      for (job <- insert) {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   454
        db.using_statement(Running.table.insert()) { stmt =>
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   455
          stmt.string(1) = job.job_name
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   456
          stmt.string(2) = job.node_info.hostname
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   457
          stmt.int(3) = job.node_info.numa_node
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   458
          stmt.execute()
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   459
        }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   460
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   461
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   462
      delete.nonEmpty || insert.nonEmpty
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   463
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   464
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   465
    def read_results_domain(db: SQL.Database): Set[String] =
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   466
      db.using_statement(Results.table.select(List(Results.name)))(stmt =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   467
        Set.from(stmt.execute_query().iterator(_.string(Results.name))))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   468
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   469
    def read_results(db: SQL.Database, names: List[String] = Nil): Map[String, Build_Job.Result] =
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   470
      db.using_statement(
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   471
        Results.table.select(sql = if_proper(names, Results.name.where_member(names)))
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   472
      ) { stmt =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   473
          Map.from(stmt.execute_query().iterator { res =>
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   474
            val name = res.string(Results.name)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   475
            val hostname = res.string(Results.hostname)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   476
            val numa_node = res.get_int(Results.numa_node)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   477
            val rc = res.int(Results.rc)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   478
            val out = res.string(Results.out)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   479
            val err = res.string(Results.err)
77495
c546e3e1f7f6 tuned signature;
wenzelm
parents: 77486
diff changeset
   480
            val timing =
c546e3e1f7f6 tuned signature;
wenzelm
parents: 77486
diff changeset
   481
              res.timing(
c546e3e1f7f6 tuned signature;
wenzelm
parents: 77486
diff changeset
   482
                Results.timing_elapsed,
c546e3e1f7f6 tuned signature;
wenzelm
parents: 77486
diff changeset
   483
                Results.timing_cpu,
c546e3e1f7f6 tuned signature;
wenzelm
parents: 77486
diff changeset
   484
                Results.timing_gc)
77475
3bc611c80346 clarified modules;
wenzelm
parents: 77474
diff changeset
   485
            val node_info = Host.Node_Info(hostname, numa_node)
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   486
            val process_result =
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   487
              Process_Result(rc,
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   488
                out_lines = split_lines(out),
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   489
                err_lines = split_lines(err),
77495
c546e3e1f7f6 tuned signature;
wenzelm
parents: 77486
diff changeset
   490
                timing = timing)
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   491
            name -> Build_Job.Result(node_info, process_result)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   492
          })
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   493
        }
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   494
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   495
    def update_results(db: SQL.Database, results: State.Results): Boolean = {
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   496
      val old_results = read_results_domain(db)
77385
4a7c42c84743 proper filterNot, not filterNot-not;
wenzelm
parents: 77383
diff changeset
   497
      val insert = results.iterator.filterNot(p => old_results.contains(p._1)).toList
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   498
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   499
      for ((name, result) <- insert) {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   500
        val node_info = result.node_info
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   501
        val process_result = result.process_result
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   502
        db.using_statement(Results.table.insert()) { stmt =>
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   503
          stmt.string(1) = name
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   504
          stmt.string(2) = node_info.hostname
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   505
          stmt.int(3) = node_info.numa_node
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   506
          stmt.int(4) = process_result.rc
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   507
          stmt.string(5) = cat_lines(process_result.out_lines)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   508
          stmt.string(6) = cat_lines(process_result.err_lines)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   509
          stmt.long(7) = process_result.timing.elapsed.ms
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   510
          stmt.long(8) = process_result.timing.cpu.ms
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   511
          stmt.long(9) = process_result.timing.gc.ms
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   512
          stmt.execute()
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   513
        }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   514
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   515
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   516
      insert.nonEmpty
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   517
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   518
77378
f047804f4860 clarified Build_Process.Context: cover all static information;
wenzelm
parents: 77375
diff changeset
   519
    def init_database(db: SQL.Database, build_context: Build_Process.Context): Unit = {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   520
      val tables =
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   521
        List(
77417
wenzelm
parents: 77416
diff changeset
   522
          Base.table,
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   523
          Serial.table,
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   524
          Progress.table,
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   525
          Sessions.table,
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   526
          Pending.table,
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   527
          Running.table,
77476
5f6f567a2661 clarified modules;
wenzelm
parents: 77475
diff changeset
   528
          Results.table,
5f6f567a2661 clarified modules;
wenzelm
parents: 77475
diff changeset
   529
          Host.Data.Node_Info.table)
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   530
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   531
      for (table <- tables) db.create_table(table)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   532
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   533
      val old_pending = Data.read_pending(db)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   534
      if (old_pending.nonEmpty) {
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   535
        error("Cannot init build process, because of unfinished " +
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   536
          commas_quote(old_pending.map(_.name)))
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   537
      }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   538
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   539
      for (table <- tables) db.using_statement(table.delete())(_.execute())
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   540
77417
wenzelm
parents: 77416
diff changeset
   541
      db.using_statement(Base.table.insert()) { stmt =>
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   542
        stmt.string(1) = build_context.uuid
77417
wenzelm
parents: 77416
diff changeset
   543
        stmt.string(2) = Isabelle_System.getenv("ML_PLATFORM")
wenzelm
parents: 77416
diff changeset
   544
        stmt.string(3) = build_context.store.options.make_prefs(Options.init(prefs = ""))
wenzelm
parents: 77416
diff changeset
   545
        stmt.execute()
wenzelm
parents: 77416
diff changeset
   546
      }
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   547
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   548
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   549
    def update_database(
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   550
      db: SQL.Database,
77456
4fec9413f14b identify Build_Process.Context.instance with Sessions.Build_Info (see also ff164add75cd);
wenzelm
parents: 77455
diff changeset
   551
      uuid: String,
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   552
      hostname: String,
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   553
      state: State
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   554
    ): State = {
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   555
      val changed =
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   556
        List(
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   557
          update_sessions(db, state.sessions),
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   558
          update_pending(db, state.pending),
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   559
          update_running(db, state.running),
77476
5f6f567a2661 clarified modules;
wenzelm
parents: 77475
diff changeset
   560
          update_results(db, state.results),
5f6f567a2661 clarified modules;
wenzelm
parents: 77475
diff changeset
   561
          Host.Data.update_numa_index(db, hostname, state.numa_index))
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   562
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   563
      val serial0 = get_serial(db)
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   564
      val serial = if (changed.exists(identity)) serial0 + 1 else serial0
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   565
77416
d88c12f22ab0 clarified scope of "serial" and "numa_index" within database;
wenzelm
parents: 77415
diff changeset
   566
      set_serial(db, serial)
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   567
      state.copy(serial = serial)
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   568
    }
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   569
  }
77396
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
   570
}
77372
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
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   573
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   574
/** main process **/
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   575
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   576
class Build_Process(
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   577
  protected val build_context: Build_Process.Context,
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   578
  protected val build_progress: Progress
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   579
)
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   580
extends AutoCloseable {
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   581
  /* context */
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   582
77338
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   583
  protected val store: Sessions.Store = build_context.store
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   584
  protected val build_options: Options = store.options
77453
e72b1f5fd88d tuned signature;
wenzelm
parents: 77452
diff changeset
   585
  protected val build_deps: Sessions.Deps = build_context.build_deps
77338
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   586
  protected val verbose: Boolean = build_context.verbose
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   587
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   588
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   589
  /* global state: internal var vs. external database */
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   590
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   591
  private var _state: Build_Process.State = init_state(Build_Process.State())
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   592
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   593
  private val _database: Option[SQL.Database] =
77390
ff43a524aa5d clarified system option: guard for testing, until the database layout has stabilized;
wenzelm
parents: 77387
diff changeset
   594
    if (!build_options.bool("build_database_test")) None
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   595
    else if (store.database_server) Some(store.open_database_server())
77383
cb75171d8c9f clarified permissions of build.db, following server.db;
wenzelm
parents: 77381
diff changeset
   596
    else {
cb75171d8c9f clarified permissions of build.db, following server.db;
wenzelm
parents: 77381
diff changeset
   597
      val db = SQLite.open_database(Build_Process.Data.database)
cb75171d8c9f clarified permissions of build.db, following server.db;
wenzelm
parents: 77381
diff changeset
   598
      try { Isabelle_System.chmod("600", Build_Process.Data.database) }
cb75171d8c9f clarified permissions of build.db, following server.db;
wenzelm
parents: 77381
diff changeset
   599
      catch { case exn: Throwable => db.close(); throw exn }
cb75171d8c9f clarified permissions of build.db, following server.db;
wenzelm
parents: 77381
diff changeset
   600
      Some(db)
cb75171d8c9f clarified permissions of build.db, following server.db;
wenzelm
parents: 77381
diff changeset
   601
    }
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   602
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   603
  def close(): Unit = synchronized { _database.foreach(_.close()) }
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   604
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   605
  private def setup_database(): Unit =
77438
0030eabbe6c3 more robust: synchronized access to database;
wenzelm
parents: 77437
diff changeset
   606
    synchronized {
0030eabbe6c3 more robust: synchronized access to database;
wenzelm
parents: 77437
diff changeset
   607
      for (db <- _database) {
0030eabbe6c3 more robust: synchronized access to database;
wenzelm
parents: 77437
diff changeset
   608
        db.transaction { Build_Process.Data.init_database(db, build_context) }
0030eabbe6c3 more robust: synchronized access to database;
wenzelm
parents: 77437
diff changeset
   609
        db.rebuild()
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   610
      }
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   611
    }
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   612
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   613
  protected def synchronized_database[A](body: => A): A =
77438
0030eabbe6c3 more robust: synchronized access to database;
wenzelm
parents: 77437
diff changeset
   614
    synchronized {
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   615
      _database match {
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   616
        case None => body
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   617
        case Some(db) => db.transaction { body }
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   618
      }
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   619
    }
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   620
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   621
  private def sync_database(): Unit =
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   622
    synchronized_database {
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   623
      for (db <- _database) {
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   624
        _state =
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   625
          Build_Process.Data.update_database(
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   626
            db, build_context.uuid, build_context.hostname, _state)
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   627
      }
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   628
    }
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   629
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   630
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   631
  /* progress backed by database */
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   632
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   633
  object progress extends Progress {
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   634
    override def echo(message: Progress.Message): Unit =
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   635
      synchronized_database {
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   636
        val serial1 = _state.serial + 1
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   637
        _state = _state.echo(build_progress, serial1, message).copy(serial = serial1)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   638
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   639
        for (db <- _database) {
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   640
          Build_Process.Data.write_progress(db, serial1, message, build_context.uuid)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   641
          Build_Process.Data.set_serial(db, serial1)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   642
        }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   643
      }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   644
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   645
    override def verbose: Boolean = build_progress.verbose
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   646
    override def stop(): Unit = build_progress.stop()
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   647
    override def stopped: Boolean = build_progress.stopped
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   648
  }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   649
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   650
  val log: Logger =
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   651
    build_options.string("system_log") match {
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   652
      case "" => No_Logger
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   653
      case "-" => Logger.make(progress)
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   654
      case log_file => Logger.make(Some(Path.explode(log_file)))
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   655
    }
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   656
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   657
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   658
  /* policy operations */
77333
0bec014c0f9f tuned signature;
wenzelm
parents: 77332
diff changeset
   659
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   660
  protected def init_state(state: Build_Process.State): Build_Process.State = {
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   661
    val sessions1 =
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   662
      build_context.sessions.foldLeft(state.sessions) { case (map, (name, session)) =>
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   663
        if (state.sessions.isDefinedAt(name)) map
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   664
        else map + (name -> session)
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   665
      }
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   666
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   667
    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
   668
    val new_pending =
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   669
      List.from(
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   670
        for {
77449
wenzelm
parents: 77448
diff changeset
   671
          (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
   672
          if !old_pending(name)
77449
wenzelm
parents: 77448
diff changeset
   673
        } yield Build_Process.Entry(name, session_context.deps))
77496
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   674
    val pending1 = new_pending ::: state.pending
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   675
f0d9f9196b9b more database content;
wenzelm
parents: 77495
diff changeset
   676
    state.copy(sessions = sessions1, pending = pending1)
77415
6b928419f109 clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents: 77414
diff changeset
   677
  }
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   678
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   679
  protected def next_job(state: Build_Process.State): Option[String] =
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   680
    if (state.running.size < (build_context.max_jobs max 1)) {
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   681
      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
   682
        .sortBy(_.name)(build_context.ordering)
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   683
        .headOption.map(_.name)
77296
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   684
    }
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   685
    else None
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   686
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   687
  protected def start_session(state: Build_Process.State, session_name: String): Build_Process.State = {
77468
wenzelm
parents: 77467
diff changeset
   688
    val ancestor_results =
wenzelm
parents: 77467
diff changeset
   689
      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
   690
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   691
    val input_shasum =
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   692
      if (ancestor_results.isEmpty) {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   693
        SHA1.shasum_meta_info(SHA1.digest(Path.explode("$POLYML_EXE")))
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   694
      }
77460
ccca589d7027 tuned signature: support general Build_Job instances;
wenzelm
parents: 77459
diff changeset
   695
      else SHA1.flat_shasum(ancestor_results.map(_.output_shasum))
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   696
77463
4e8bec105ce5 tuned signature;
wenzelm
parents: 77462
diff changeset
   697
    val store_heap = build_context.store_heap(session_name)
77469
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   698
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   699
    val (current, output_shasum) =
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   700
      store.check_output(session_name,
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   701
        sources_shasum = build_context.sources_shasum(session_name),
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   702
        input_shasum = input_shasum,
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   703
        fresh_build = build_context.fresh_build,
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   704
        store_heap = store_heap)
5af7e8ffcab7 clarified modules;
wenzelm
parents: 77468
diff changeset
   705
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   706
    val all_current = current && ancestor_results.forall(_.current)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   707
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   708
    if (all_current) {
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   709
      state
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   710
        .remove_pending(session_name)
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   711
        .make_result(session_name, Process_Result.ok, output_shasum, current = true)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   712
    }
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   713
    else if (build_context.no_build) {
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   714
      progress.echo_if(verbose, "Skipping " + session_name + " ...")
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   715
      state.
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   716
        remove_pending(session_name).
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   717
        make_result(session_name, Process_Result.error, output_shasum)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   718
    }
77452
wenzelm
parents: 77449
diff changeset
   719
    else if (!ancestor_results.forall(_.ok) || progress.stopped) {
wenzelm
parents: 77449
diff changeset
   720
      progress.echo(session_name + " CANCELLED")
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   721
      state
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   722
        .remove_pending(session_name)
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   723
        .make_result(session_name, Process_Result.undefined, output_shasum)
77452
wenzelm
parents: 77449
diff changeset
   724
    }
wenzelm
parents: 77449
diff changeset
   725
    else {
77463
4e8bec105ce5 tuned signature;
wenzelm
parents: 77462
diff changeset
   726
      progress.echo((if (store_heap) "Building " else "Running ") + session_name + " ...")
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   727
77472
a073ac3f3b56 clarified modules;
wenzelm
parents: 77470
diff changeset
   728
      store.init_output(session_name)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   729
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   730
      val (numa_node, state1) = state.numa_next(build_context.numa_nodes)
77475
3bc611c80346 clarified modules;
wenzelm
parents: 77474
diff changeset
   731
      val node_info = Host.Node_Info(build_context.hostname, numa_node)
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   732
      val job =
77505
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   733
        Build_Job.start_session(build_context, progress, log,
7ee426daafa3 support progress backed by database;
wenzelm
parents: 77496
diff changeset
   734
          build_deps.background(session_name), input_shasum, node_info)
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   735
      state1.add_running(session_name, job)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   736
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   737
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   738
77436
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   739
c10fa027caa0 tuned comments and outline;
wenzelm
parents: 77417
diff changeset
   740
  /* run */
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   741
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   742
  def run(): Map[String, Process_Result] = {
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   743
    def finished(): Boolean = synchronized_database { _state.finished }
77400
f3e5b3fe230e clarified signature: more explicit "synchronized" regions;
wenzelm
parents: 77398
diff changeset
   744
77437
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   745
    def sleep(): Unit =
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   746
      Isabelle_Thread.interrupt_handler(_ => progress.stop()) {
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   747
        build_options.seconds("editor_input_delay").sleep()
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   748
      }
dcbf96acae27 clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents: 77436
diff changeset
   749
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   750
    def start(): Boolean = synchronized_database {
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   751
      next_job(_state) match {
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   752
        case Some(name) =>
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   753
          if (Build_Job.is_session_name(name)) {
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   754
            _state = start_session(_state, name)
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   755
            true
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   756
          }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   757
          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
   758
        case None => false
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   759
      }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   760
    }
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   761
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   762
    if (finished()) {
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   763
      progress.echo_warning("Nothing to build")
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   764
      Map.empty[String, Process_Result]
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   765
    }
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   766
    else {
77372
44fe9fe96130 support for build database: still inactive;
wenzelm
parents: 77344
diff changeset
   767
      setup_database()
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   768
      while (!finished()) {
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   769
        if (progress.stopped) synchronized_database { _state.stop_running() }
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   770
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   771
        for (job <- synchronized_database { _state.finished_running() }) {
77398
19e9cafaafc5 clarified signature: works for general Build_Job;
wenzelm
parents: 77396
diff changeset
   772
          val job_name = job.job_name
77486
032c76e04475 clarified execution context: main work happens within Future.thread;
wenzelm
parents: 77477
diff changeset
   773
          val (process_result, output_shasum) = job.join
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   774
          synchronized_database {
77396
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
   775
            _state = _state.
77398
19e9cafaafc5 clarified signature: works for general Build_Job;
wenzelm
parents: 77396
diff changeset
   776
              remove_pending(job_name).
19e9cafaafc5 clarified signature: works for general Build_Job;
wenzelm
parents: 77396
diff changeset
   777
              remove_running(job_name).
77461
a553e419e9dc tuned signature;
wenzelm
parents: 77460
diff changeset
   778
              make_result(job_name, process_result, output_shasum, node_info = job.node_info)
77396
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
   779
          }
f184fbac99bc clarified modules;
wenzelm
parents: 77395
diff changeset
   780
        }
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   781
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   782
        if (!start()) {
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   783
          sync_database()
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   784
          sleep()
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   785
        }
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   786
      }
77467
e27bc7957297 more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents: 77466
diff changeset
   787
77466
94dcf2c3895a more thorough synchronized_database for internal *and* external state;
wenzelm
parents: 77465
diff changeset
   788
      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
   789
        for ((name, result) <- _state.results) yield name -> result.process_result
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   790
      }
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   791
    }
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   792
  }
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
   793
}