mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2026-03-16 21:07:30 +01:00
Reduce insert pressure in db. Increase sqlite timeout value
Entire-Checkpoint: a1e2931d4deb
This commit is contained in:
@@ -46,6 +46,12 @@ type RepositoryConfig struct {
|
||||
// It's a soft limit — queries won't fail, but cache eviction becomes more aggressive.
|
||||
// Default: 16384 (16GB)
|
||||
DbSoftHeapLimitMB int
|
||||
|
||||
// BusyTimeoutMs is the SQLite busy_timeout in milliseconds.
|
||||
// When a write is blocked by another writer, SQLite retries internally
|
||||
// using a backoff mechanism for up to this duration before returning SQLITE_BUSY.
|
||||
// Default: 60000 (60 seconds)
|
||||
BusyTimeoutMs int
|
||||
}
|
||||
|
||||
// DefaultConfig returns the default repository configuration.
|
||||
@@ -60,6 +66,7 @@ func DefaultConfig() *RepositoryConfig {
|
||||
MinRunningJobDuration: 600, // 10 minutes
|
||||
DbCacheSizeMB: 2048, // 2GB per connection
|
||||
DbSoftHeapLimitMB: 16384, // 16GB process-wide
|
||||
BusyTimeoutMs: 60000, // 60 seconds
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ func Connect(db string) {
|
||||
connectionURLParams := make(url.Values)
|
||||
connectionURLParams.Add("_txlock", "immediate")
|
||||
connectionURLParams.Add("_journal_mode", "WAL")
|
||||
connectionURLParams.Add("_busy_timeout", "5000")
|
||||
connectionURLParams.Add("_busy_timeout", fmt.Sprintf("%d", repoConfig.BusyTimeoutMs))
|
||||
connectionURLParams.Add("_synchronous", "NORMAL")
|
||||
cacheSizeKiB := repoConfig.DbCacheSizeMB * 1024 // Convert MB to KiB
|
||||
connectionURLParams.Add("_cache_size", fmt.Sprintf("-%d", cacheSizeKiB))
|
||||
|
||||
@@ -92,20 +92,33 @@ func (r *JobRepository) SyncJobs() ([]*schema.Job, error) {
|
||||
jobs = append(jobs, job)
|
||||
}
|
||||
|
||||
// Transfer cached jobs to main table and clear cache in a single transaction.
|
||||
tx, err := r.DB.Beginx()
|
||||
if err != nil {
|
||||
cclog.Errorf("SyncJobs: begin transaction: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// Use INSERT OR IGNORE to skip jobs already transferred by the stop path
|
||||
_, err = r.DB.Exec(
|
||||
_, err = tx.Exec(
|
||||
"INSERT OR IGNORE INTO job (job_id, cluster, subcluster, start_time, hpc_user, project, cluster_partition, array_job_id, num_nodes, num_hwthreads, num_acc, shared, monitoring_status, smt, job_state, duration, walltime, footprint, energy, energy_footprint, resources, meta_data) SELECT job_id, cluster, subcluster, start_time, hpc_user, project, cluster_partition, array_job_id, num_nodes, num_hwthreads, num_acc, shared, monitoring_status, smt, job_state, duration, walltime, footprint, energy, energy_footprint, resources, meta_data FROM job_cache")
|
||||
if err != nil {
|
||||
cclog.Errorf("Error while Job sync: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = r.DB.Exec("DELETE FROM job_cache")
|
||||
_, err = tx.Exec("DELETE FROM job_cache")
|
||||
if err != nil {
|
||||
cclog.Errorf("Error while Job cache clean: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
cclog.Errorf("SyncJobs: commit transaction: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Resolve correct job.id from the job table. The IDs read from job_cache
|
||||
// are from a different auto-increment sequence and must not be used to
|
||||
// query the job table.
|
||||
|
||||
@@ -244,6 +244,77 @@ func (r *NodeRepository) UpdateNodeState(hostname string, cluster string, nodeSt
|
||||
return nil
|
||||
}
|
||||
|
||||
// NodeStateUpdate holds the data needed to update one node's state in a batch operation.
|
||||
type NodeStateUpdate struct {
|
||||
Hostname string
|
||||
Cluster string
|
||||
NodeState *schema.NodeStateDB
|
||||
}
|
||||
|
||||
// BatchUpdateNodeStates inserts node state rows for multiple nodes in a single transaction.
|
||||
// For each node, it looks up (or creates) the node row, then inserts the state row.
|
||||
// This reduces lock acquisitions from 2*N to 1 for N nodes.
|
||||
func (r *NodeRepository) BatchUpdateNodeStates(updates []NodeStateUpdate) error {
|
||||
tx, err := r.DB.Beginx()
|
||||
if err != nil {
|
||||
return fmt.Errorf("BatchUpdateNodeStates: begin transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
stmtLookup, err := tx.Preparex("SELECT id FROM node WHERE hostname = ? AND cluster = ?")
|
||||
if err != nil {
|
||||
return fmt.Errorf("BatchUpdateNodeStates: prepare lookup: %w", err)
|
||||
}
|
||||
defer stmtLookup.Close()
|
||||
|
||||
stmtInsertNode, err := tx.PrepareNamed(NamedNodeInsert)
|
||||
if err != nil {
|
||||
return fmt.Errorf("BatchUpdateNodeStates: prepare node insert: %w", err)
|
||||
}
|
||||
defer stmtInsertNode.Close()
|
||||
|
||||
stmtInsertState, err := tx.PrepareNamed(NamedNodeStateInsert)
|
||||
if err != nil {
|
||||
return fmt.Errorf("BatchUpdateNodeStates: prepare state insert: %w", err)
|
||||
}
|
||||
defer stmtInsertState.Close()
|
||||
|
||||
for _, u := range updates {
|
||||
var id int64
|
||||
if err := stmtLookup.QueryRow(u.Hostname, u.Cluster).Scan(&id); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
subcluster, scErr := archive.GetSubClusterByNode(u.Cluster, u.Hostname)
|
||||
if scErr != nil {
|
||||
cclog.Errorf("BatchUpdateNodeStates: subcluster lookup for '%s' in '%s': %v", u.Hostname, u.Cluster, scErr)
|
||||
continue
|
||||
}
|
||||
node := schema.NodeDB{
|
||||
Hostname: u.Hostname, Cluster: u.Cluster, SubCluster: subcluster,
|
||||
}
|
||||
res, insertErr := stmtInsertNode.Exec(&node)
|
||||
if insertErr != nil {
|
||||
cclog.Errorf("BatchUpdateNodeStates: insert node '%s': %v", u.Hostname, insertErr)
|
||||
continue
|
||||
}
|
||||
id, _ = res.LastInsertId()
|
||||
} else {
|
||||
cclog.Errorf("BatchUpdateNodeStates: lookup node '%s': %v", u.Hostname, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
u.NodeState.NodeID = id
|
||||
if _, err := stmtInsertState.Exec(u.NodeState); err != nil {
|
||||
cclog.Errorf("BatchUpdateNodeStates: insert state for '%s': %v", u.Hostname, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("BatchUpdateNodeStates: commit: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// func (r *NodeRepository) UpdateHealthState(hostname string, healthState *schema.MonitoringState) error {
|
||||
// if _, err := sq.Update("node").Set("health_state", healthState).Where("node.id = ?", id).RunWith(r.DB).Exec(); err != nil {
|
||||
// cclog.Errorf("error while updating node '%d'", id)
|
||||
|
||||
@@ -188,6 +188,21 @@ func (r *UserRepository) AddUser(user *schema.User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddUserIfNotExists inserts a user only if the username does not already exist.
|
||||
// Uses INSERT OR IGNORE to avoid UNIQUE constraint errors when a user is
|
||||
// concurrently created (e.g., by a login while LDAP sync is running).
|
||||
// Unlike AddUser, this intentionally skips the deprecated default metrics config insertion.
|
||||
func (r *UserRepository) AddUserIfNotExists(user *schema.User) error {
|
||||
rolesJson, _ := json.Marshal(user.Roles)
|
||||
projectsJson, _ := json.Marshal(user.Projects)
|
||||
|
||||
cols := "username, name, roles, projects, ldap"
|
||||
_, err := r.DB.Exec(
|
||||
`INSERT OR IGNORE INTO hpc_user (`+cols+`) VALUES (?, ?, ?, ?, ?)`,
|
||||
user.Username, user.Name, string(rolesJson), string(projectsJson), int(user.AuthSource))
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *UserRepository) UpdateUser(dbUser *schema.User, user *schema.User) error {
|
||||
// user contains updated info -> Apply to dbUser
|
||||
// --- Simple Name Update ---
|
||||
|
||||
Reference in New Issue
Block a user