def make(new_graph: Sessions.Graph): Sessions = if (graph == new_graph) this else { new Sessions(
new_graph.iterator.foldLeft(new_graph) { case (g, (name, (session, _))) => g.add_deps_acyclic(name, session.deps)
})
}
def update(updates: List[Update.Op[Build_Job.Session_Context]]): Sessions = { val graph1 =
updates.foldLeft(graph) { case (g, Update.Delete(name)) => g.del_node(name) case (g, Update.Insert(session)) =>
(if (g.defined(session.name)) g.del_node(session.name) else g)
.new_node(session.name, session)
}
make(graph1)
}
def init(
build_context: isabelle.Build.Context,
database_server: Option[SQL.Database],
progress: Progress = new Progress
): Sessions = { val sessions_structure = build_context.sessions_structure
make(
sessions_structure.build_graph.iterator.foldLeft(graph) { case (graph0, (name, (info, _))) => val deps = info.parent.toList val prefs = info.session_prefs val ancestors = sessions_structure.build_requirements(deps) val sources_shasum = build_context.deps.sources_shasum(name)
if (graph0.defined(name)) { val session0 = graph0.get_node(name) val prefs0 = session0.session_prefs val ancestors0 = session0.ancestors val sources_shasum0 = session0.sources_shasum
lazyval max_time: Map[String, Double] = { val maximals = graph.maximals.toSet def descendants_time(name: String): Double = { if (maximals.contains(name)) apply(name).old_time.seconds else { val descendants = graph.all_succs(List(name)).toSet val g = graph.restrict(descendants)
(0.0 :: g.maximals.flatMap { desc => val ps = g.all_preds(List(desc)) if (ps.exists(p => !graph.defined(p))) None else Some(ps.map(p => apply(p).old_time.seconds).sum)
}).max
}
}
Map.from( for (name <- graph.keys_iterator) yield name -> descendants_time(name)).withDefaultValue(0.0)
}
lazyval ordering: Ordering[String] =
(a: String, b: String) =>
max_time(b) compare max_time(a) match { case 0 =>
apply(b).timeout compare apply(a).timeout match { case 0 => a compare b case ord => ord
} case ord => ord
}
}
def remove_pending(a: String): State =
copy(pending =
pending.foldLeft(pending) { case (map, (b, task)) => if (a == b) map - a else {
task.resolve(a) match { case None => map case Some(task1) => map + (b -> task1)
}
}
})
object Generic { val build_id = SQL.Column.long("build_id") val build_uuid = SQL.Column.string("build_uuid") val worker_uuid = SQL.Column.string("worker_uuid") val name = SQL.Column.string("name")
object Updates { val build_id = Generic.build_id.make_primary_key val serial = SQL.Column.long("serial").make_primary_key val kind = SQL.Column.int("kind").make_primary_key val name = Generic.name.make_primary_key
val table = make_table(List(build_id, serial, kind, name), name = "updates")
// virtual columns for JOIN with data val delete = SQL.Column.bool("delete").make_expr(name.undefined) val dom = SQL.Column.string("dom") val dom_name = dom.make_expr(name.ident) val name_dom = name.make_expr(dom.ident)
}
db.execute_query_statement(select_sql, List.from[Update.Op[A]],
res => if (res.bool(Updates.delete)) Update.Delete(res.string(Updates.name)) else Update.Insert(get(res)))
}
def write_updates(
db: SQL.Database,
build_id: Long,
serial: Long,
updates: List[Update]
): Unit =
db.execute_batch_statement(db.insert_permissive(Updates.table), batch = for (update <- updates.iterator; kind = update.kind; name <- update.domain.iterator) yield { (stmt: SQL.Statement) =>
require(build_id > 0 && serial > 0 && kind > 0 && name.nonEmpty, "Bad database update: build_id = " + build_id + ", serial = " + serial + ", kind = " + kind + ", name = " + quote(name))
stmt.long(1) = build_id
stmt.long(2) = serial
stmt.int(3) = kind
stmt.string(4) = name
})
/* base table */
object Base { val build_uuid = Generic.build_uuid.make_primary_key val build_id = Generic.build_id.make_primary_key val ml_platform = SQL.Column.string("ml_platform") val options = SQL.Column.string("options") val start = SQL.Column.date("start") val stop = SQL.Column.date("stop")
val table = make_table(List(build_uuid, build_id, ml_platform, options, start, stop))
}
def clean_build(db: SQL.Database): Unit = { val remove =
db.execute_query_statement(
Base.table.select(List(Base.build_uuid), sql = SQL.where(Base.stop.defined)),
List.from[String], res => res.string(Base.build_uuid))
remove_builds(db, remove)
}
/* sessions */
object Sessions { val name = Generic.name.make_primary_key val deps = SQL.Column.string("deps") val ancestors = SQL.Column.string("ancestors") val options = SQL.Column.string("options") val sources = SQL.Column.string("sources") val timeout = SQL.Column.long("timeout") val old_time = SQL.Column.long("old_time") val old_command_timings = SQL.Column.bytes("old_command_timings") val build_uuid = Generic.build_uuid
val table =
make_table(
List(name, deps, ancestors, options, sources, timeout,
old_time, old_command_timings, build_uuid),
name = "sessions")
object Workers { val worker_uuid = Generic.worker_uuid.make_primary_key val build_uuid = Generic.build_uuid val start = SQL.Column.date("start") val stamp = SQL.Column.date("stamp") val stop = SQL.Column.date("stop") val serial = SQL.Column.long("serial")
val table =
make_table(List(worker_uuid, build_uuid, start, stamp, stop, serial), name = "workers")
lazyval table_index: Int = tables.index(table)
}
def read_serial(db: SQL.Database): Long =
db.execute_query_statementO[Long](
Workers.table.select(List(Workers.serial.max)), _.long(Workers.serial)).getOrElse(0L)
val build_stop =
db.execute_query_statementO(
Base.table.select(List(Base.stop), sql = Base.build_uuid.where_equal(build_uuid)),
res => res.get_date(Base.stop))
build_stop match { case Some(None) => case Some(Some(_)) => err("for already stopped build process " + build_uuid) case None => err("for unknown build process " + build_uuid)
}
db.execute_statement(Workers.table.insert(), body =
{ stmt => val now = db.now()
stmt.string(1) = worker_uuid
stmt.string(2) = build_uuid
stmt.date(3) = now
stmt.date(4) = now
stmt.date(5) = None
stmt.long(6) = serial
})
}
def stamp_worker(
db: SQL.Database,
worker_uuid: String,
serial: Long,
stop_now: Boolean = false
): Unit = { val sql = Workers.worker_uuid.where_equal(worker_uuid)
val stop =
db.execute_query_statementO(
Workers.table.select(List(Workers.stop), sql = sql), _.get_date(Workers.stop)).flatten
db.execute_statement(
Workers.table.update(List(Workers.stamp, Workers.stop, Workers.serial), sql = sql),
body = { stmt => val now = db.now()
stmt.date(1) = now
stmt.date(2) = if (stop_now) Some(now) else stop
stmt.long(3) = serial
})
}
/* pending jobs */
object Pending { val name = Generic.name.make_primary_key val deps = SQL.Column.string("deps") val build_uuid = Generic.build_uuid
val table = make_table(List(name, deps, build_uuid), name = "pending")
lazyval table_index: Int = tables.index(table)
}
def pull_pending(db: SQL.Database, build_id: Long, state: State): State.Pending =
Update.data(state.pending,
read_updates(db, Pending.table, build_id, state.serial,
{ res => val name = res.string(Pending.name) val deps = res.string(Pending.deps) val build_uuid = res.string(Pending.build_uuid)
Task(name, split_lines(deps), build_uuid)
})
)
if (update.deletes) {
db.execute_statement(
Pending.table.delete(sql = Generic.sql_where(names = update.delete)))
}
if (update.inserts) {
db.execute_batch_statement(Pending.table.insert(), batch = for (name <- update.insert) yield { (stmt: SQL.Statement) => val task = pending(name)
stmt.string(1) = task.name
stmt.string(2) = cat_lines(task.deps)
stmt.string(3) = task.build_uuid
})
}
update
}
/* running jobs */
object Running { val name = Generic.name.make_primary_key val worker_uuid = Generic.worker_uuid val build_uuid = Generic.build_uuid val hostname = SQL.Column.string("hostname") val numa_node = SQL.Column.int("numa_node") val rel_cpus = SQL.Column.string("rel_cpus") val start_date = SQL.Column.date("start_date")
val table =
make_table(
List(name, worker_uuid, build_uuid, hostname, numa_node, rel_cpus, start_date),
name = "running")
lazyval table_index: Int = tables.index(table)
}
def pull_running(db: SQL.Database, build_id: Long, state: State): State.Running =
Update.data(state.running,
read_updates(db, Running.table, build_id, state.serial,
{ res => val name = res.string(Running.name) val worker_uuid = res.string(Running.worker_uuid) val build_uuid = res.string(Running.build_uuid) val hostname = res.string(Running.hostname) val numa_node = res.get_int(Running.numa_node) val rel_cpus = res.string(Running.rel_cpus) val start_date = res.date(Running.start_date) val node_info = Host.Node_Info(hostname, numa_node, Host.Range.from(rel_cpus))
object Results { val name = Generic.name.make_primary_key val worker_uuid = Generic.worker_uuid val build_uuid = Generic.build_uuid val hostname = SQL.Column.string("hostname") val numa_node = SQL.Column.int("numa_node") val rel_cpus = SQL.Column.string("rel_cpus") val start_date = SQL.Column.date("start_date") val rc = SQL.Column.int("rc") val out = SQL.Column.string("out") val err = SQL.Column.string("err") val timing_elapsed = SQL.Column.long("timing_elapsed") val timing_cpu = SQL.Column.long("timing_cpu") val timing_gc = SQL.Column.long("timing_gc") val output_shasum = SQL.Column.string("output_shasum") val current = SQL.Column.bool("current")
def pull_state(db: SQL.Database, build_id: Long, worker_uuid: String, state: State): State = { val serial_db = read_serial(db) if (serial_db == state.serial) state else { val serial = serial_db max state.serial
stamp_worker(db, worker_uuid, serial)
val sessions = pull_sessions(db, build_id, state) val pending = pull_pending(db, build_id, state) val running = pull_running(db, build_id, state) val results = pull_results(db, build_id, state)
protecteddef synchronized_database[A](label: String)(body: => A): A =
synchronized {
_build_database match { case None => body case Some(db) =>
Build_Process.private_data.transaction_lock(db, label = label) { val old_state =
Build_Process.private_data.pull_state(db, build_id, worker_uuid, _state)
_state = old_state val res = body
_state =
Build_Process.private_data.push_state(
db, build_id, worker_uuid, _state, old_state)
res
}
}
}
/* policy operations */
protecteddef next_jobs(state: Build_Process.State): List[String] = { val limit = { if (progress.stopped) { if (build_context.master) Int.MaxValue else 0 } else build_context.jobs - state.build_running.length
} if (limit > 0) state.next_ready.sortBy(_.name)(state.sessions.ordering).take(limit).map(_.name) else Nil
}
protecteddef next_node_info(state: Build_Process.State, session_name: String): Host.Node_Info = { val available_nodes = build_context.numa_nodes val used_nodes =
Set.from(for (job <- state.running.valuesIterator; i <- job.node_info.numa_node) yield i) val numa_node = Host.next_numa_node(_host_database, hostname, available_nodes, used_nodes)
Host.Node_Info(hostname, numa_node, Nil)
}
val finished = current && ancestor_results.forall(_.current) val skipped = build_context.no_build val cancelled = progress.stopped || !ancestor_results.forall(_.ok)
if (!skipped && !cancelled) { for (db <- _database_server orElse _heaps_database) { val hierarchy =
(session_name :: ancestor_results.map(_.name))
.map(store.output_session(_, store_heap = true))
ML_Heap.restore(db, hierarchy, cache = store.cache.compress)
}
}
val result_name = (session_name, worker_uuid, build_uuid)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.