tsdb: improve initTime busy-wait loop

- Add runtime.Gosched() to yield CPU during the busy-wait loop
- Reduce timeout from 500ms to 100ms (actual wait should be microseconds)
- Add warning log when timeout occurs to aid debugging

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2026-02-03 15:32:34 +01:00
parent 02c68154bc
commit 5a15925bc1

View file

@ -19,6 +19,7 @@ import (
"fmt"
"log/slog"
"math"
"runtime"
"time"
"github.com/prometheus/prometheus/model/exemplar"
@ -120,12 +121,18 @@ func (h *Head) initTime(t int64) {
if !h.minTime.CompareAndSwap(math.MaxInt64, t) {
// Concurrent appends that are initializing.
// Wait until h.maxTime is swapped to avoid minTime/maxTime races.
antiDeadlockTimeout := time.After(500 * time.Millisecond)
// This should complete in microseconds under normal operation.
antiDeadlockTimeout := time.After(100 * time.Millisecond)
for h.maxTime.Load() == math.MinInt64 {
select {
case <-antiDeadlockTimeout:
// This should never happen in normal operation.
// If it does, there may be a bug or the system is severely overloaded.
h.logger.Warn("initTime timeout waiting for maxTime initialization")
return
default:
// Yield to allow the initializing goroutine to complete.
runtime.Gosched()
}
}
return