nsmcp::AccessRecordStoreSQLite method slowMinuteBackfill (public)

 <instance of nsmcp::AccessRecordStoreSQLite[i]> slowMinuteBackfill \
    [ maxSlots ] [ from ] [ to ]

Defined in /usr/local/ns/tcl/nsmcp/log-traffic-record-store.tcl

Repair old rollups from retained request rows. Stage a bounded set of source-minutes and aggregate them in one indexed query; per-minute SELECT/UPDATE/COMMIT cycles become very costly on a busy disk. Start with the newest minutes for incremental background repair.

Parameters:
maxSlots (optional, defaults to "5")
from (optional)
to (optional)

Testcases:
No testcase defined.
Source code:
set cutoff [expr {[clock seconds]-[dict get ${:settings} retention]}]
if {$from ne {}} {set cutoff [expr {max($cutoff,$from)}]}
if {$to eq {}} {set to [expr {[clock seconds]+60}]}
${:db} execute {
    CREATE TEMP TABLE IF NOT EXISTS nsmcp_slow_repair_slots(
        fid INTEGER NOT NULL, slot INTEGER NOT NULL, PRIMARY KEY(fid,slot)
    ) WITHOUT ROWID
}
${:db} execute {
    CREATE TEMP TABLE IF NOT EXISTS nsmcp_slow_repair_aggregate(
        fid INTEGER NOT NULL, slot INTEGER NOT NULL, status INTEGER NOT NULL,
        pool TEXT NOT NULL, method TEXT NOT NULL, source TEXT NOT NULL,
        n INTEGER NOT NULL, timed INTEGER NOT NULL,
        slow1 INTEGER NOT NULL, slow2 INTEGER NOT NULL,
        PRIMARY KEY(fid,slot,status,pool,method,source)
    ) WITHOUT ROWID
}
${:db} execute {DELETE FROM temp.nsmcp_slow_repair_slots}
${:db} execute {DELETE FROM temp.nsmcp_slow_repair_aggregate}
${:db} execute {
    INSERT INTO temp.nsmcp_slow_repair_slots
    SELECT DISTINCT fid,slot FROM main.minutes
    WHERE slot>=:cutoff AND slot<:to AND slowValid IN (0,1)
    ORDER BY slot DESC LIMIT :maxSlots
}
set count [${:db} scalar {SELECT count(*) FROM temp.nsmcp_slow_repair_slots}]
if {$count>0} {
    # CROSS JOIN keeps the small slot table outermost, using by_time
    # for each source-minute instead of walking all retained rows.
    ${:db} execute {
        INSERT INTO temp.nsmcp_slow_repair_aggregate
        SELECT r.fid,s.slot,r.status,r.pool,r.method,r.source,
               count(*),sum(r.phase=1),
               sum(r.phase=1 AND r.q+r.f+r.r>=1.0),
               sum(r.phase=1 AND r.q+r.f+r.r>=2.0)
        FROM temp.nsmcp_slow_repair_slots AS s
        CROSS JOIN main.rows AS r INDEXED BY by_time
        WHERE r.t>=s.slot AND r.t<s.slot+60 AND r.fid=s.fid AND r.kind=0
        GROUP BY r.fid,s.slot,r.status,r.pool,r.method,r.source
    }
    ${:db} transaction {
        # Match the existing rollup keys; old rows with lost source
        # detail stay marked incomplete rather than becoming zeros.
        ${:db} execute {
            UPDATE main.minutes AS m SET slowValid=-1
            WHERE m.slot>=:cutoff AND m.slot<:to AND m.slowValid IN (0,1) AND EXISTS (
                SELECT 1 FROM temp.nsmcp_slow_repair_slots AS s
                WHERE s.fid=m.fid AND s.slot=m.slot)
        }
        ${:db} execute {
            UPDATE main.minutes AS m
            SET timed=a.timed,slow1=a.slow1,slow2=a.slow2,slowValid=2
            FROM temp.nsmcp_slow_repair_aggregate AS a
            WHERE m.fid=a.fid AND m.slot=a.slot AND m.status=a.status
              AND m.pool=a.pool AND m.method=a.method AND m.source=a.source
              AND m.n=a.n AND m.slowValid=-1
        }
    }
}
return $count
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: