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