Fix Share stats disappearing regression
This commit is contained in:
6 files changed
+266
-2
No files matched your search
@@ -205,3 +205,97 @@ func TestPowPairing_LowerDiffDoesNotReplace(t *testing.T) {
|
||||
t.Fatalf("bestPow = %+v, want the 9000-diff record kept", a.bestPow)
|
||||
}
|
||||
}
|
||||
|
||||
// Reproduces the startup race that destroyed accumulated share
|
||||
// statistics: the ingest goroutine runs concurrently with the restore,
|
||||
// and its first event triggers a persist. If that persist is allowed to
|
||||
// run before the counters are loaded, it writes the zero-valued
|
||||
// in-memory histogram over the stored history — which the restore then
|
||||
// reads back, making the loss permanent.
|
||||
func TestShareStats_NotPersistedBeforeLoad(t *testing.T) {
|
||||
s := openTempStore(t)
|
||||
// Seed the store as a long-running pool would: 4M accepted shares.
|
||||
const seeded = `{"b":[4000000,0,0,0,12,3],"n":4000015,"s":4.2e10}`
|
||||
if err := s.SetKV(kvDiffDistAll, seeded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// An aggregator that has NOT loaded yet, as during startup.
|
||||
a := New(nil, nil, time.Minute, discardLog())
|
||||
a.Store = s
|
||||
ingest(t, a, logmon.ShareEvent{SeenAt: time.Now(), Diff: 1234, Hash: "aa"})
|
||||
|
||||
v, err := s.GetKV(kvDiffDistAll)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v != seeded {
|
||||
t.Fatalf("history overwritten before load:\n got %s\nwant %s", v, seeded)
|
||||
}
|
||||
|
||||
// After loading, the accumulated history is in memory and further
|
||||
// shares extend it instead of replacing it.
|
||||
a.LoadPersisted()
|
||||
a.mu.RLock()
|
||||
count := a.alltimeDiffCount
|
||||
bucket0 := a.alltimeDiffDist[0]
|
||||
a.mu.RUnlock()
|
||||
if count != 4000015 || bucket0 != 4000000 {
|
||||
t.Fatalf("loaded count=%d bucket0=%d, want 4000015/4000000", count, bucket0)
|
||||
}
|
||||
|
||||
ingest(t, a, logmon.ShareEvent{SeenAt: time.Now(), Diff: 1234, Hash: "bb"})
|
||||
a.mu.RLock()
|
||||
after := a.alltimeDiffCount
|
||||
a.mu.RUnlock()
|
||||
if after != 4000016 {
|
||||
t.Errorf("alltime count = %d after one more share, want 4000016", after)
|
||||
}
|
||||
}
|
||||
|
||||
// Recovery path: rebuild the all-time histogram by rescanning the log
|
||||
// when the stored counters hold fewer shares than the log accounts for.
|
||||
func TestRebuildShareStatsFromLog(t *testing.T) {
|
||||
logPath := tempLogFile(t,
|
||||
`[2026-09-02 10:00:00.001] Accepted client 1 share diff 500/42/1.0K: aa`+"\n",
|
||||
`[2026-09-02 10:00:00.002] Accepted client 1 share diff 2000000/42/1.0K: bb`+"\n",
|
||||
`[2026-09-02 10:00:00.003] Accepted client 1 share diff 500000000000/42/1.0K: cc`+"\n",
|
||||
`[2026-09-02 10:00:00.004] Rejected client 1 dupe diff 500/42/1.0K: aa`+"\n",
|
||||
`[2026-09-02 10:00:00.005] Rejected client 1 invalid share Stale`+"\n",
|
||||
)
|
||||
s := openTempStore(t)
|
||||
a := New(nil, nil, time.Minute, discardLog())
|
||||
a.Store = s
|
||||
a.LogFilePath = logPath
|
||||
a.LoadPersisted()
|
||||
|
||||
adopted, scanned, err := a.RebuildShareStatsFromLog()
|
||||
if err != nil {
|
||||
t.Fatalf("rebuild: %v", err)
|
||||
}
|
||||
if !adopted || scanned != 3 {
|
||||
t.Fatalf("adopted=%v scanned=%d, want true/3", adopted, scanned)
|
||||
}
|
||||
|
||||
a.mu.RLock()
|
||||
dist := a.alltimeDiffDist
|
||||
count := a.alltimeDiffCount
|
||||
reasons := a.alltimeRejectReasons
|
||||
a.mu.RUnlock()
|
||||
// buckets: <1M, 1M-100M, 100M-1G, 1G-100G, 100G-1T, >=1T
|
||||
if dist[0] != 1 || dist[1] != 1 || dist[4] != 1 || count != 3 {
|
||||
t.Errorf("dist=%v count=%d, want one share each in buckets 0,1,4", dist, count)
|
||||
}
|
||||
if reasons["Duplicate"] != 1 || reasons["Stale"] != 1 {
|
||||
t.Errorf("reject reasons = %v, want one Duplicate and one Stale", reasons)
|
||||
}
|
||||
|
||||
// A rebuild that would shrink the history must be refused.
|
||||
a.mu.Lock()
|
||||
a.alltimeDiffCount = 4000000
|
||||
a.mu.Unlock()
|
||||
adopted, _, err = a.RebuildShareStatsFromLog()
|
||||
if err != nil || adopted {
|
||||
t.Errorf("adopted=%v err=%v, want refusal when the log holds fewer shares", adopted, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user