Store SCUM facts in typed platform tables
Run pushes typed SCUM facts through POST /api/v1/run/scum/facts, but the production router rejected that path before the signature middleware because runServiceRequest listed individual Run channel prefixes, and MySQL kept SCUM rows inside the whole metadata snapshot instead of platform tables. - /api/v1/run/ is now the signed machine channel space while /api/v1/run/endpoints keeps normal bearer/admin authorization. - MySQL gets real scum_user, scum_user_trajectory, scum_vehicle, scum_vehicle_trajectory and scum_vehicle_lock tables with parameterized per-row repositories instead of full snapshot rewrites. Snapshot-shaped tables from the unreleased interim build are replaced, and SCUM rows still inside a metadata snapshot are migrated once. - Facts ingest verifies the target server plugin type and converges stale online users to offline after SCUMUserOfflineAfter. - The plugin page and browser read one bounded /scum/surface response instead of five list calls per refresh. Call-count budget for one server: per 5s facts batch, one SELECT plus one INSERT/UPDATE per reported user and vehicle, one INSERT per moved trajectory sample or new lock row, one bounded stale-user SELECT, and a trajectory retention DELETE at most once per hour. One browser refresh issues one surface request every 15s instead of five list requests.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -21,9 +22,11 @@ const (
|
||||
|
||||
type MySQLStore struct {
|
||||
*MemoryStore
|
||||
db *sql.DB
|
||||
persistMu sync.Mutex
|
||||
runtimePersistMu sync.Mutex
|
||||
db *sql.DB
|
||||
persistMu sync.Mutex
|
||||
runtimePersistMu sync.Mutex
|
||||
scumUserTrackPruner scumTrajectoryPruner
|
||||
scumVehicleTrackPruner scumTrajectoryPruner
|
||||
}
|
||||
|
||||
func NewMySQLStore(dsn string) (*MySQLStore, error) {
|
||||
@@ -31,7 +34,7 @@ func NewMySQLStore(dsn string) (*MySQLStore, error) {
|
||||
if dsn == "" {
|
||||
return nil, fmt.Errorf("PLATFORM_MYSQL_DSN is required when PLATFORM_STORAGE_BACKEND=mysql")
|
||||
}
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
db, err := sql.Open("mysql", mysqlDSNWithParseTime(dsn))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open mysql metadata store: %w", err)
|
||||
}
|
||||
@@ -150,21 +153,6 @@ func (store *MySQLStore) GameClientBridgeSnapshotStreams() GameClientBridgeSnaps
|
||||
func (store *MySQLStore) PluginDataRecords() PluginDataRecordRepository {
|
||||
return &persistentRepository[domain.PluginDataRecord, domain.PluginDataFilter]{repository: store.MemoryStore.pluginDataRecords, persist: store.persist}
|
||||
}
|
||||
func (store *MySQLStore) SCUMUsers() SCUMUserRepository {
|
||||
return &persistentRepository[domain.SCUMUser, domain.SCUMUserFilter]{repository: store.MemoryStore.scumUsers, persist: store.persist}
|
||||
}
|
||||
func (store *MySQLStore) SCUMUserTrajectories() SCUMUserTrajectoryRepository {
|
||||
return &persistentRepository[domain.SCUMUserTrajectory, domain.SCUMUserTrajectoryFilter]{repository: store.MemoryStore.scumUserTracks, persist: store.persist}
|
||||
}
|
||||
func (store *MySQLStore) SCUMVehicles() SCUMVehicleRepository {
|
||||
return &persistentRepository[domain.SCUMVehicle, domain.SCUMVehicleFilter]{repository: store.MemoryStore.scumVehicles, persist: store.persist}
|
||||
}
|
||||
func (store *MySQLStore) SCUMVehicleTrajectories() SCUMVehicleTrajectoryRepository {
|
||||
return &persistentRepository[domain.SCUMVehicleTrajectory, domain.SCUMVehicleTrajectoryFilter]{repository: store.MemoryStore.scumVehicleTracks, persist: store.persist}
|
||||
}
|
||||
func (store *MySQLStore) SCUMVehicleLocks() SCUMVehicleLockRepository {
|
||||
return &persistentRepository[domain.SCUMVehicleLock, domain.SCUMVehicleLockFilter]{repository: store.MemoryStore.scumVehicleLocks, persist: store.persist}
|
||||
}
|
||||
func (store *MySQLStore) initialize() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
@@ -180,6 +168,9 @@ CREATE TABLE IF NOT EXISTS platform_metadata_snapshots (
|
||||
if err != nil {
|
||||
return fmt.Errorf("create mysql metadata snapshot table: %w", err)
|
||||
}
|
||||
if err := store.initializeSCUMTables(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -199,9 +190,63 @@ func (store *MySQLStore) load() error {
|
||||
return fmt.Errorf("decode mysql metadata snapshot: %w", err)
|
||||
}
|
||||
store.loadSnapshot(snapshot)
|
||||
if err := store.migrateSnapshotSCUM(ctx, snapshot); err != nil {
|
||||
return err
|
||||
}
|
||||
return store.loadRuntime()
|
||||
}
|
||||
|
||||
// migrateSnapshotSCUM copies SCUM rows that an older build kept inside the
|
||||
// metadata snapshot into the typed SCUM tables. SCUM is no longer stored in
|
||||
// the snapshot, so this runs at most once for a snapshot written before the
|
||||
// typed tables existed.
|
||||
func (store *MySQLStore) migrateSnapshotSCUM(ctx context.Context, snapshot StoreSnapshot) error {
|
||||
if len(snapshot.SCUMUsers)+len(snapshot.SCUMVehicles)+len(snapshot.SCUMUserTrajectories)+len(snapshot.SCUMVehicleTrajectories)+len(snapshot.SCUMVehicleLocks) == 0 {
|
||||
return nil
|
||||
}
|
||||
users := store.SCUMUsers()
|
||||
for _, value := range snapshot.SCUMUsers {
|
||||
if _, err := users.Get(value.ID); err == nil {
|
||||
continue
|
||||
} else if !errors.Is(err, ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
if err := users.Create(value); err != nil && !errors.Is(err, ErrDuplicate) {
|
||||
return fmt.Errorf("migrate scum_user %s: %w", value.ID, err)
|
||||
}
|
||||
}
|
||||
vehicles := store.SCUMVehicles()
|
||||
for _, value := range snapshot.SCUMVehicles {
|
||||
if _, err := vehicles.Get(value.ID); err == nil {
|
||||
continue
|
||||
} else if !errors.Is(err, ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
if err := vehicles.Create(value); err != nil && !errors.Is(err, ErrDuplicate) {
|
||||
return fmt.Errorf("migrate scum_vehicle %s: %w", value.ID, err)
|
||||
}
|
||||
}
|
||||
userTracks := store.SCUMUserTrajectories()
|
||||
for _, value := range snapshot.SCUMUserTrajectories {
|
||||
if err := userTracks.Create(value); err != nil && !errors.Is(err, ErrDuplicate) {
|
||||
return fmt.Errorf("migrate scum_user_trajectory %s: %w", value.ID, err)
|
||||
}
|
||||
}
|
||||
vehicleTracks := store.SCUMVehicleTrajectories()
|
||||
for _, value := range snapshot.SCUMVehicleTrajectories {
|
||||
if err := vehicleTracks.Create(value); err != nil && !errors.Is(err, ErrDuplicate) {
|
||||
return fmt.Errorf("migrate scum_vehicle_trajectory %s: %w", value.ID, err)
|
||||
}
|
||||
}
|
||||
locks := store.SCUMVehicleLocks()
|
||||
for _, value := range snapshot.SCUMVehicleLocks {
|
||||
if err := locks.Create(value); err != nil && !errors.Is(err, ErrDuplicate) {
|
||||
return fmt.Errorf("migrate scum_vehicle_lock %s: %w", value.ID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MySQLStore) loadRuntime() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
@@ -288,11 +333,12 @@ func (store *MySQLStore) snapshot() StoreSnapshot {
|
||||
GameClientBridgeSnapshots: snapshotRepository(store.MemoryStore.bridgeSnapshots.memoryRepository),
|
||||
GameClientBridgeStreams: snapshotRepository(store.MemoryStore.bridgeStreams),
|
||||
PluginDataRecords: snapshotRepository(store.MemoryStore.pluginDataRecords),
|
||||
SCUMUsers: snapshotRepository(store.MemoryStore.scumUsers),
|
||||
SCUMUserTrajectories: snapshotRepository(store.MemoryStore.scumUserTracks),
|
||||
SCUMVehicles: snapshotRepository(store.MemoryStore.scumVehicles),
|
||||
SCUMVehicleTrajectories: snapshotRepository(store.MemoryStore.scumVehicleTracks),
|
||||
SCUMVehicleLocks: snapshotRepository(store.MemoryStore.scumVehicleLocks),
|
||||
// SCUM rows live in the typed scum_* tables, never in the metadata snapshot.
|
||||
SCUMUsers: nil,
|
||||
SCUMUserTrajectories: nil,
|
||||
SCUMVehicles: nil,
|
||||
SCUMVehicleTrajectories: nil,
|
||||
SCUMVehicleLocks: nil,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user