author | wenzelm |
Mon, 13 Mar 2023 21:43:55 +0100 | |
changeset 77638 | a4266d54ec35 |
parent 77637 | afb1a19307c4 |
child 77639 | d5344cc1fae7 |
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( |
|
21 |
store: Sessions.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( |
|
77257 | 92 |
val store: Sessions.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 = |
108 |
"Build_Process.Context(build_uuid = " + quote(build_uuid) + ")" |
|
109 |
||
77470 | 110 |
def build_options: Options = store.options |
111 |
||
77453 | 112 |
def sessions_structure: Sessions.Structure = build_deps.sessions_structure |
77257 | 113 |
|
77462 | 114 |
def sources_shasum(name: String): SHA1.Shasum = sessions(name).sources_shasum |
77458 | 115 |
|
77462 | 116 |
def old_command_timings(name: String): List[Properties.T] = |
117 |
sessions.get(name) match { |
|
77444
0c704aba71e3
clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents:
77443
diff
changeset
|
118 |
case Some(session_context) => |
0c704aba71e3
clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents:
77443
diff
changeset
|
119 |
Properties.uncompress(session_context.old_command_timings_blob, cache = store.cache) |
0c704aba71e3
clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents:
77443
diff
changeset
|
120 |
case None => Nil |
0c704aba71e3
clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents:
77443
diff
changeset
|
121 |
} |
77439
d6bf9ec39d12
avoid premature Properties.uncompress: allow blob to be stored in another database;
wenzelm
parents:
77438
diff
changeset
|
122 |
|
77536
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
123 |
def prepare_database(): Unit = { |
77629 | 124 |
using_option(store.open_build_database()) { db => |
77539
2b996a0df1ce
proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents:
77538
diff
changeset
|
125 |
db.transaction { |
77596 | 126 |
Data.all_tables.create_lock(db) |
77539
2b996a0df1ce
proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents:
77538
diff
changeset
|
127 |
Data.clean_build(db) |
2b996a0df1ce
proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents:
77538
diff
changeset
|
128 |
} |
77536
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
129 |
db.rebuild() |
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
130 |
} |
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
131 |
} |
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
132 |
|
77463 | 133 |
def store_heap(name: String): Boolean = |
77462 | 134 |
build_heap || Sessions.is_pure(name) || |
135 |
sessions.valuesIterator.exists(_.ancestors.contains(name)) |
|
77578
149d48a4801b
support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents:
77561
diff
changeset
|
136 |
|
77594 | 137 |
def worker_active: Boolean = max_jobs > 0 |
77246 | 138 |
} |
77257 | 139 |
|
140 |
||
77436 | 141 |
|
142 |
/** dynamic state **/ |
|
77257 | 143 |
|
77505 | 144 |
type Progress_Messages = SortedMap[Long, Progress.Message] |
145 |
||
77631 | 146 |
case class Worker( |
147 |
worker_uuid: String, |
|
148 |
build_uuid: String, |
|
149 |
hostname: String, |
|
150 |
java_pid: Long, |
|
151 |
java_start: Date, |
|
152 |
start: Date, |
|
153 |
stamp: Date, |
|
154 |
stop: Option[Date], |
|
155 |
serial: Long |
|
156 |
) |
|
157 |
||
158 |
case class Task( |
|
159 |
name: String, |
|
160 |
deps: List[String], |
|
161 |
info: JSON.Object.T = JSON.Object.empty |
|
162 |
) { |
|
77313
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
163 |
def is_ready: Boolean = deps.isEmpty |
77585 | 164 |
def resolve(dep: String): Task = |
77313
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
165 |
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
|
166 |
} |
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
167 |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
168 |
case class Job( |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
169 |
name: String, |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
170 |
worker_uuid: String, |
77634 | 171 |
build_uuid: String, |
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
172 |
node_info: Host.Node_Info, |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
173 |
build: Option[Build_Job] |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
174 |
) { |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
175 |
def no_build: Job = copy(build = None) |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
176 |
} |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
177 |
|
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
178 |
case class Result( |
77461 | 179 |
process_result: Process_Result, |
77460
ccca589d7027
tuned signature: support general Build_Job instances;
wenzelm
parents:
77459
diff
changeset
|
180 |
output_shasum: SHA1.Shasum, |
77475 | 181 |
node_info: Host.Node_Info, |
77461 | 182 |
current: Boolean |
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
183 |
) { |
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
184 |
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
|
185 |
} |
77312 | 186 |
|
77496 | 187 |
object State { |
188 |
type Sessions = Map[String, Build_Job.Session_Context] |
|
77583 | 189 |
type Workers = List[Worker] |
77585 | 190 |
type Pending = List[Task] |
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
191 |
type Running = Map[String, Job] |
77496 | 192 |
type Results = Map[String, Result] |
77513 | 193 |
|
194 |
def inc_serial(serial: Long): Long = { |
|
195 |
require(serial < java.lang.Long.MAX_VALUE, "serial overflow") |
|
196 |
serial + 1 |
|
197 |
} |
|
77496 | 198 |
} |
199 |
||
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
200 |
sealed case class State( |
77372 | 201 |
serial: Long = 0, |
77505 | 202 |
progress_seen: Long = 0, |
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77522
diff
changeset
|
203 |
numa_next: Int = 0, |
77496 | 204 |
sessions: State.Sessions = Map.empty, // static build targets |
77583 | 205 |
workers: State.Workers = Nil, // available worker processes |
77496 | 206 |
pending: State.Pending = Nil, // dynamic build "queue" |
207 |
running: State.Running = Map.empty, // presently running jobs |
|
208 |
results: State.Results = Map.empty // finished results |
|
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
209 |
) { |
77513 | 210 |
require(serial >= 0, "serial underflow") |
211 |
def inc_serial: State = copy(serial = State.inc_serial(serial)) |
|
212 |
def set_serial(i: Long): State = { |
|
213 |
require(serial <= i, "non-monotonic change of serial") |
|
214 |
copy(serial = i) |
|
215 |
} |
|
216 |
||
77522
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
217 |
def progress_serial(message_serial: Long = serial): State = |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
218 |
if (message_serial > progress_seen) copy(progress_seen = message_serial) |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
219 |
else error("Bad serial " + message_serial + " for progress output (already seen)") |
77505 | 220 |
|
77583 | 221 |
def set_workers(new_workers: State.Workers): State = copy(workers = new_workers) |
222 |
||
77632 | 223 |
def next_numa_node(numa_nodes: List[Int]): (Option[Int], State) = |
77343 | 224 |
if (numa_nodes.isEmpty) (None, this) |
225 |
else { |
|
226 |
val available = numa_nodes.zipWithIndex |
|
77372 | 227 |
val used = |
228 |
Set.from(for (job <- running.valuesIterator; i <- job.node_info.numa_node) yield i) |
|
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77522
diff
changeset
|
229 |
|
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77522
diff
changeset
|
230 |
val numa_index = available.collectFirst({ case (n, i) if n == numa_next => i }).getOrElse(0) |
77343 | 231 |
val candidates = available.drop(numa_index) ::: available.take(numa_index) |
232 |
val (n, i) = |
|
233 |
candidates.find({ case (n, i) => i == numa_index && !used(n) }) orElse |
|
234 |
candidates.find({ case (n, _) => !used(n) }) getOrElse candidates.head |
|
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77522
diff
changeset
|
235 |
|
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77522
diff
changeset
|
236 |
(Some(n), copy(numa_next = numa_nodes((i + 1) % numa_nodes.length))) |
77343 | 237 |
} |
77337 | 238 |
|
77335 | 239 |
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
|
240 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
241 |
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
|
242 |
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
|
243 |
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
|
244 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
245 |
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
|
246 |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
247 |
def stop_running(): Unit = |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
248 |
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
|
249 |
|
77398
19e9cafaafc5
clarified signature: works for general Build_Job;
wenzelm
parents:
77396
diff
changeset
|
250 |
def finished_running(): List[Build_Job] = |
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
251 |
List.from( |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
252 |
for (job <- running.valuesIterator; build <- job.build if build.is_finished) |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
253 |
yield build) |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
254 |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
255 |
def add_running(job: Job): State = |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
256 |
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
|
257 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
258 |
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
|
259 |
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
|
260 |
|
77336 | 261 |
def make_result( |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
262 |
name: String, |
77461 | 263 |
process_result: Process_Result, |
77460
ccca589d7027
tuned signature: support general Build_Job instances;
wenzelm
parents:
77459
diff
changeset
|
264 |
output_shasum: SHA1.Shasum, |
77475 | 265 |
node_info: Host.Node_Info = Host.Node_Info.none, |
77461 | 266 |
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
|
267 |
): State = { |
77461 | 268 |
val entry = name -> Build_Process.Result(process_result, output_shasum, node_info, current) |
269 |
copy(results = results + entry) |
|
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
270 |
} |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
271 |
} |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
272 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
273 |
|
77436 | 274 |
|
275 |
/** SQL data model **/ |
|
77372 | 276 |
|
277 |
object Data { |
|
278 |
def make_table(name: String, columns: List[SQL.Column], body: String = ""): SQL.Table = |
|
279 |
SQL.Table("isabelle_build" + if_proper(name, "_" + name), columns, body = body) |
|
280 |
||
281 |
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
|
282 |
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
|
283 |
val worker_uuid = SQL.Column.string("worker_uuid") |
77372 | 284 |
val name = SQL.Column.string("name") |
285 |
||
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
|
286 |
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
|
287 |
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
|
288 |
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
|
289 |
name: String = "", |
40ccee0fe19a
separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents:
77527
diff
changeset
|
290 |
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
|
291 |
): SQL.Source = |
77372 | 292 |
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
|
293 |
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
|
294 |
if_proper(worker_uuid, Generic.worker_uuid.equal(worker_uuid)), |
40ccee0fe19a
separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents:
77527
diff
changeset
|
295 |
if_proper(name, Generic.name.equal(name)), |
77375 | 296 |
if_proper(names, Generic.name.member(names))) |
77372 | 297 |
} |
298 |
||
77536
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
299 |
|
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
300 |
/* base table */ |
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
301 |
|
77417 | 302 |
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
|
303 |
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
|
304 |
val ml_platform = SQL.Column.string("ml_platform") |
77372 | 305 |
val options = SQL.Column.string("options") |
77536
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
306 |
val start = SQL.Column.date("start") |
77545 | 307 |
val stop = SQL.Column.date("stop") |
77635 | 308 |
val progress_stopped = SQL.Column.bool("progress_stopped") |
77372 | 309 |
|
77635 | 310 |
val table = |
311 |
make_table("", List(build_uuid, ml_platform, options, start, stop, progress_stopped)) |
|
77536
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
312 |
} |
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
313 |
|
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
314 |
def start_build( |
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
315 |
db: SQL.Database, |
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
316 |
build_uuid: String, |
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
317 |
ml_platform: String, |
77635 | 318 |
options: String, |
319 |
progress_stopped: Boolean |
|
77536
7c7f1473e51a
clarified database content and prepare/init stages;
wenzelm
parents:
77534
diff
changeset
|
320 |
): Unit = { |
77541 | 321 |
db.execute_statement(Base.table.insert(), body = |
322 |
{ stmt => |
|
323 |
stmt.string(1) = build_uuid |
|
324 |
stmt.string(2) = ml_platform |
|
325 |
stmt.string(3) = options |
|
326 |
stmt.date(4) = db.now() |
|
327 |
stmt.date(5) = None |
|
77635 | 328 |
stmt.bool(6) = progress_stopped |
77541 | 329 |
}) |
77372 | 330 |
} |
331 |
||
77545 | 332 |
def stop_build(db: SQL.Database, build_uuid: String): Unit = |
77541 | 333 |
db.execute_statement( |
77636 | 334 |
Base.table.update(List(Base.stop), sql = Base.build_uuid.where_equal(build_uuid)), |
77541 | 335 |
body = { stmt => stmt.date(1) = db.now() }) |
77538 | 336 |
|
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
|
337 |
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
|
338 |
val old = |
77552 | 339 |
db.execute_query_statement( |
340 |
Base.table.select(List(Base.build_uuid), sql = SQL.where(Base.stop.defined)), |
|
341 |
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
|
342 |
|
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
|
343 |
if (old.nonEmpty) { |
2b996a0df1ce
proper clean_build of old data at start of new process --- allow to inspect remains of the last process;
wenzelm
parents:
77538
diff
changeset
|
344 |
for (table <- List(Base.table, Sessions.table, Progress.table, Workers.table)) { |
77540 | 345 |
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
|
346 |
} |
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
|
347 |
} |
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
|
348 |
} |
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
|
349 |
|
77416
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
350 |
|
77531 | 351 |
/* sessions */ |
77505 | 352 |
|
77496 | 353 |
object Sessions { |
354 |
val name = Generic.name.make_primary_key |
|
355 |
val deps = SQL.Column.string("deps") |
|
356 |
val ancestors = SQL.Column.string("ancestors") |
|
77610
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
357 |
val options = SQL.Column.string("options") |
77496 | 358 |
val sources = SQL.Column.string("sources") |
359 |
val timeout = SQL.Column.long("timeout") |
|
360 |
val old_time = SQL.Column.long("old_time") |
|
361 |
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
|
362 |
val build_uuid = Generic.build_uuid |
77496 | 363 |
|
364 |
val table = make_table("sessions", |
|
77610
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
365 |
List(name, deps, ancestors, options, sources, timeout, |
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
366 |
old_time, old_command_timings, build_uuid)) |
77496 | 367 |
} |
368 |
||
369 |
def read_sessions_domain(db: SQL.Database): Set[String] = |
|
77552 | 370 |
db.execute_query_statement( |
371 |
Sessions.table.select(List(Sessions.name)), |
|
372 |
Set.from[String], res => res.string(Sessions.name)) |
|
77496 | 373 |
|
374 |
def read_sessions(db: SQL.Database, names: Iterable[String] = Nil): State.Sessions = |
|
77552 | 375 |
db.execute_query_statement( |
376 |
Sessions.table.select(sql = if_proper(names, Sessions.name.where_member(names))), |
|
377 |
Map.from[String, Build_Job.Session_Context], |
|
378 |
{ res => |
|
379 |
val name = res.string(Sessions.name) |
|
380 |
val deps = split_lines(res.string(Sessions.deps)) |
|
381 |
val ancestors = split_lines(res.string(Sessions.ancestors)) |
|
77610
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
382 |
val options = res.string(Sessions.options) |
77552 | 383 |
val sources_shasum = SHA1.fake_shasum(res.string(Sessions.sources)) |
384 |
val timeout = Time.ms(res.long(Sessions.timeout)) |
|
385 |
val old_time = Time.ms(res.long(Sessions.old_time)) |
|
386 |
val old_command_timings_blob = res.bytes(Sessions.old_command_timings) |
|
387 |
val build_uuid = res.string(Sessions.build_uuid) |
|
77610
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
388 |
name -> Build_Job.Session_Context(name, deps, ancestors, options, sources_shasum, |
77552 | 389 |
timeout, old_time, old_command_timings_blob, build_uuid) |
77496 | 390 |
} |
77552 | 391 |
) |
77496 | 392 |
|
393 |
def update_sessions(db:SQL.Database, sessions: State.Sessions): Boolean = { |
|
394 |
val old_sessions = read_sessions_domain(db) |
|
395 |
val insert = sessions.iterator.filterNot(p => old_sessions.contains(p._1)).toList |
|
396 |
||
397 |
for ((name, session) <- insert) { |
|
77541 | 398 |
db.execute_statement(Sessions.table.insert(), body = |
399 |
{ stmt => |
|
400 |
stmt.string(1) = name |
|
401 |
stmt.string(2) = cat_lines(session.deps) |
|
402 |
stmt.string(3) = cat_lines(session.ancestors) |
|
77610
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
403 |
stmt.string(4) = session.session_prefs |
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
404 |
stmt.string(5) = session.sources_shasum.toString |
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
405 |
stmt.long(6) = session.timeout.ms |
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
406 |
stmt.long(7) = session.old_time.ms |
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
407 |
stmt.bytes(8) = session.old_command_timings_blob |
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77609
diff
changeset
|
408 |
stmt.string(9) = session.build_uuid |
77541 | 409 |
}) |
77496 | 410 |
} |
411 |
||
412 |
insert.nonEmpty |
|
413 |
} |
|
414 |
||
77531 | 415 |
|
416 |
/* progress */ |
|
417 |
||
418 |
object Progress { |
|
419 |
val serial = SQL.Column.long("serial").make_primary_key |
|
420 |
val kind = SQL.Column.int("kind") |
|
421 |
val text = SQL.Column.string("text") |
|
422 |
val verbose = SQL.Column.bool("verbose") |
|
423 |
val build_uuid = Generic.build_uuid |
|
424 |
||
425 |
val table = make_table("progress", List(serial, kind, text, verbose, build_uuid)) |
|
426 |
} |
|
427 |
||
428 |
def read_progress(db: SQL.Database, seen: Long = 0, build_uuid: String = ""): Progress_Messages = |
|
77552 | 429 |
db.execute_query_statement( |
77531 | 430 |
Progress.table.select( |
431 |
sql = |
|
432 |
SQL.where( |
|
433 |
SQL.and( |
|
434 |
if (seen <= 0) "" else Progress.serial.ident + " > " + seen, |
|
77552 | 435 |
Generic.sql(build_uuid = build_uuid)))), |
436 |
SortedMap.from[Long, isabelle.Progress.Message], |
|
437 |
{ res => |
|
438 |
val serial = res.long(Progress.serial) |
|
439 |
val kind = isabelle.Progress.Kind(res.int(Progress.kind)) |
|
440 |
val text = res.string(Progress.text) |
|
441 |
val verbose = res.bool(Progress.verbose) |
|
442 |
serial -> isabelle.Progress.Message(kind, text, verbose = verbose) |
|
77531 | 443 |
} |
77552 | 444 |
) |
77531 | 445 |
|
446 |
def write_progress( |
|
447 |
db: SQL.Database, |
|
448 |
message_serial: Long, |
|
449 |
message: isabelle.Progress.Message, |
|
450 |
build_uuid: String |
|
451 |
): Unit = { |
|
77541 | 452 |
db.execute_statement(Progress.table.insert(), body = |
453 |
{ stmt => |
|
454 |
stmt.long(1) = message_serial |
|
455 |
stmt.int(2) = message.kind.id |
|
456 |
stmt.string(3) = message.text |
|
457 |
stmt.bool(4) = message.verbose |
|
458 |
stmt.string(5) = build_uuid |
|
459 |
}) |
|
77531 | 460 |
} |
461 |
||
77637 | 462 |
def sync_progress( |
463 |
db: SQL.Database, |
|
464 |
seen: Long, |
|
465 |
build_uuid: String, |
|
466 |
build_progress: Progress |
|
467 |
): (Progress_Messages, Boolean) = { |
|
468 |
require(build_uuid.nonEmpty) |
|
469 |
||
470 |
val messages = read_progress(db, seen = seen, build_uuid = build_uuid) |
|
471 |
||
77635 | 472 |
val stopped_db = |
473 |
db.execute_query_statementO[Boolean]( |
|
474 |
Base.table.select(List(Base.progress_stopped), |
|
475 |
sql = SQL.where(Base.build_uuid.equal(build_uuid))), |
|
476 |
res => res.bool(Base.progress_stopped) |
|
477 |
).getOrElse(false) |
|
478 |
||
479 |
def stop_db(): Unit = |
|
480 |
db.execute_statement( |
|
481 |
Base.table.update( |
|
77636 | 482 |
List(Base.progress_stopped), sql = Base.build_uuid.where_equal(build_uuid)), |
77635 | 483 |
body = { stmt => stmt.bool(1) = true }) |
484 |
||
485 |
val stopped = build_progress.stopped |
|
486 |
||
487 |
if (stopped_db && !stopped) build_progress.stop() |
|
488 |
if (stopped && !stopped_db) stop_db() |
|
77637 | 489 |
|
490 |
(messages, messages.isEmpty && stopped_db == stopped) |
|
77635 | 491 |
} |
492 |
||
77531 | 493 |
|
494 |
/* workers */ |
|
495 |
||
496 |
object Workers { |
|
497 |
val worker_uuid = Generic.worker_uuid.make_primary_key |
|
498 |
val build_uuid = Generic.build_uuid |
|
77584 | 499 |
val hostname = SQL.Column.string("hostname") |
500 |
val java_pid = SQL.Column.long("java_pid") |
|
501 |
val java_start = SQL.Column.date("java_start") |
|
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
502 |
val start = SQL.Column.date("start") |
77531 | 503 |
val stamp = SQL.Column.date("stamp") |
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
504 |
val stop = SQL.Column.date("stop") |
77531 | 505 |
val serial = SQL.Column.long("serial") |
506 |
||
77584 | 507 |
val table = make_table("workers", |
508 |
List(worker_uuid, build_uuid, hostname, java_pid, java_start, start, stamp, stop, serial)) |
|
77531 | 509 |
|
510 |
val serial_max = serial.copy(expr = "MAX(" + serial.ident + ")") |
|
511 |
} |
|
512 |
||
77586 | 513 |
def read_workers( |
514 |
db: SQL.Database, |
|
515 |
build_uuid: String = "", |
|
516 |
worker_uuid: String = "" |
|
517 |
): State.Workers = { |
|
518 |
db.execute_query_statement( |
|
519 |
Workers.table.select(sql = |
|
520 |
SQL.where(Generic.sql(build_uuid = build_uuid, worker_uuid = worker_uuid))), |
|
521 |
List.from[Worker], |
|
522 |
{ res => |
|
523 |
Worker( |
|
524 |
worker_uuid = res.string(Workers.worker_uuid), |
|
525 |
build_uuid = res.string(Workers.build_uuid), |
|
526 |
hostname = res.string(Workers.hostname), |
|
527 |
java_pid = res.long(Workers.java_pid), |
|
77588
066d5df144f0
assume total operation: ProcessHandle.current().info.startInstant appears to work on all platforms;
wenzelm
parents:
77587
diff
changeset
|
528 |
java_start = res.date(Workers.java_start), |
77586 | 529 |
start = res.date(Workers.start), |
530 |
stamp = res.date(Workers.stamp), |
|
531 |
stop = res.get_date(Workers.stop), |
|
532 |
serial = res.long(Workers.serial)) |
|
533 |
}) |
|
534 |
} |
|
535 |
||
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
536 |
def serial_max(db: SQL.Database): Long = |
77552 | 537 |
db.execute_query_statementO[Long]( |
538 |
Workers.table.select(List(Workers.serial_max)), |
|
539 |
res => res.long(Workers.serial) |
|
540 |
).getOrElse(0L) |
|
77531 | 541 |
|
77584 | 542 |
def start_worker( |
543 |
db: SQL.Database, |
|
544 |
worker_uuid: String, |
|
545 |
build_uuid: String, |
|
546 |
hostname: String, |
|
547 |
java_pid: Long, |
|
77588
066d5df144f0
assume total operation: ProcessHandle.current().info.startInstant appears to work on all platforms;
wenzelm
parents:
77587
diff
changeset
|
548 |
java_start: Date |
77584 | 549 |
): Long = { |
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
550 |
def err(msg: String): Nothing = |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
551 |
error("Cannot start worker " + worker_uuid + if_proper(msg, "\n" + msg)) |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
552 |
|
77552 | 553 |
val build_stop = |
554 |
db.execute_query_statementO( |
|
77636 | 555 |
Base.table.select(List(Base.stop), sql = Base.build_uuid.where_equal(build_uuid)), |
77552 | 556 |
res => res.get_date(Base.stop)) |
557 |
||
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
558 |
build_stop match { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
559 |
case Some(None) => |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
560 |
case Some(Some(_)) => err("for already stopped build process " + build_uuid) |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
561 |
case None => err("for unknown build process " + build_uuid) |
77531 | 562 |
} |
563 |
||
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
564 |
val serial = serial_max(db) |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
565 |
db.execute_statement(Workers.table.insert(), body = |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
566 |
{ stmt => |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
567 |
val now = db.now() |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
568 |
stmt.string(1) = worker_uuid |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
569 |
stmt.string(2) = build_uuid |
77584 | 570 |
stmt.string(3) = hostname |
571 |
stmt.long(4) = java_pid |
|
572 |
stmt.date(5) = java_start |
|
573 |
stmt.date(6) = now |
|
574 |
stmt.date(7) = now |
|
575 |
stmt.date(8) = None |
|
576 |
stmt.long(9) = serial |
|
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
577 |
}) |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
578 |
serial |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
579 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
580 |
|
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
581 |
def stamp_worker( |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
582 |
db: SQL.Database, |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
583 |
worker_uuid: String, |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
584 |
serial: Long, |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
585 |
stop: Boolean = false |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
586 |
): Unit = { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
587 |
val sql = |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
588 |
Workers.table.update(List(Workers.stamp, Workers.stop, Workers.serial), |
77636 | 589 |
sql = Workers.worker_uuid.where_equal(worker_uuid)) |
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
590 |
db.execute_statement(sql, body = |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
591 |
{ stmt => |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
592 |
val now = db.now() |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
593 |
stmt.date(1) = now |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
594 |
stmt.date(2) = if (stop) Some(now) else None |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
595 |
stmt.long(3) = serial |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
596 |
}) |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
597 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
598 |
|
77531 | 599 |
|
600 |
/* pending jobs */ |
|
601 |
||
602 |
object Pending { |
|
603 |
val name = Generic.name.make_primary_key |
|
604 |
val deps = SQL.Column.string("deps") |
|
605 |
val info = SQL.Column.string("info") |
|
606 |
||
607 |
val table = make_table("pending", List(name, deps, info)) |
|
608 |
} |
|
609 |
||
77585 | 610 |
def read_pending(db: SQL.Database): List[Task] = |
77552 | 611 |
db.execute_query_statement( |
612 |
Pending.table.select(sql = SQL.order_by(List(Pending.name))), |
|
77585 | 613 |
List.from[Task], |
77552 | 614 |
{ res => |
615 |
val name = res.string(Pending.name) |
|
616 |
val deps = res.string(Pending.deps) |
|
617 |
val info = res.string(Pending.info) |
|
77585 | 618 |
Task(name, split_lines(deps), info = JSON.Object.parse(info)) |
77552 | 619 |
}) |
77372 | 620 |
|
77496 | 621 |
def update_pending(db: SQL.Database, pending: State.Pending): Boolean = { |
77372 | 622 |
val old_pending = read_pending(db) |
623 |
val (delete, insert) = Library.symmetric_difference(old_pending, pending) |
|
624 |
||
625 |
if (delete.nonEmpty) { |
|
77540 | 626 |
db.execute_statement( |
627 |
Pending.table.delete(sql = SQL.where(Generic.sql(names = delete.map(_.name))))) |
|
77372 | 628 |
} |
629 |
||
630 |
for (entry <- insert) { |
|
77541 | 631 |
db.execute_statement(Pending.table.insert(), body = |
632 |
{ stmt => |
|
633 |
stmt.string(1) = entry.name |
|
634 |
stmt.string(2) = cat_lines(entry.deps) |
|
635 |
stmt.string(3) = JSON.Format(entry.info) |
|
636 |
}) |
|
77372 | 637 |
} |
638 |
||
639 |
delete.nonEmpty || insert.nonEmpty |
|
640 |
} |
|
641 |
||
77531 | 642 |
|
643 |
/* running jobs */ |
|
644 |
||
645 |
object Running { |
|
646 |
val name = Generic.name.make_primary_key |
|
77587 | 647 |
val worker_uuid = Generic.worker_uuid |
77634 | 648 |
val build_uuid = Generic.build_uuid |
77531 | 649 |
val hostname = SQL.Column.string("hostname") |
650 |
val numa_node = SQL.Column.int("numa_node") |
|
651 |
||
77634 | 652 |
val table = make_table("running", List(name, worker_uuid, build_uuid, hostname, numa_node)) |
77531 | 653 |
} |
654 |
||
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
655 |
def read_running(db: SQL.Database): List[Job] = |
77552 | 656 |
db.execute_query_statement( |
657 |
Running.table.select(sql = SQL.order_by(List(Running.name))), |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
658 |
List.from[Job], |
77552 | 659 |
{ res => |
660 |
val name = res.string(Running.name) |
|
77587 | 661 |
val worker_uuid = res.string(Running.worker_uuid) |
77634 | 662 |
val build_uuid = res.string(Running.build_uuid) |
77552 | 663 |
val hostname = res.string(Running.hostname) |
664 |
val numa_node = res.get_int(Running.numa_node) |
|
77634 | 665 |
Job(name, worker_uuid, build_uuid, Host.Node_Info(hostname, numa_node), None) |
77552 | 666 |
} |
667 |
) |
|
77372 | 668 |
|
77496 | 669 |
def update_running(db: SQL.Database, running: State.Running): Boolean = { |
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
670 |
val running0 = read_running(db) |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
671 |
val running1 = running.valuesIterator.map(_.no_build).toList |
77372 | 672 |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
673 |
val (delete, insert) = Library.symmetric_difference(running0, running1) |
77372 | 674 |
|
675 |
if (delete.nonEmpty) { |
|
77540 | 676 |
db.execute_statement( |
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
677 |
Running.table.delete(sql = SQL.where(Generic.sql(names = delete.map(_.name))))) |
77372 | 678 |
} |
679 |
||
680 |
for (job <- insert) { |
|
77541 | 681 |
db.execute_statement(Running.table.insert(), body = |
682 |
{ stmt => |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
683 |
stmt.string(1) = job.name |
77587 | 684 |
stmt.string(2) = job.worker_uuid |
77634 | 685 |
stmt.string(3) = job.build_uuid |
686 |
stmt.string(4) = job.node_info.hostname |
|
687 |
stmt.int(5) = job.node_info.numa_node |
|
77541 | 688 |
}) |
77372 | 689 |
} |
690 |
||
691 |
delete.nonEmpty || insert.nonEmpty |
|
692 |
} |
|
693 |
||
77531 | 694 |
|
695 |
/* job results */ |
|
696 |
||
697 |
object Results { |
|
698 |
val name = Generic.name.make_primary_key |
|
699 |
val hostname = SQL.Column.string("hostname") |
|
700 |
val numa_node = SQL.Column.string("numa_node") |
|
701 |
val rc = SQL.Column.int("rc") |
|
702 |
val out = SQL.Column.string("out") |
|
703 |
val err = SQL.Column.string("err") |
|
704 |
val timing_elapsed = SQL.Column.long("timing_elapsed") |
|
705 |
val timing_cpu = SQL.Column.long("timing_cpu") |
|
706 |
val timing_gc = SQL.Column.long("timing_gc") |
|
707 |
||
708 |
val table = |
|
709 |
make_table("results", |
|
710 |
List(name, hostname, numa_node, rc, out, err, timing_elapsed, timing_cpu, timing_gc)) |
|
711 |
} |
|
712 |
||
77496 | 713 |
def read_results_domain(db: SQL.Database): Set[String] = |
77552 | 714 |
db.execute_query_statement( |
715 |
Results.table.select(List(Results.name)), |
|
716 |
Set.from[String], res => res.string(Results.name)) |
|
77496 | 717 |
|
77372 | 718 |
def read_results(db: SQL.Database, names: List[String] = Nil): Map[String, Build_Job.Result] = |
77552 | 719 |
db.execute_query_statement( |
720 |
Results.table.select(sql = if_proper(names, Results.name.where_member(names))), |
|
721 |
Map.from[String, Build_Job.Result], |
|
722 |
{ res => |
|
723 |
val name = res.string(Results.name) |
|
724 |
val hostname = res.string(Results.hostname) |
|
725 |
val numa_node = res.get_int(Results.numa_node) |
|
726 |
val rc = res.int(Results.rc) |
|
727 |
val out = res.string(Results.out) |
|
728 |
val err = res.string(Results.err) |
|
729 |
val timing = |
|
730 |
res.timing( |
|
731 |
Results.timing_elapsed, |
|
732 |
Results.timing_cpu, |
|
733 |
Results.timing_gc) |
|
734 |
val node_info = Host.Node_Info(hostname, numa_node) |
|
735 |
val process_result = |
|
736 |
Process_Result(rc, |
|
737 |
out_lines = split_lines(out), |
|
738 |
err_lines = split_lines(err), |
|
739 |
timing = timing) |
|
740 |
name -> Build_Job.Result(node_info, process_result) |
|
77496 | 741 |
} |
77552 | 742 |
) |
77372 | 743 |
|
77496 | 744 |
def update_results(db: SQL.Database, results: State.Results): Boolean = { |
745 |
val old_results = read_results_domain(db) |
|
77385 | 746 |
val insert = results.iterator.filterNot(p => old_results.contains(p._1)).toList |
77372 | 747 |
|
748 |
for ((name, result) <- insert) { |
|
749 |
val node_info = result.node_info |
|
750 |
val process_result = result.process_result |
|
77541 | 751 |
db.execute_statement(Results.table.insert(), body = |
752 |
{ stmt => |
|
753 |
stmt.string(1) = name |
|
754 |
stmt.string(2) = node_info.hostname |
|
755 |
stmt.int(3) = node_info.numa_node |
|
756 |
stmt.int(4) = process_result.rc |
|
757 |
stmt.string(5) = cat_lines(process_result.out_lines) |
|
758 |
stmt.string(6) = cat_lines(process_result.err_lines) |
|
759 |
stmt.long(7) = process_result.timing.elapsed.ms |
|
760 |
stmt.long(8) = process_result.timing.cpu.ms |
|
761 |
stmt.long(9) = process_result.timing.gc.ms |
|
762 |
}) |
|
77372 | 763 |
} |
764 |
||
765 |
insert.nonEmpty |
|
766 |
} |
|
767 |
||
77531 | 768 |
|
769 |
/* collective operations */ |
|
770 |
||
77596 | 771 |
val all_tables: SQL.Tables = |
772 |
SQL.Tables( |
|
77534 | 773 |
Base.table, |
774 |
Workers.table, |
|
775 |
Progress.table, |
|
776 |
Sessions.table, |
|
777 |
Pending.table, |
|
778 |
Running.table, |
|
779 |
Results.table, |
|
780 |
Host.Data.Node_Info.table) |
|
781 |
||
77416
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
782 |
def update_database( |
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
783 |
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
|
784 |
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
|
785 |
build_uuid: String, |
77416
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
786 |
hostname: String, |
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
787 |
state: State |
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
788 |
): State = { |
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
789 |
val changed = |
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
790 |
List( |
77496 | 791 |
update_sessions(db, state.sessions), |
77416
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
792 |
update_pending(db, state.pending), |
d88c12f22ab0
clarified scope of "serial" and "numa_index" within database;
wenzelm
parents:
77415
diff
changeset
|
793 |
update_running(db, state.running), |
77476 | 794 |
update_results(db, state.results), |
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77522
diff
changeset
|
795 |
Host.Data.update_numa_next(db, hostname, state.numa_next)) |
77372 | 796 |
|
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
797 |
val serial0 = serial_max(db) |
77513 | 798 |
val serial = if (changed.exists(identity)) State.inc_serial(serial0) else serial0 |
77372 | 799 |
|
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
800 |
stamp_worker(db, worker_uuid, serial) |
77583 | 801 |
state.set_serial(serial).set_workers(read_workers(db)) |
77372 | 802 |
} |
803 |
} |
|
77396 | 804 |
} |
77372 | 805 |
|
806 |
||
77436 | 807 |
|
808 |
/** main process **/ |
|
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
809 |
|
77505 | 810 |
class Build_Process( |
77530 | 811 |
protected final val build_context: Build_Process.Context, |
812 |
protected final val build_progress: Progress |
|
77505 | 813 |
) |
77436 | 814 |
extends AutoCloseable { |
815 |
/* context */ |
|
816 |
||
77530 | 817 |
protected final val store: Sessions.Store = build_context.store |
818 |
protected final val build_options: Options = store.options |
|
819 |
protected final val build_deps: Sessions.Deps = build_context.build_deps |
|
820 |
protected final val build_uuid: String = build_context.build_uuid |
|
821 |
protected final val worker_uuid: String = UUID.random().toString |
|
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
822 |
|
77638 | 823 |
override def toString: String = |
824 |
"Build_Process(worker_uuid = " + quote(worker_uuid) + ", build_uuid = " + quote(build_uuid) + |
|
825 |
if_proper(build_context.master, ", master = true") + ")" |
|
826 |
||
77436 | 827 |
|
77437
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
828 |
/* global state: internal var vs. external database */ |
77436 | 829 |
|
77437
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
830 |
private var _state: Build_Process.State = init_state(Build_Process.State()) |
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
831 |
|
77629 | 832 |
private val _database: Option[SQL.Database] = store.open_build_database() |
77372 | 833 |
|
77466
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
834 |
def close(): Unit = synchronized { _database.foreach(_.close()) } |
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
835 |
|
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
836 |
protected def synchronized_database[A](body: => A): A = |
77438 | 837 |
synchronized { |
77466
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
838 |
_database match { |
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
839 |
case None => body |
77635 | 840 |
case Some(db) => |
77637 | 841 |
@tailrec def loop(): A = { |
842 |
val sync_progress = |
|
843 |
db.transaction_lock(Build_Process.Data.all_tables) { |
|
844 |
val (messages, sync) = |
|
845 |
Build_Process.Data.sync_progress( |
|
846 |
db, _state.progress_seen, build_uuid, build_progress) |
|
847 |
if (sync) Left(body) else Right(messages) |
|
848 |
} |
|
849 |
sync_progress match { |
|
850 |
case Left(res) => res |
|
851 |
case Right(messages) => |
|
852 |
for ((message_serial, message) <- messages) { |
|
853 |
_state = _state.progress_serial(message_serial = message_serial) |
|
854 |
if (build_progress.do_output(message)) build_progress.output(message) |
|
855 |
} |
|
856 |
loop() |
|
857 |
} |
|
77635 | 858 |
} |
77637 | 859 |
loop() |
77436 | 860 |
} |
861 |
} |
|
862 |
||
77466
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
863 |
private def sync_database(): Unit = |
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
864 |
synchronized_database { |
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
865 |
for (db <- _database) { |
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
866 |
_state = |
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
867 |
Build_Process.Data.update_database( |
77529
40ccee0fe19a
separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents:
77527
diff
changeset
|
868 |
db, worker_uuid, build_uuid, build_context.hostname, _state) |
77466
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
869 |
} |
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
870 |
} |
77436 | 871 |
|
872 |
||
77505 | 873 |
/* progress backed by database */ |
874 |
||
77533 | 875 |
private def progress_output(message: Progress.Message, build_progress_output: => Unit): Unit = { |
77522
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
876 |
synchronized_database { |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
877 |
val state1 = _state.inc_serial.progress_serial() |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
878 |
for (db <- _database) { |
77529
40ccee0fe19a
separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents:
77527
diff
changeset
|
879 |
Build_Process.Data.write_progress(db, state1.serial, message, build_uuid) |
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
880 |
Build_Process.Data.stamp_worker(db, worker_uuid, state1.serial) |
77522
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
881 |
} |
77533 | 882 |
build_progress_output |
77522
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
883 |
_state = state1 |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
884 |
} |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
885 |
} |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
886 |
|
77530 | 887 |
protected object progress extends Progress { |
77509
3bc49507bae5
clarified treatment of "verbose" messages, e.g. Progress.theory();
wenzelm
parents:
77505
diff
changeset
|
888 |
override def verbose: Boolean = build_progress.verbose |
77505 | 889 |
|
77509
3bc49507bae5
clarified treatment of "verbose" messages, e.g. Progress.theory();
wenzelm
parents:
77505
diff
changeset
|
890 |
override def output(message: Progress.Message): Unit = |
77522
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
891 |
progress_output(message, if (do_output(message)) build_progress.output(message)) |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
892 |
|
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
893 |
override def theory(theory: Progress.Theory): Unit = |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
894 |
progress_output(theory.message, build_progress.theory(theory)) |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
895 |
|
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
896 |
override def nodes_status(nodes_status: Document_Status.Nodes_Status): Unit = |
a1d30297cd61
more complete coverage of non-final Progress methods, notably for Server.Connection_Progress;
wenzelm
parents:
77521
diff
changeset
|
897 |
build_progress.nodes_status(nodes_status) |
77505 | 898 |
|
899 |
override def stop(): Unit = build_progress.stop() |
|
900 |
override def stopped: Boolean = build_progress.stopped |
|
901 |
} |
|
902 |
||
77530 | 903 |
protected val log: Logger = Logger.make_system_log(progress, build_options) |
77505 | 904 |
|
905 |
||
77437
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
906 |
/* policy operations */ |
77333 | 907 |
|
77415
6b928419f109
clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents:
77414
diff
changeset
|
908 |
protected def init_state(state: Build_Process.State): Build_Process.State = { |
77496 | 909 |
val sessions1 = |
910 |
build_context.sessions.foldLeft(state.sessions) { case (map, (name, session)) => |
|
911 |
if (state.sessions.isDefinedAt(name)) map |
|
912 |
else map + (name -> session) |
|
913 |
} |
|
914 |
||
77415
6b928419f109
clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents:
77414
diff
changeset
|
915 |
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
|
916 |
val new_pending = |
6b928419f109
clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents:
77414
diff
changeset
|
917 |
List.from( |
6b928419f109
clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents:
77414
diff
changeset
|
918 |
for { |
77449 | 919 |
(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
|
920 |
if !old_pending(name) |
77585 | 921 |
} yield Build_Process.Task(name, session_context.deps)) |
77496 | 922 |
val pending1 = new_pending ::: state.pending |
923 |
||
924 |
state.copy(sessions = sessions1, pending = pending1) |
|
77415
6b928419f109
clarified signature: allow more general init, e.g. from existing database;
wenzelm
parents:
77414
diff
changeset
|
925 |
} |
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
926 |
|
77437
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
927 |
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
|
928 |
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
|
929 |
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
|
930 |
.sortBy(_.name)(build_context.ordering) |
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
931 |
.headOption.map(_.name) |
77296
eeaa2872320b
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77295
diff
changeset
|
932 |
} |
eeaa2872320b
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77295
diff
changeset
|
933 |
else None |
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
934 |
|
77467
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
935 |
protected def start_session(state: Build_Process.State, session_name: String): Build_Process.State = { |
77468 | 936 |
val ancestor_results = |
937 |
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
|
938 |
|
77460
ccca589d7027
tuned signature: support general Build_Job instances;
wenzelm
parents:
77459
diff
changeset
|
939 |
val input_shasum = |
77260 | 940 |
if (ancestor_results.isEmpty) { |
941 |
SHA1.shasum_meta_info(SHA1.digest(Path.explode("$POLYML_EXE"))) |
|
942 |
} |
|
77460
ccca589d7027
tuned signature: support general Build_Job instances;
wenzelm
parents:
77459
diff
changeset
|
943 |
else SHA1.flat_shasum(ancestor_results.map(_.output_shasum)) |
77257 | 944 |
|
77463 | 945 |
val store_heap = build_context.store_heap(session_name) |
77469 | 946 |
|
947 |
val (current, output_shasum) = |
|
948 |
store.check_output(session_name, |
|
949 |
sources_shasum = build_context.sources_shasum(session_name), |
|
950 |
input_shasum = input_shasum, |
|
951 |
fresh_build = build_context.fresh_build, |
|
952 |
store_heap = store_heap) |
|
953 |
||
77260 | 954 |
val all_current = current && ancestor_results.forall(_.current) |
77257 | 955 |
|
77260 | 956 |
if (all_current) { |
77467
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
957 |
state |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
958 |
.remove_pending(session_name) |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
959 |
.make_result(session_name, Process_Result.ok, output_shasum, current = true) |
77260 | 960 |
} |
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
961 |
else if (build_context.no_build) { |
77521
5642de4d225d
clarified signature: manage "verbose" flag via "progress";
wenzelm
parents:
77514
diff
changeset
|
962 |
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
|
963 |
state. |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
964 |
remove_pending(session_name). |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
965 |
make_result(session_name, Process_Result.error, output_shasum) |
77260 | 966 |
} |
77578
149d48a4801b
support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents:
77561
diff
changeset
|
967 |
else if (progress.stopped || !ancestor_results.forall(_.ok)) { |
77452 | 968 |
progress.echo(session_name + " CANCELLED") |
77467
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
969 |
state |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
970 |
.remove_pending(session_name) |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
971 |
.make_result(session_name, Process_Result.undefined, output_shasum) |
77452 | 972 |
} |
973 |
else { |
|
77632 | 974 |
val (numa_node, state1) = state.next_numa_node(build_context.numa_nodes) |
77551 | 975 |
val node_info = Host.Node_Info(build_context.hostname, numa_node) |
976 |
||
977 |
progress.echo( |
|
978 |
(if (store_heap) "Building " else "Running ") + session_name + |
|
979 |
if_proper(node_info.numa_node, " on " + node_info) + " ...") |
|
77260 | 980 |
|
77472 | 981 |
store.init_output(session_name) |
77257 | 982 |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
983 |
val build = |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
984 |
Build_Job.start_session(build_context, progress, log, |
77505 | 985 |
build_deps.background(session_name), input_shasum, node_info) |
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
986 |
|
77634 | 987 |
val job = Build_Process.Job(session_name, worker_uuid, build_uuid, node_info, Some(build)) |
988 |
||
989 |
state1.add_running(job) |
|
77260 | 990 |
} |
991 |
} |
|
77257 | 992 |
|
77436 | 993 |
|
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
994 |
/* build process roles */ |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
995 |
|
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
996 |
final def start_build(): Unit = synchronized_database { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
997 |
for (db <- _database) { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
998 |
Build_Process.Data.start_build(db, build_uuid, build_context.ml_platform, |
77635 | 999 |
build_context.sessions_structure.session_prefs, progress.stopped) |
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1000 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1001 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1002 |
|
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1003 |
final def stop_build(): Unit = synchronized_database { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1004 |
for (db <- _database) { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1005 |
Build_Process.Data.stop_build(db, build_uuid) |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1006 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1007 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1008 |
|
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1009 |
final def start_worker(): Unit = synchronized_database { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1010 |
for (db <- _database) { |
77584 | 1011 |
val java = ProcessHandle.current() |
1012 |
val java_pid = java.pid |
|
77588
066d5df144f0
assume total operation: ProcessHandle.current().info.startInstant appears to work on all platforms;
wenzelm
parents:
77587
diff
changeset
|
1013 |
val java_start = Date.instant(java.info.startInstant.get) |
77584 | 1014 |
val serial = |
1015 |
Build_Process.Data.start_worker( |
|
1016 |
db, worker_uuid, build_uuid, build_context.hostname, java_pid, java_start) |
|
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1017 |
_state = _state.set_serial(serial) |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1018 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1019 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1020 |
|
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1021 |
final def stop_worker(): Unit = synchronized_database { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1022 |
for (db <- _database) { |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1023 |
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
|
1024 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1025 |
} |
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1026 |
|
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1027 |
|
77436 | 1028 |
/* run */ |
77372 | 1029 |
|
77579
69d3547206db
clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents:
77578
diff
changeset
|
1030 |
def run(): Map[String, Process_Result] = { |
77466
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
1031 |
def finished(): Boolean = synchronized_database { _state.finished } |
77400
f3e5b3fe230e
clarified signature: more explicit "synchronized" regions;
wenzelm
parents:
77398
diff
changeset
|
1032 |
|
77437
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
1033 |
def sleep(): Unit = |
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
1034 |
Isabelle_Thread.interrupt_handler(_ => progress.stop()) { |
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
1035 |
build_options.seconds("editor_input_delay").sleep() |
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
1036 |
} |
dcbf96acae27
clarified signature: do not expose global state to object-oriented variants;
wenzelm
parents:
77436
diff
changeset
|
1037 |
|
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1038 |
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
|
1039 |
next_job(_state) match { |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1040 |
case Some(name) => |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1041 |
if (Build_Job.is_session_name(name)) { |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1042 |
_state = start_session(_state, name) |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1043 |
true |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1044 |
} |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1045 |
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
|
1046 |
case None => false |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1047 |
} |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1048 |
} |
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1049 |
|
77335 | 1050 |
if (finished()) { |
1051 |
progress.echo_warning("Nothing to build") |
|
1052 |
Map.empty[String, Process_Result] |
|
1053 |
} |
|
1054 |
else { |
|
77579
69d3547206db
clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents:
77578
diff
changeset
|
1055 |
if (build_context.master) start_build() |
77578
149d48a4801b
support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents:
77561
diff
changeset
|
1056 |
|
77580
32f9e75c92e9
clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents:
77579
diff
changeset
|
1057 |
start_worker() |
32f9e75c92e9
clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents:
77579
diff
changeset
|
1058 |
if (build_context.master && !build_context.worker_active) { |
32f9e75c92e9
clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents:
77579
diff
changeset
|
1059 |
progress.echo("Waiting for external workers ...") |
32f9e75c92e9
clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents:
77579
diff
changeset
|
1060 |
} |
77578
149d48a4801b
support for "isabelle build -j0": require external workers to make progress;
wenzelm
parents:
77561
diff
changeset
|
1061 |
|
77538 | 1062 |
try { |
1063 |
while (!finished()) { |
|
1064 |
if (progress.stopped) synchronized_database { _state.stop_running() } |
|
77310 | 1065 |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
1066 |
for (build <- synchronized_database { _state.finished_running() }) { |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
1067 |
val job_name = build.job_name |
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
1068 |
val (process_result, output_shasum) = build.join |
77538 | 1069 |
synchronized_database { |
1070 |
_state = _state. |
|
1071 |
remove_pending(job_name). |
|
1072 |
remove_running(job_name). |
|
77630
86ef80d13544
clarified signature: avoid confusion due to object-orientation;
wenzelm
parents:
77629
diff
changeset
|
1073 |
make_result(job_name, process_result, output_shasum, node_info = build.node_info) |
77538 | 1074 |
} |
1075 |
} |
|
1076 |
||
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1077 |
if (!start_job()) { |
77538 | 1078 |
sync_database() |
1079 |
sleep() |
|
77396 | 1080 |
} |
1081 |
} |
|
77310 | 1082 |
} |
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1083 |
finally { |
77580
32f9e75c92e9
clarified worker state: always maintain database content via worker_uuid;
wenzelm
parents:
77579
diff
changeset
|
1084 |
stop_worker() |
77579
69d3547206db
clarified signature: prefer Build_Process.Context for parameters;
wenzelm
parents:
77578
diff
changeset
|
1085 |
if (build_context.master) stop_build() |
77546
9b9179cda155
clarified build process roles: "worker" vs. "build";
wenzelm
parents:
77545
diff
changeset
|
1086 |
} |
77467
e27bc7957297
more robust: proper synchronization of transition from next_job to start_session;
wenzelm
parents:
77466
diff
changeset
|
1087 |
|
77466
94dcf2c3895a
more thorough synchronized_database for internal *and* external state;
wenzelm
parents:
77465
diff
changeset
|
1088 |
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
|
1089 |
for ((name, result) <- _state.results) yield name -> result.process_result |
77257 | 1090 |
} |
1091 |
} |
|
1092 |
} |
|
77238 | 1093 |
} |