nsmcp::UnifiedAccessAnalytics method refreshLocked (protected)

 <instance of nsmcp::UnifiedAccessAnalytics[i]> refreshLocked \
    [ run ]

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

Parameters:
run (optional)

Testcases:
No testcase defined.
Source code:
accessIngestionProgress $run planning initialize-source
if {!${:sourceReady}} {
    :initializeSource
}
accessIngestionProgress $run planning retire-legacy
:retireLegacy
set start [clock milliseconds]
set now [clock seconds]
if {${:dynamicSources}} {
    # Other virtual servers can publish nslog traces after this module
    # initializes. Reconcile the source list on every scheduled pass.
    if {![catch {:refreshedAccessPaths} discoveredPaths]} {
        set :additionalPaths [lsort -unique [lsearch -all -inline -not -exact $discoveredPaths ${:path}]]
    }
}
set paths [list ${:path} {*}${:additionalPaths}]
nsv_set nsmcp:access-sources:[ns_info server] paths $paths
accessIngestionProgress $run planning open-store
set store [:accessRecordStoreOpen ${:settings} writer [list ::nsmcp::accessIngestionProgress $run planning]]
if {![$store available]} {
    $store close
    return {busy true}
}
try {
    accessIngestionProgress $run planning discover-live
    set control [$store stateGet continuous {}]
    catch {
        accessIngestionCheckpoint $control
    }
    set partition [$store partitionInfo]
    foreach key {partitionInterval partitionCount bootstrapFrom availableFrom historicalBackfillEnabled} {
        if {[dict exists $partition $key]} {
            dict set control $key [dict get $partition $key]
        }
    }
    set pressure [$store maintenancePressure]
    set oldLive [:dict getdef $control live {}]
    set floor [expr {max([$store stateGet retentionFloor 0],wide(floor(($now-[dict get ${:settings} retention])/60.0))*60)}]
    if {[:dict getdef ${:settings} currentDayOnly false]} {
        set floor [expr {max($floor,[::nsmcp::accessRecordDayFloor $now [dict get ${:settings} dayTimezone]])}]
    }
    if {[:dict getdef ${:settings} partitionInterval 0]>0
        && ![:dict getdef ${:settings} historicalBackfill false]} {
        set floor [expr {max($floor,[$store stateGet bootstrapFrom $floor])}]
    }
    set options [::nsmcp::AccessTrafficSelection options [dict create from [expr {max($floor,$now-300)}] to [expr {$now+1}] timezone UTC pathLiteral {} includeRotations false selection ordered mode summary maxGroups 20 ingestOnly true]]
    set livePaths $paths
    set first [lsearch -exact $paths [:dict getdef $control nextLivePath {}]]
    if {$first>0} {
        set livePaths [concat [lrange $paths $first end] [lrange $paths 0 $first-1]]
    }
    set discovered [accessScanDiscover $livePaths false [llength $paths]]
    set files [dict get $discovered files]
    set unavailable [dict get $discovered unavailableFiles]
    set live {}
    set earliest [:dict getdef $control recentFrom [expr {$now - 300}]]
    foreach file $files {
        accessIngestionProgress $run planning prepare-source
        set fid [$store prepare $file]
        if {[dict exists $oldLive $fid]} {
            dict set file offset [expr {min([dict get $oldLive $fid],[dict get $file size])}]
        } else {
            accessIngestionProgress $run planning seek-recent
            set planning {files {} index 0 examinedBytes 0 stats {}}
            accessOrderedPlan file planning $options [accessScanDefaultLimits] {}
            if {[dict get $file status] eq "outside-window"} {
                dict set file offset 0
            }
        }
        dict set file status running
        dict set file archiveFid $fid
        dict set file archiveLo [dict get $file offset]
        dict set file scanEnd [dict get $file size]
        dict set file scanStart [dict get $file offset]
        lappend live $file
    }
} finally {
    $store close
}
set limits [dict merge [accessScanDefaultLimits] [accessAggregateSettings] [dict create slice ${:ingestBytes} milliseconds ${:ingestMilliseconds}]]
set state [dict create files $live omittedFiles 0 unavailableFiles $unavailable ignoredCandidates 0 index 0 stats [::nsmcp::AccessScan empty] examinedBytes 0 parsedRecordBytes 0 reusedBytes 0 nativeSearch false recordStore ${:settings} limits $limits failedTrafficSelection [accessFailureSelection $options]]
dict set options scanState $state
set step [:accessRecordScan $paths [dict get ${:settings} directory] $options ::nsmcp::unifiedRedactor $limits {} ${:settings} [list ::nsmcp::accessIngestionProgress $run live]]
if {[llength $step]!=4} {
    error "continuous ingestion could not retain its checkpoint"
}
set state [lindex $step 3]
set live {}
set sourceBytes 0
set cursor 0
set caught [expr {$unavailable==0 ? "true" : "false"}]
set pending 0
# A busy first source can use the entire slice. Give the following
# source first access to the shared budget on the next tick.
set scanned [dict get $state files]
set next [dict get $state index]
if {$next<[llength $scanned]} {
    set current [lindex $scanned $next]
    if {[dict get $current offset]>[dict get $current archiveLo]} {
        incr next
    }
}
if {$next<[llength $scanned]} {
    dict set control nextLivePath [dict get [lindex $scanned $next] path]
} else {
    dict unset control nextLivePath
}
foreach file [dict get $state files] {
    if {[dict exists $file archivePending]} {
        incr pending
    }
    dict set live [dict get $file archiveFid] [dict get $file offset]
    incr sourceBytes [dict get $file size]
    incr cursor [dict get $file offset]
    if {[dict get $file offset]<[dict get $file size] && ![dict exists $file archivePending]} {
        set caught false
    }
}
dict set control live $live
dict set control observedSize $sourceBytes
dict set control cursor $cursor
dict set control recentFrom $earliest
dict set control updatedAt $now
dict set control caughtUp $caught
dict set control pending $pending
dict set control liveFiles [llength $scanned]
dict set control unavailableLiveFiles $unavailable
set backfill [:dict getdef $control backfill {}]
set previousPaths [:dict getdef $control sourcePaths [list ${:path}]]
if {![dict exists $control sourceCatchupVersion]} {
    # Existing stores may already list a secondary log while having
    # imported only its recent tail. Revisit current secondary files
    # once after upgrading this ingestion policy.
    set secondary [lsearch -all -inline -not -exact $paths ${:path}]
    if {[llength $secondary]} {
        dict set control newSourcePaths [lsort -unique [concat  [:dict getdef $control newSourcePaths {}] $secondary]]
        set backfill {}
        dict unset control backfill
        dict unset control backfillOptions
    }
    dict set control sourceCatchupVersion 1
}
if {$previousPaths ne $paths} {
    set backfill {}
    dict unset control backfill
    dict unset control backfillOptions
    dict set control recentFrom [expr {max($earliest,$now-300)}]
    set newPaths {}
    foreach sourcePath $paths {
        if {$sourcePath ni $previousPaths} {lappend newPaths $sourcePath}
    }
    if {[llength $newPaths]} {
        dict set control newSourcePaths [lsort -unique [concat  [:dict getdef $control newSourcePaths {}] $newPaths]]
    }
}
dict set control sourcePaths $paths
set finished [expr {$backfill ne {} && [dict exists $backfill ingestComplete]}]
set due [expr {!$finished || ![dict exists $control backfillAt] || $now-[dict get $control backfillAt]>=60}]
if {$finished && $due} {
    set backfill {}
}
set newSourcePaths [:dict getdef $control newSourcePaths {}]
set historyAllowed [expr {![dict get $pressure capacityPressure]
                          && (![dict get $pressure expired] || [llength $newSourcePaths]>0)}]
dict set control historyDeferred [expr {!$historyAllowed}]
dict set control maintenancePressure $pressure
if {$caught && $due && $historyAllowed && [clock milliseconds]-$start<${:ingestMilliseconds}} {
    set historicalPaths $paths
    set historicalFrom $floor
    set includeRotations true
    if {[llength $newSourcePaths]>0} {
        # Catch up a newly discovered live file even when old-data
        # pruning has temporarily deferred the general backfill.
        set historicalPaths $newSourcePaths
        set historicalFrom [expr {max($floor,$now-86400)}]
        set includeRotations false
    }
    set historical [::nsmcp::AccessTrafficSelection options [dict create from $historicalFrom to [expr {$now+1}] timezone UTC pathLiteral {} includeRotations $includeRotations selection ordered mode summary maxGroups 20 ingestOnly true]]
    if {$backfill ne {} && ![dict exists $backfill ingestComplete]} {
        # Freeze the previous backfill's window until that pass completes.
        set historical [dict get $control backfillOptions]
        set historicalPaths [:dict getdef $control backfillPaths $paths]
        if {[catch {
            accessUnifiedRestore $backfill
        } restored]} {
            set backfill {}
        } else {
            dict set historical scanState $restored
        }
    }
    set remaining [expr {max(10,${:ingestMilliseconds}-([clock milliseconds]-$start))}]
    set histLimits [dict replace $limits milliseconds $remaining]
    if {[dict exists $historical scanState]} {
        dict set historical scanState limits milliseconds $remaining
    }
    if {[catch {
        :accessRecordScan $historicalPaths [dict get ${:settings} directory] $historical ::nsmcp::unifiedRedactor $histLimits {} ${:settings} [list ::nsmcp::accessIngestionProgress $run history]
    } step]} {
        set backfill {}
        dict set control lastBackfillError [clock seconds]
    } elseif {[llength $step]==4} {
        set backfill [lindex $step 3]
        dict unset historical scanState
        dict set control backfillOptions $historical
        dict set control backfillPaths $historicalPaths
        dict set control backfill [accessUnifiedPack $backfill]
        dict set control backfillAt $now
        if {[dict exists $backfill ingestComplete]} {
            dict set control availableFrom [dict get $historical from]
            if {[llength $newSourcePaths]>0 && $historicalPaths eq $newSourcePaths} {
                dict unset control newSourcePaths
            }
        }
    }
}
set remaining 0
set unsupported 0
if {$backfill eq {}} {
    set remaining 1
} else {
    foreach file [dict get $backfill files] {
        if {[dict get $file compressed]} {
            incr unsupported
        }
        if {[dict get $file status] ni {complete outside-window unsupported-compressed}} {
            incr remaining [expr {max(0,[dict get $file size]-[dict get $file offset])}]
        }
    }
}
dict set control historyUnreadBytes $remaining
dict set control unsupportedCompressed $unsupported
accessIngestionProgress $run finalizing open-store
set store [:accessRecordStoreOpen ${:settings} writer [list ::nsmcp::accessIngestionProgress $run finalizing]]
if {![$store available]} {
    $store close
    return {busy true}
}
try {
    set partition [$store partitionInfo]
    foreach key {partitionInterval partitionCount bootstrapFrom historicalBackfillEnabled} {
        if {[dict exists $partition $key]} {
            dict set control $key [dict get $partition $key]
        }
    }
    accessIngestionProgress $run finalizing save-control
    $store statePut continuous $control
    catch {
        accessIngestionCheckpoint $control
    }
    accessIngestionProgress $run finalizing rollup-backfill
    if {$historyAllowed && [clock milliseconds]-$start<${:ingestMilliseconds}} {
        $store rollupBackfill 1
    }
    # A few old minutes per refresh; never put a whole-day repair on
    # the startup or ingestion critical path.
    if {$caught} {
        if {[catch {$store slowMinuteBackfill  [expr {[clock milliseconds]-$start<${:ingestMilliseconds} ? 3 : 1}]} message]} {
            set key nsmcp:slow-minute-repair:[ns_info server]
            if {![nsv_exists $key warnedAt] || [clock seconds]-[nsv_get $key warnedAt]>60} {
                ns_log warning "nsmcp: slow-minute rollup repair failed: $message"
                nsv_set $key warnedAt [clock seconds]
            }
        }
    }
} finally {
    $store close
}
return [dict create caughtUp $caught historyUnreadBytes $remaining parsedBytes [dict get $state parsedRecordBytes] wallMs [expr {[clock milliseconds]-$start}]]
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: