Modelle pinnen: dauerhaft, für einen Zeitraum oder wöchentlich
- Pins liegen in der Datenbank und werden mit omega-proxy pin add/list/remove verwaltet; der laufende Proxy liest sie alle 5 s neu - Gepinnte Modelle lädt der Proxy selbst, schon pin_preload_secs vor Beginn, und verdrängt sie nie; Anfragen weichen auf andere Rechner aus oder warten - Beim Anlegen prüft er, ob die gleichzeitig gepinnten Modelle jedes Rechners zu jedem Zeitpunkt in den Speicher passen - Wöchentliche Pins (z. B. für Vorlesungen) lassen sich auf einen Zeitraum wie ein Semester begrenzen - Unit-Tests für die Zeitlogik, tests/multispark/pin_test.py am laufenden System Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5c3317a27c
commit
4723f56b2c
17 files changed
+1098
-145
No files matched your search
+159
-2
@@ -2,15 +2,23 @@ mod config;
|
||||
mod db;
|
||||
mod engines;
|
||||
mod forward;
|
||||
mod pins;
|
||||
mod routes;
|
||||
mod scheduler;
|
||||
|
||||
use std::{path::PathBuf, sync::Arc, time::Duration};
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use crate::{config::Config, db::Db, routes::AppState, scheduler::Scheduler};
|
||||
use crate::{
|
||||
config::Config,
|
||||
db::Db,
|
||||
engines::Engine,
|
||||
pins::{Pin, Weekly},
|
||||
routes::AppState,
|
||||
scheduler::Scheduler,
|
||||
};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about = "Omega AI Proxy")]
|
||||
@@ -28,6 +36,9 @@ enum Command {
|
||||
/// API-Keys verwalten
|
||||
#[command(subcommand)]
|
||||
Key(KeyCommand),
|
||||
/// Modelle pinnen: sie sind dann garantiert im Speicher und werden nie verdrängt
|
||||
#[command(subcommand)]
|
||||
Pin(PinCommand),
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -38,6 +49,35 @@ enum KeyCommand {
|
||||
Revoke { id: i64 },
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum PinCommand {
|
||||
/// Pin anlegen: dauerhaft, von-bis oder wöchentlich (auch begrenzt mit --from/--until)
|
||||
Add {
|
||||
/// Engine und Modell, z. B. ollama/qwen3:0.6b, llamacpp/qwen3-0.6b, vllm/qwen2.5-0.5b
|
||||
model: String,
|
||||
/// Rechner; nur nötig, wenn mehrere das Modell anbieten
|
||||
#[arg(long)]
|
||||
node: Option<String>,
|
||||
/// Beginn, z. B. "2026-11-24 08:00", "2026-11-24" oder "+10m"
|
||||
#[arg(long)]
|
||||
from: Option<String>,
|
||||
/// Ende, Format wie --from
|
||||
#[arg(long)]
|
||||
until: Option<String>,
|
||||
/// Wochentage, z. B. "mo,mi" oder "mo-fr"
|
||||
#[arg(long, requires = "time")]
|
||||
weekly: Option<String>,
|
||||
/// Uhrzeit für --weekly, z. B. "08:00-11:15"
|
||||
#[arg(long, requires = "weekly")]
|
||||
time: Option<String>,
|
||||
/// Anmerkung, z. B. die Lehrveranstaltung
|
||||
#[arg(long, default_value = "")]
|
||||
note: String,
|
||||
},
|
||||
List,
|
||||
Remove { id: i64 },
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
@@ -70,12 +110,129 @@ async fn main() -> Result<()> {
|
||||
println!("Key {id} widerrufen");
|
||||
Ok(())
|
||||
}
|
||||
Command::Pin(PinCommand::Add { model, node, from, until, weekly, time, note }) => {
|
||||
add_pin(config, db, model, node, from, until, weekly, time, note).await
|
||||
}
|
||||
Command::Pin(PinCommand::List) => {
|
||||
let now = chrono::Local::now();
|
||||
for p in db.list_pins()? {
|
||||
let state = if p.active_at(now) { "aktiv" } else { "" };
|
||||
println!(
|
||||
"{:>4} {:<30} {:<10} {:<50} {:<6} {}",
|
||||
p.id,
|
||||
format!("{}/{}", p.engine, p.model),
|
||||
p.node,
|
||||
p.describe(),
|
||||
state,
|
||||
p.note
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Command::Pin(PinCommand::Remove { id }) => {
|
||||
if !db.remove_pin(id)? {
|
||||
bail!("kein Pin mit ID {id}");
|
||||
}
|
||||
println!("Pin {id} entfernt");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn add_pin(
|
||||
config: Config,
|
||||
db: Arc<Db>,
|
||||
model: String,
|
||||
node: Option<String>,
|
||||
from: Option<String>,
|
||||
until: Option<String>,
|
||||
weekly: Option<String>,
|
||||
time: Option<String>,
|
||||
note: String,
|
||||
) -> Result<()> {
|
||||
let (engine_name, model) = model.split_once('/').context("Modell als engine/modell angeben, z. B. ollama/qwen3:0.6b")?;
|
||||
let engine = Engine::parse(engine_name).with_context(|| format!("unbekannte Engine {engine_name} (ollama, llamacpp, vllm)"))?;
|
||||
let model = engines::normalize_model(engine, model);
|
||||
|
||||
let now = chrono::Local::now();
|
||||
let from = from.map(|s| pins::parse_when(&s, now)).transpose()?;
|
||||
let until = until.map(|s| pins::parse_when(&s, now)).transpose()?;
|
||||
if let (Some(f), Some(u)) = (from, until)
|
||||
&& f >= u
|
||||
{
|
||||
bail!("--until muss nach --from liegen");
|
||||
}
|
||||
let weekly = match (weekly, time) {
|
||||
(Some(days), Some(time)) => {
|
||||
let (start, end) = pins::parse_time_range(&time)?;
|
||||
Some(Weekly { days: pins::parse_days(&days)?, start, end })
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// Der Scheduler fragt die Engines, welche Modelle es wo gibt.
|
||||
let sched = Scheduler::new(config.nodes, config.scheduler, db.clone()).await?;
|
||||
let offering = sched.nodes_offering(engine, &model);
|
||||
let node = match node {
|
||||
Some(name) => {
|
||||
let i = sched.node_index(&name).with_context(|| format!("unbekannter Rechner {name}"))?;
|
||||
if !offering.contains(&i) {
|
||||
bail!("{name} bietet {engine_name}/{model} nicht an");
|
||||
}
|
||||
i
|
||||
}
|
||||
None => match offering.as_slice() {
|
||||
[] => bail!("{engine_name}/{model} wird auf keinem Rechner angeboten"),
|
||||
[i] => *i,
|
||||
several => bail!(
|
||||
"{engine_name}/{model} gibt es auf mehreren Rechnern ({}); bitte --node angeben",
|
||||
several.iter().map(|&i| sched.nodes[i].name.as_str()).collect::<Vec<_>>().join(", ")
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
let pin = Pin {
|
||||
id: 0,
|
||||
node: sched.nodes[node].name.clone(),
|
||||
engine: engine_name.to_string(),
|
||||
model: model.clone(),
|
||||
from: from.map(|t| t.timestamp()),
|
||||
until: until.map(|t| t.timestamp()),
|
||||
weekly,
|
||||
note,
|
||||
};
|
||||
if pin.until.is_some_and(|u| u <= now.timestamp()) {
|
||||
bail!("der Zeitraum liegt in der Vergangenheit");
|
||||
}
|
||||
|
||||
let mut all = db.list_pins()?;
|
||||
all.push(pin.clone());
|
||||
pins::check_conflicts(
|
||||
&all,
|
||||
now,
|
||||
|p| match (sched.node_index(&p.node), Engine::parse(&p.engine)) {
|
||||
(Some(n), Some(e)) => sched.need_mb(n, e, &p.model),
|
||||
_ => 0,
|
||||
},
|
||||
|name| sched.node_index(name).map(|n| sched.budget(n)).unwrap_or(0),
|
||||
)
|
||||
.context("Pin passt nicht in den Speicher")?;
|
||||
|
||||
let id = db.add_pin(&pin)?;
|
||||
println!(
|
||||
"Pin {id}: {engine_name}/{model} auf {} ({} MB), {}",
|
||||
pin.node,
|
||||
sched.need_mb(node, engine, &model),
|
||||
pin.describe()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn serve(config: Config, db: Arc<Db>) -> Result<()> {
|
||||
let sched = Scheduler::new(config.nodes, config.scheduler, db.clone()).await?;
|
||||
sched.spawn_reconciler();
|
||||
sched.spawn_pin_watcher();
|
||||
let http = reqwest::Client::builder()
|
||||
.connect_timeout(Duration::from_secs(5))
|
||||
.build()?;
|
||||
|
||||
Reference in new issue
Block a user