Files
Pulse/internal/monitoring/cache_cleanup_progress_test.go
pulse-triage[bot] d3c0b00b02 Fix monitoring test logger data race and rebalance CI backend shards
1. Eliminate the recurring main Build and Test Backend tests (rest-1)
   DATA RACE observed in runs 46191313678 (job 141127584174) and
   46197384546 (job 141144000959), where
   TestPollPBSConfigLoggingDoesNotBlockConfigEditOrPeerPoll overwrote
   the unsynchronized global zerolog/log.Logger struct while background
   alerts.NewManager().periodicSaveAlerts() goroutines leaked by earlier
   tests called log.Debug():
   - Install a single atomic io.Writer router (monitoringTestLogSink) on
     log.Logger once in internal/monitoring/main_test.go TestMain and
     route all 10 internal/monitoring/*_test.go log-capture and
     log-suppression sites through setPackageTestLogOutput /
     disablePackageTestLogs (atomic sink swap + atomic zerolog global
     level) and syncTestLogBuffer, plus a -race regression test.
   - Add t.Cleanup(m.alertManager.Stop) to all six
     TestMaybePollPhysicalDisksAsync_* tests in
     monitor_pve_disk_alert_settle_test.go and
     monitor_pve_disk_fallback_test.go.

2. Rebalance .github/scripts/go-package-test-seconds.txt from ten
   complete post-#5428 hosted Build and Test runs (seven 3-way PR runs
   and three 2-way main push runs, runner-speed normalized) to address
   PR checks (Pulse) — the #1 slowest stage in output-constraints.json
   (19.1m median / 44.8m p90):
   - Set REST_SHARD_FIXED_SECONDS 165.028 0.000 and
     REST_SHARD_FIXED_SECONDS_3 32.619 236.856 0.000 (replacing the
     inverted pre-#5428 309.230 / 191.908 / 0.000 offsets).
   - Leave-one-out holdout cross-validation cuts runner-normalized peak
     shard task-seconds on 7/7 3-way holdouts (-86.8s to -214.8s) and
     3/3 2-way holdouts (-58.9s to -117.6s).

3. Add disposable per-suite NODE_COMPILE_CACHE bytecode caching to
   frontend-modern/scripts/__tests__/check-bundle-size.test.mjs and
   build-embed-assets.test.mjs, matching sync-embed-dist.test.mjs,
   to avoid recompiling jsdom across 107 test child processes.

Contract-Neutral: Test-harness logger synchronization, CI shard scheduling weights, and test-only Node bytecode caching; no production code, runtime behavior, or subsystem contracts changed.
Change-source: pulse-maintainer
2026-11-28 23:28:12 +00:00

140 lines
4.5 KiB
Go

package monitoring
import (
"bytes"
"testing"
"time"
"github.com/rs/zerolog"
)
type blockedCacheExpiryLog struct {
message string
entered chan struct{}
resume chan struct{}
}
func (w blockedCacheExpiryLog) Write(p []byte) (int, error) {
if bytes.Contains(p, []byte(w.message)) {
close(w.entered)
<-w.resume
}
return len(p), nil
}
func TestCacheExpiryLoggingDoesNotHoldPollCacheLocks(t *testing.T) {
now := time.Now()
stale := now.Add(-2 * time.Hour)
const deadKey = "expired"
const instance, node, vmid = "live", "node", 105
guestKey := guestMetadataCacheKey(instance, node, vmid)
for _, kind := range []string{"metadata", "limiter", "node-snapshot", "guest-snapshot", "rrd"} {
t.Run(kind, func(t *testing.T) {
var message string
var sweep func(time.Time)
var checkLive func()
m := &Monitor{}
switch kind {
case "metadata", "limiter":
sweep = m.cleanupGuestMetadataCache
m.guestMetadataCache = map[string]guestMetadataCacheEntry{guestKey: metadataObservationFixture(now)}
if kind == "metadata" {
m.guestMetadataCache[deadKey] = metadataObservationFixture(stale)
message = "Cleaned up stale guest metadata cache entry"
} else {
m.guestMetadataLimiter = map[string]time.Time{deadKey: stale}
message = "Cleaned up stale guest metadata limiter entry"
}
checkLive = func() {
if !m.guestMetadataMu.TryRLock() {
t.Error("expiry logging held the guest metadata cache lock")
} else {
_, retained := m.guestMetadataCache[deadKey]
m.guestMetadataMu.RUnlock()
if retained {
t.Error("expired metadata was still present when its removal was logged")
}
if !m.hasRecentGuestMetadataEvidence(instance, node, vmid, now) {
t.Error("live guest lost its metadata observation")
}
}
if !m.guestMetadataLimiterMu.TryLock() {
t.Fatal("expiry logging held the guest metadata limiter lock")
}
_, retained := m.guestMetadataLimiter[deadKey]
m.guestMetadataLimiterMu.Unlock()
if retained {
t.Error("expired limiter was still present when its removal was logged")
}
if !m.tryReserveGuestMetadataFetch(guestKey, now) {
t.Fatal("live guest could not reserve its next read")
}
m.releaseGuestMetadataFetch(guestKey)
}
case "node-snapshot", "guest-snapshot":
sweep = m.cleanupDiagnosticSnapshots
m.guestSnapshots = map[string]GuestMemorySnapshot{guestKey: {Instance: instance, RetrievedAt: now}}
if kind == "node-snapshot" {
m.nodeSnapshots = map[string]NodeMemorySnapshot{deadKey: {RetrievedAt: stale}}
message = "Cleaned up stale node snapshot"
} else {
m.guestSnapshots[deadKey] = GuestMemorySnapshot{RetrievedAt: stale}
message = "Cleaned up stale guest snapshot"
}
checkLive = func() {
if !m.diagMu.TryRLock() {
t.Fatal("expiry logging held the diagnostic snapshots lock")
}
m.diagMu.RUnlock()
got := m.GetDiagnosticSnapshots()
if len(got.Nodes) != 0 || len(got.Guests) != 1 || got.Guests[0].Instance != instance || !got.Guests[0].RetrievedAt.Equal(now) {
t.Fatalf("diagnostic read did not preserve the live guest alone: %+v", got)
}
}
case "rrd":
sweep = m.cleanupRRDCache
message = "Cleaned up stale RRD cache entry"
m.nodeRRDMemCache = map[string]rrdMemCacheEntry{deadKey: {fetchedAt: stale}}
m.guestAgentFSInfoCacheTTL = vmFSInfoCacheTTL
m.storeVMFSInfo(instance, node, vmid, 100, nil, now)
checkLive = func() {
if !m.rrdCacheMu.TryLock() {
t.Fatal("expiry logging held the guest memory and filesystem cache lock")
}
_, retained := m.nodeRRDMemCache[deadKey]
m.rrdCacheMu.Unlock()
if retained {
t.Error("expired RRD sample was still present when its removal was logged")
}
if got, ok := m.cachedVMFSInfo(instance, node, vmid, 101, now); !ok || !got.fetchedAt.Equal(now) {
t.Fatal("poll lost its independent filesystem observation or changed its age")
}
}
}
// No parallel execution: the package log sink is shared.
sink := blockedCacheExpiryLog{message, make(chan struct{}), make(chan struct{})}
restoreLogs := setPackageTestLogOutput(t, sink, zerolog.DebugLevel)
done := make(chan struct{})
go func() {
defer close(done)
sweep(now)
}()
defer func() {
close(sink.resume)
<-done
restoreLogs()
}()
select {
case <-sink.entered:
checkLive()
case <-done:
t.Fatal("sweep did not emit its expiry diagnostic")
case <-time.After(5 * time.Second):
t.Fatal("sweep did not reach the gated logger")
}
})
}
}