Fix Share stats disappearing regression

This commit is contained in:
satoshi committed 2026-09-02 00:42:32 +03:00
1 parent 4e40e08fd4
commit 89e9b32efd
6 files changed
+266 -2

No files matched your search

+87 -2
View File
@@ -7,6 +7,7 @@ package state
import (
"context"
"encoding/json"
"errors"
"log/slog"
"strconv"
"sync"
@@ -371,6 +372,17 @@ type Aggregator struct {
sessionDiffSum float64 // sum of share difficulties (session)
alltimeDiffSum float64 // sum of share difficulties (alltime)
lastShareStatsSave time.Time
// loadOnce guards LoadPersisted so main can complete the restore
// before starting the ingest goroutines, while Run stays safe to
// call on its own.
loadOnce sync.Once
// statsLoaded reports that the alltime share statistics have been
// read back from the store. Persisting before that would write the
// zero-valued in-memory counters over the accumulated history — the
// first share event triggers a save, so this is the difference
// between a restart and permanent data loss.
statsLoaded bool
}
func New(ck *ckpool.Client, rpc *bitcoind.RPC, interval time.Duration, log *slog.Logger) *Aggregator {
@@ -387,6 +399,19 @@ func New(ck *ckpool.Client, rpc *bitcoind.RPC, interval time.Duration, log *slog
}
}
// LoadPersisted restores block history and counters from the store.
// Run calls it, but callers should invoke it directly before starting
// any ingest goroutine: those goroutines mutate the same counters and
// persist them on their first event, so a restore still in flight would
// be overwritten by — and then read back as — near-empty state. Safe to
// call repeatedly; only the first call does the work.
func (a *Aggregator) LoadPersisted() {
a.loadOnce.Do(func() {
a.loadPersistedBlocks()
a.loadPersistedState()
})
}
// Ready returns a channel that's closed once the aggregator has
// completed its first refresh — used by main to delay HTTP serving
// until /api/snapshot reflects real state instead of zeros.
@@ -407,8 +432,7 @@ func (a *Aggregator) Run(ctx context.Context, tipEvents <-chan zmqmon.TipEvent)
a.mu.Lock()
a.zmqEnabled = tipEvents != nil
a.mu.Unlock()
a.loadPersistedBlocks()
a.loadPersistedState()
a.LoadPersisted()
a.refresh(ctx)
t := time.NewTicker(a.Interval)
defer t.Stop()
@@ -926,6 +950,50 @@ func (a *Aggregator) IngestShareEvents(ctx context.Context, events <-chan logmon
}
}
// RebuildShareStatsFromLog recounts the all-time share statistics by
// scanning the whole ckpool log, and adopts the result when it accounts
// for more shares than the counters currently hold. Recovery for stored
// stats that were lost or truncated: the log is the only other record of
// per-share difficulty, and ckpool keeps its full history there.
//
// Deliberately never automatic — the scan walks every line of a log that
// can hold millions, and it was a long startup scan that made the race
// this repairs so easy to hit in the first place.
func (a *Aggregator) RebuildShareStatsFromLog() (adopted bool, scanned int64, err error) {
if a.LogFilePath == "" {
return false, 0, errors.New("no ckpool log path configured")
}
st, err := logmon.ScanShareStats(a.LogFilePath)
if err != nil {
return false, 0, err
}
var dist [6]int64
for _, d := range st.Diffs {
dist[diffBucket(d)]++
}
a.mu.Lock()
current := a.alltimeDiffCount
if st.Accepted <= current {
a.mu.Unlock()
a.Log.Info("share stats rebuild: log has no more shares than stored, keeping stored",
"log_shares", st.Accepted, "stored_shares", current)
return false, st.Accepted, nil
}
a.alltimeDiffDist = dist
a.alltimeDiffCount = st.Accepted
a.alltimeDiffSum = st.DiffSum
a.alltimeRejectReasons = st.RejectReasons
a.statsLoaded = true
a.mu.Unlock()
a.persistShareStats()
a.Log.Warn("share stats rebuilt from ckpool log",
"shares", st.Accepted, "was", current)
return true, st.Accepted, nil
}
// ResetSessionShareStats clears session-level share statistics. Called
// when a ckpool restart is detected (pool.Shares decreases).
func (a *Aggregator) resetSessionShareStats() {
@@ -939,6 +1007,15 @@ func (a *Aggregator) persistShareStats() {
if a.Store == nil {
return
}
a.mu.RLock()
loaded := a.statsLoaded
a.mu.RUnlock()
if !loaded {
// Belt and braces alongside the load-before-ingest ordering in
// main: writing here would replace the accumulated history with
// whatever few shares have arrived since startup.
return
}
a.mu.Lock()
reasonsCopy := make(map[string]int64, len(a.alltimeRejectReasons))
for k, v := range a.alltimeRejectReasons {
@@ -1165,6 +1242,14 @@ func (a *Aggregator) loadPersistedState() {
}
}
// From here the alltime counters reflect the store, so persisting
// them can no longer erase history.
a.mu.Lock()
a.statsLoaded = true
loadedShares := a.alltimeDiffCount
a.mu.Unlock()
a.Log.Info("share stats loaded", "alltime_shares", loadedShares)
cutoff := time.Now().Add(-24 * time.Hour).Unix()
if samples, err := a.Store.HashrateSince(cutoff); err != nil {
a.Log.Warn("hashrate history load failed", "err", err)