nsmcp::AccessRecordPartitions method partitionPrune (protected)

 <instance of nsmcp::AccessRecordPartitions[i]> partitionPrune \
    batchRows

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

Parameters:
batchRows (required)

Testcases:
No testcase defined.
Source code:
if {![string is integer -strict $batchRows] || $batchRows<1 || $batchRows>10000} {
    error "retention batch rows must be 1..10000"
}
if {[${:db} inTransaction]} {
    error "retention requires an idle database session"
}
set cutoff [:retentionFloor]
set peerCutoff [expr {[clock seconds]-[dict get ${:settings} peerRetention]}]
set now [clock seconds]
set counts {requests 0 diagnostics 0 minutes 0 peers 0}
:accessIngestionObserve prepare-retention
${:db} execute {CREATE TEMP TABLE nsmcp_retention_candidates(id INTEGER PRIMARY KEY,n INTEGER)}
try {
    :accessIngestionObserve begin-transaction
    ${:db} transaction {
        :statePut retentionFloor $cutoff
        :accessIngestionObserve partition-selection
        set parts [${:db} execute {SELECT * FROM main.partitions WHERE stop<=:cutoff ORDER BY start LIMIT 1}]
        if {$parts ne {}} {
            set part [lindex $parts 0]
            set name [dict get $part name]
            if {![regexp {^p_[0-9]+$} $name]} {
                error "record-store-invalid-partition"
            }
            :accessIngestionObserve partition-drop
            ${:db} execute {DROP VIEW main.rows}
            ${:db} execute "DROP TABLE main.$name"
            ${:db} execute {DELETE FROM main.partitions WHERE name=:name}
            :partitionView
            dict set counts requests [dict get $part n]
            set first [dict get $part first]
            set last [dict get $part last]
            set n [dict get $part n]
            ${:db} execute {
                INSERT INTO main.evictions(dropped_at,first_time,last_time,rows,reason)
                VALUES(:now,:first,:last,:n,'partition');
                DELETE FROM main.evictions WHERE id NOT IN(
                    SELECT id FROM main.evictions ORDER BY id DESC LIMIT 64)
            }
        }
        :accessIngestionObserve diagnostic-deletion
        # Loss metadata is small and independent of request partitions.
        ${:db} execute {
            INSERT INTO temp.nsmcp_retention_candidates
            SELECT rowid,malformed+oversized+skipped FROM main.scan_counts
             WHERE completed<:cutoff ORDER BY completed LIMIT :batchRows
        }
        dict set counts diagnostics [${:db} scalar {SELECT coalesce(sum(n),0) FROM temp.nsmcp_retention_candidates}]
        ${:db} execute {DELETE FROM main.scan_counts WHERE rowid IN(SELECT id FROM temp.nsmcp_retention_candidates)}
        :accessIngestionObserve minute-deletion
        ${:db} execute {DELETE FROM main.minutes WHERE rowid IN(
            SELECT rowid FROM main.minutes WHERE slot<:cutoff ORDER BY slot LIMIT :batchRows)}
        dict set counts minutes [${:db} changes]
        :accessIngestionObserve peer-deletion
        ${:db} execute {DELETE FROM main.peer_addresses WHERE rowid IN(
            SELECT rowid FROM main.peer_addresses WHERE seen<:peerCutoff ORDER BY seen LIMIT :batchRows)}
        dict set counts peers [${:db} changes]
        :statePut droppedThrough [expr {$cutoff-1}]
        :statePut lastEvictionAt $now
        :accessIngestionObserve commit
    }
    :accessIngestionObserve committed
} finally {
    ${:db} execute {DROP TABLE temp.nsmcp_retention_candidates}
}
return $counts
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: