diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eee968526..b8bfc916d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -211,6 +211,13 @@ jobs: CELERIS_MONO_SKEW=$skew go test -count=1 -timeout=120s -tags celeris_monoskew -run '^(TestArmHeaderDeadlineAt|TestHeaderDeadline_ClearArmCycle)$' ./internal/conn/ CELERIS_MONO_SKEW=$skew go test -count=1 -timeout=120s -tags celeris_monoskew -run '^TestDetachSnapshotElapsedFromStartTimeNs889$' . done + # Step 5 (celeris#1178): the HTTP/2 deadlines and waits, whole packages: conn + # (the H2 sweep against the stream stamps) and the stream code itself. With + # steps 2 to 5 the whole stamp component is routed, and both pass skewed. + for skew in 3600s -3600s; do + echo "== conn and h2 stream, whole, CELERIS_MONO_SKEW=$skew" + CELERIS_MONO_SKEW=$skew go test -count=1 -timeout=300s -tags celeris_monoskew ./internal/conn/ ./internal/protocol/h2/stream/ + done # Excludes ./test/... to keep this job deterministic — those # suites (integration, spec, sustainedload) are timing-sensitive # on shared GitHub Actions runners and run locally / in the diff --git a/internal/conn/h2_sweep_889_test.go b/internal/conn/h2_sweep_889_test.go index 14669b00e..8dcb8898e 100644 --- a/internal/conn/h2_sweep_889_test.go +++ b/internal/conn/h2_sweep_889_test.go @@ -10,6 +10,7 @@ import ( "golang.org/x/net/http2" "golang.org/x/net/http2/hpack" + "github.com/goceleris/celeris/internal/mono" "github.com/goceleris/celeris/internal/protocol/h2/stream" ) @@ -103,7 +104,7 @@ func TestSweepExpiresARequestAnswers408AndResets889(t *testing.T) { r.frames(func(fr *http2.Framer) { _ = fr.WriteHeaders(http2.HeadersFrameParam{StreamID: 1, BlockFragment: r.block("POST", "/u"), EndHeaders: true}) }) - now := time.Now().UnixNano() + now := mono.Now() if res := r.st.Sweep(now, r.write); res.Wrote || res.Expired != 0 { t.Fatalf("a request inside its deadline was touched: %+v", res) } @@ -157,7 +158,7 @@ func TestSweepHeaderBlockDeadlineEndsTheConn889(t *testing.T) { r.frames(func(fr *http2.Framer) { _ = fr.WriteHeaders(http2.HeadersFrameParam{StreamID: 1, BlockFragment: blk[:2], EndStream: true, EndHeaders: false}) }) - now := time.Now().UnixNano() + now := mono.Now() if res := r.st.Sweep(now, r.write); res.CloseAfterFlush || res.Wrote { t.Fatalf("inside the deadline: %+v", res) } @@ -190,7 +191,7 @@ func TestSweepHeaderBlockDeadlineEndsTheConn889(t *testing.T) { func TestSweepIdleClockIgnoresPings889(t *testing.T) { r := newSweepRig889(t, H2Config{IdleTimeout: time.Second}) r.written() - t0 := time.Now().UnixNano() + t0 := mono.Now() if res := r.st.Sweep(t0, r.write); res.CloseNow { t.Fatalf("the first sweep closed the conn: %+v", res) } @@ -227,7 +228,7 @@ func TestSweepTakesNoLockWhenNothingIsDue889(t *testing.T) { r.frames(func(fr *http2.Framer) { _ = fr.WriteHeaders(http2.HeadersFrameParam{StreamID: 1, BlockFragment: r.block("POST", "/u"), EndHeaders: true}) }) - now := time.Now().UnixNano() + now := mono.Now() r.st.mu.Lock() done := make(chan SweepResult) go func() { done <- r.st.Sweep(now, r.write) }() @@ -261,7 +262,7 @@ func TestSweepTakesNoLockWhenNothingIsDue889(t *testing.T) { func TestSweepExtendIdleAndGoAwayNoWait889(t *testing.T) { r := newSweepRig889(t, H2Config{IdleTimeout: time.Second}) r.written() - t0 := time.Now().UnixNano() + t0 := mono.Now() r.st.Sweep(t0, r.write) at := t0 + int64(1500*time.Millisecond) if res := r.st.Sweep(at, r.write); !res.CloseNow { @@ -317,7 +318,7 @@ func TestSweepAnswersABurstOfExpiriesInOneSweep889(t *testing.T) { _ = fr.WriteHeaders(http2.HeadersFrameParam{StreamID: uint32(1 + 2*i), BlockFragment: r.block("POST", "/u"), EndHeaders: true}) } }) - now := time.Now().UnixNano() + int64(2*time.Second) + now := mono.Now() + int64(2*time.Second) total := 0 for total < n && sweeps < 20 { e := r.st.Sweep(now+int64(sweeps), r.write).Expired @@ -352,7 +353,7 @@ func TestSweepExtendIdleNeedsProgress889(t *testing.T) { newConn := func() (*sweepRig889, int64) { r := newSweepRig889(t, H2Config{IdleTimeout: time.Second}) r.written() - now := time.Now().UnixNano() + now := mono.Now() r.st.Sweep(now, r.write) return r, now } @@ -487,7 +488,7 @@ func TestSweepFramesOnAResetStreamAreNotActivity889(t *testing.T) { t.Run("reset-for-another-cause", func(t *testing.T) { r := newSweepRig889(t, H2Config{IdleTimeout: time.Second}) r.written() - now := time.Now().UnixNano() + now := mono.Now() r.st.Sweep(now, r.write) r.frames(func(fr *http2.Framer) { noPath := []byte{0x82, 0x86, 0x41, 0x01, 'x'} @@ -506,7 +507,7 @@ func TestSweepFramesOnAResetStreamAreNotActivity889(t *testing.T) { t.Run("expired-request", func(t *testing.T) { r := newSweepRig889(t, H2Config{IdleTimeout: time.Second, ReadTimeout: time.Second}) r.written() - now := time.Now().UnixNano() + now := mono.Now() r.frames(func(fr *http2.Framer) { _ = fr.WriteHeaders(http2.HeadersFrameParam{StreamID: 1, BlockFragment: r.block("POST", "/u"), EndHeaders: true}) }) @@ -562,7 +563,7 @@ func TestSweepResetsAStalledStreamWithInternalError889(t *testing.T) { return s } a, b := mk(1), mk(3) - now := time.Now().UnixNano() + now := mono.Now() if res := r.st.Sweep(now+int64(500*time.Millisecond), r.write); res.Expired != 0 { t.Fatalf("expired half a window in: %+v", res) } diff --git a/internal/mono/mono.go b/internal/mono/mono.go index 17ec1497a..3b46f546c 100644 --- a/internal/mono/mono.go +++ b/internal/mono/mono.go @@ -214,6 +214,12 @@ func Time(ns int64) time.Time { return t.Add(-time.Duration(curAt(t) - ns)) } +// Until is how long it is, on Now's clock, until the stamp ns: ns - Now(), as a +// duration (negative when it has passed). The waiters' timers are made of it. It +// replaces time.Until(time.Unix(0, ns)), which reads the WALL clock against a +// stamp of this one: the two differ by every step since the start of the process. +func Until(ns int64) time.Duration { return time.Duration(ns - Now()) } + // FromTime is the stamp of an instant t that the caller already has (a // time.Now() it took for another reason, as handler.go does for a request // that no engine stamped): no second clock read. t should carry a monotonic diff --git a/internal/mono/mono_test.go b/internal/mono/mono_test.go index 3da8bcbb3..a4178cd76 100644 --- a/internal/mono/mono_test.go +++ b/internal/mono/mono_test.go @@ -282,3 +282,19 @@ func TestZeroAllocations(t *testing.T) { } } } + +func TestUntil(t *testing.T) { + for _, d := range []time.Duration{10 * time.Second, -10 * time.Second, 0} { + if got := Until(Now() + int64(d)); got < d-time.Second || got > d+time.Second { + t.Errorf("Until(Now()%+v) = %v", d, got) + } + } + // Under a stepped seam the answer follows the clock, not the real wall. + restore := SetSeam(func() Reading { + return Reading{Wall: time.Now().UnixNano() + int64(75*time.Second), Mono: int64(time.Since(start))} + }) + defer restore() + if got := Until(Now() + int64(5*time.Second)); got < 4*time.Second || got > 6*time.Second { + t.Errorf("under a stepped seam Until(Now()+5s) = %v", got) + } +} diff --git a/internal/mono/scan_tables_test.go b/internal/mono/scan_tables_test.go index d336a6672..a1e9ccbae 100644 --- a/internal/mono/scan_tables_test.go +++ b/internal/mono/scan_tables_test.go @@ -25,40 +25,10 @@ var stays = map[site]entry{ var pending = map[site]entry{ // step 4: route internal/conn H1 and the root package (handler.go, context_response.go; context.go StartTime keeps time.Unix(0, ns) until the flip PR, which makes it mono.Time and deletes its entry) {"context.go", "Context.StartTime", "time.Unix"}: {1, "step 4"}, - // step 5: route the HTTP/2 stream code (internal/protocol/h2/stream) - {"internal/protocol/h2/stream/deadline.go", "Processor.armHeaderBlock", ".UnixNano"}: {1, "step 5"}, - {"internal/protocol/h2/stream/processor.go", "Processor.admitHeaders", ".UnixNano"}: {1, "step 5"}, - {"internal/protocol/h2/stream/write_deadline.go", "Manager.noteBuffered", ".UnixNano"}: {1, "step 5"}, - {"internal/protocol/h2/stream/write_wait.go", "Manager.beginWait", ".UnixNano"}: {2, "step 5"}, - {"internal/protocol/h2/stream/write_wait.go", "Manager.judgeWait", ".UnixNano"}: {1, "step 5"}, - {"internal/protocol/h2/stream/write_wait.go", "Stream.awaitSendWindowRate", "time.Unix"}: {2, "step 5"}, } // pendingFixtures: test functions that write a wall reading into a stamp field or // hand one to a function that takes a stamp. Each moves to mono.Now() with the // routing PR of its package (the skew canary fails them otherwise), and its entry // goes with it. -var pendingFixtures = map[string]bool{ - // step 5 - "internal/conn/h2_sweep_889_test.go | TestSweepAnswersABurstOfExpiriesInOneSweep889": true, - "internal/conn/h2_sweep_889_test.go | TestSweepExpiresARequestAnswers408AndResets889": true, - "internal/conn/h2_sweep_889_test.go | TestSweepExtendIdleAndGoAwayNoWait889": true, - "internal/conn/h2_sweep_889_test.go | TestSweepExtendIdleNeedsProgress889": true, - "internal/conn/h2_sweep_889_test.go | TestSweepFramesOnAResetStreamAreNotActivity889": true, - "internal/conn/h2_sweep_889_test.go | TestSweepHeaderBlockDeadlineEndsTheConn889": true, - "internal/conn/h2_sweep_889_test.go | TestSweepIdleClockIgnoresPings889": true, - "internal/conn/h2_sweep_889_test.go | TestSweepResetsAStalledStreamWithInternalError889": true, - "internal/conn/h2_sweep_889_test.go | TestSweepTakesNoLockWhenNothingIsDue889": true, - "internal/protocol/h2/stream/request_deadline_889_test.go | TestClientResetOfAnExpiredRequestForgetsIt889": true, - "internal/protocol/h2/stream/request_deadline_889_test.go | TestEveryRequestOpenStreamCarriesADeadline889": true, - "internal/protocol/h2/stream/request_deadline_889_test.go | TestExpireRequestAnswers408AndRemembersTheStream889": true, - "internal/protocol/h2/stream/request_deadline_889_test.go | TestExpiredRequestTombstonesRunOut889": true, - "internal/protocol/h2/stream/request_deadline_889_test.go | TestRequestDeadlineDisarmedWhenTheStreamLeaves889": true, - "internal/protocol/h2/stream/request_deadline_889_test.go | TestRequestDeadlineNeverAllocatesOnTheRequestPath889": true, - "internal/protocol/h2/stream/write_deadline_889_test.go | TestExpiredWritesBatch889": true, - "internal/protocol/h2/stream/write_deadline_889_test.go | TestExpiredWritesWindows889": true, - "internal/protocol/h2/stream/write_rate_889_test.go | TestExpiredWritesJudgesTheRecordItFoundDue889": true, - "internal/protocol/h2/stream/write_rate_889_test.go | TestHugeTimeoutsDoNotOverflow889": true, - "internal/protocol/h2/stream/write_wait_1170_test.go | TestWaiterAndBufferNeverHaveTwoJudges1170": true, - "internal/protocol/h2/stream/write_wait_1170_test.go | TestWaiterRecordBooks1170": true, -} +var pendingFixtures = map[string]bool{} diff --git a/internal/protocol/h2/stream/deadline.go b/internal/protocol/h2/stream/deadline.go index 79788cdc3..1c8994663 100644 --- a/internal/protocol/h2/stream/deadline.go +++ b/internal/protocol/h2/stream/deadline.go @@ -4,6 +4,8 @@ import ( "time" "golang.org/x/net/http2" + + "github.com/goceleris/celeris/internal/mono" ) // The per-stream request deadline and the header-block deadline (celeris#889). @@ -169,7 +171,7 @@ func (p *Processor) SetHeaderBlockTimeout(d time.Duration) { p.hdrTimeout = clam // (a HEADERS frame without END_HEADERS). func (p *Processor) armHeaderBlock() { if p.hdrTimeout > 0 { - p.manager.hints.HdrDL.Store(time.Now().UnixNano() + int64(p.hdrTimeout)) + p.manager.hints.HdrDL.Store(mono.Now() + int64(p.hdrTimeout)) } } diff --git a/internal/protocol/h2/stream/manager.go b/internal/protocol/h2/stream/manager.go index 02f924e77..880071356 100644 --- a/internal/protocol/h2/stream/manager.go +++ b/internal/protocol/h2/stream/manager.go @@ -844,7 +844,7 @@ func (m *Manager) SetRequestTimeout(d time.Duration) { m.reqTimeout = clampTimeo func (m *Manager) RequestTimeout() time.Duration { return m.reqTimeout } // ArmRequest starts the request deadline of s, a stream just opened by a HEADERS -// frame without END_STREAM, at now (nanoseconds, wall clock). Absolute: it is +// frame without END_STREAM, at now (nanoseconds, internal/mono). Absolute: it is // not moved by the stream's DATA or by anything else. The instant is kept in // s.StartTimeNs, unused on HTTP/2 (the engines' cache of a recv's clock, which // only HTTP/1 fills), and the flag marks that it is there; the manager keeps diff --git a/internal/protocol/h2/stream/processor.go b/internal/protocol/h2/stream/processor.go index 2c979983f..e1ef53fa2 100644 --- a/internal/protocol/h2/stream/processor.go +++ b/internal/protocol/h2/stream/processor.go @@ -11,6 +11,8 @@ import ( "golang.org/x/net/http2" "golang.org/x/net/http2/hpack" + + "github.com/goceleris/celeris/internal/mono" ) // ContinuationState tracks the state of CONTINUATION frames. @@ -1751,7 +1753,7 @@ func (p *Processor) admitHeaders(id uint32, endStream bool, block []byte) (*Stre // A request that is not whole at its HEADERS has a deadline (celeris#889): // ReadTimeout from now, absolute. A request without a body (every GET) // pays the one test of endStream that is made anyway. - m.ArmRequest(stream, time.Now().UnixNano()) + m.ArmRequest(stream, mono.Now()) } return stream, admitNew, nil } diff --git a/internal/protocol/h2/stream/request_deadline_889_test.go b/internal/protocol/h2/stream/request_deadline_889_test.go index 528f9be78..08d469a20 100644 --- a/internal/protocol/h2/stream/request_deadline_889_test.go +++ b/internal/protocol/h2/stream/request_deadline_889_test.go @@ -7,6 +7,8 @@ import ( "time" "golang.org/x/net/http2" + + "github.com/goceleris/celeris/internal/mono" ) // The request deadline and the header-block deadline of the processor @@ -130,11 +132,11 @@ func TestRequestDeadlineDisarmedWhenTheStreamLeaves889(t *testing.T) { t.Fatalf("after a reset, a delete and a GOAWAY: armed %d, want 1 (stream 5)", armedIn889(m)) } // A hint left stale by the disarms costs one scan, and settles. - ids := m.ExpiredRequests(time.Now().UnixNano()+int64(time.Hour), nil) + ids := m.ExpiredRequests(mono.Now()+int64(time.Hour), nil) if len(ids) != 0 { t.Fatalf("no cap, no ids: %v", ids) } - ids = m.ExpiredRequests(time.Now().UnixNano()+int64(time.Hour), make([]uint32, 0, 8)) + ids = m.ExpiredRequests(mono.Now()+int64(time.Hour), make([]uint32, 0, 8)) if len(ids) != 1 || ids[0] != 5 { t.Fatalf("expired %v, want [5]", ids) } @@ -150,7 +152,7 @@ func TestExpireRequestAnswers408AndRemembersTheStream889(t *testing.T) { if err := p.ProcessFrame(ctx, postHeaders889(t, 1, false)); err != nil { t.Fatal(err) } - now := time.Now().UnixNano() + int64(2*time.Second) + now := mono.Now() + int64(2*time.Second) for _, id := range m.ExpiredRequests(now, make([]uint32, 0, 8)) { p.ExpireRequest(id, now) } @@ -203,7 +205,7 @@ func TestExpiredRequestTombstonesRunOut889(t *testing.T) { if err := p.ProcessFrame(ctx, postHeaders889(t, 1, false)); err != nil { t.Fatal(err) } - now := time.Now().UnixNano() + int64(2*time.Second) + now := mono.Now() + int64(2*time.Second) for _, id := range p.GetManager().ExpiredRequests(now, make([]uint32, 0, 8)) { p.ExpireRequest(id, now) } @@ -224,7 +226,7 @@ func TestRequestDeadlineNeverAllocatesOnTheRequestPath889(t *testing.T) { m := NewManager() m.SetRequestTimeout(time.Second) s, _ := m.TryOpenStream(1) - now := time.Now().UnixNano() + now := mono.Now() if n := testing.AllocsPerRun(1000, func() { m.ArmRequest(s, now) m.DisarmRequest(s) @@ -257,7 +259,7 @@ func TestHeaderBlockDeadline889(t *testing.T) { t.Fatal(err) } dl := p.NextHeaderBlockDeadline() - if dl == 0 || dl < time.Now().UnixNano() { + if dl == 0 || dl < mono.Now() { t.Fatalf("header block deadline %d, want one in the future", dl) } var buf []byte @@ -343,7 +345,7 @@ func TestEveryRequestOpenStreamCarriesADeadline889(t *testing.T) { step("a malformed request", noPath) step("a client RST_STREAM", makeRSTStreamFrame(t, 1, http2.ErrCodeCancel)) step("a POST after the reset", postHeaders889(t, 15, false)) - now := time.Now().UnixNano() + int64(2*time.Second) + now := mono.Now() + int64(2*time.Second) for _, id := range m.ExpiredRequests(now, make([]uint32, 0, 8)) { p.ExpireRequest(id, now) } @@ -361,7 +363,7 @@ func TestClientResetOfAnExpiredRequestForgetsIt889(t *testing.T) { if err := p.ProcessFrame(ctx, postHeaders889(t, 1, false)); err != nil { t.Fatal(err) } - now := time.Now().UnixNano() + int64(2*time.Second) + now := mono.Now() + int64(2*time.Second) for _, id := range p.GetManager().ExpiredRequests(now, make([]uint32, 0, 8)) { p.ExpireRequest(id, now) } diff --git a/internal/protocol/h2/stream/stream.go b/internal/protocol/h2/stream/stream.go index a3d5b3504..ffe687e64 100644 --- a/internal/protocol/h2/stream/stream.go +++ b/internal/protocol/h2/stream/stream.go @@ -113,7 +113,7 @@ type Stream struct { CachedRoutePath string CachedRouteHandlers any // []celeris.HandlerFunc — typed any to avoid cycle CachedRouteFullPath string - // StartTimeNs is the engine's cached time.Now().UnixNano() for the + // StartTimeNs is the engine's cached mono.Now() for the // recv that produced this request. populated by populateCachedStream // from the engine's worker-local clock cache so HandleStream avoids a // per-request time.Now() vDSO call. Zero means "unset, fall back to diff --git a/internal/protocol/h2/stream/write_deadline.go b/internal/protocol/h2/stream/write_deadline.go index b67d8eddf..9f53197e9 100644 --- a/internal/protocol/h2/stream/write_deadline.go +++ b/internal/protocol/h2/stream/write_deadline.go @@ -5,6 +5,8 @@ import ( "time" "golang.org/x/net/http2" + + "github.com/goceleris/celeris/internal/mono" ) // The per-stream write deadline (celeris#889, D2). @@ -110,7 +112,7 @@ func clampTimeout(d time.Duration) time.Duration { // stall is the record of a stream that holds buffered bytes. type stall struct { - armedAt int64 // when the current window began (wall clock ns) + armedAt int64 // when the current window began (internal/mono ns) held int // bytes the stream held when the window began pending int // bytes it holds now released int // bytes of its own released in the window @@ -160,7 +162,7 @@ func (m *Manager) noteBuffered(s *Stream, n int) { m.lowerWrDL(st.armedAt + int64(m.wrTimeout)) } if st == nil { - now := time.Now().UnixNano() // read only when a window begins, not for every append to a stalled stream + now := mono.Now() // read only when a window begins, not for every append to a stalled stream if m.stalls == nil { m.stalls = make(map[uint32]*stall) } diff --git a/internal/protocol/h2/stream/write_deadline_889_test.go b/internal/protocol/h2/stream/write_deadline_889_test.go index f891473d3..559f3d17b 100644 --- a/internal/protocol/h2/stream/write_deadline_889_test.go +++ b/internal/protocol/h2/stream/write_deadline_889_test.go @@ -9,6 +9,8 @@ import ( "time" "golang.org/x/net/http2" + + "github.com/goceleris/celeris/internal/mono" ) // The write deadline's books and verdicts (celeris#889, D2): the records, the @@ -70,7 +72,7 @@ func stalled889(m *Manager, id uint32, n int) *Stream { func discard889(uint32, bool, []byte) {} -func dueAt889() int64 { return time.Now().UnixNano() + int64(wt889) + int64(time.Millisecond) } +func dueAt889() int64 { return mono.Now() + int64(wt889) + int64(time.Millisecond) } // A record is made when a buffer fills and only with a write deadline, and the hint // is the end of the first window; sending that fits the window makes none. @@ -85,9 +87,9 @@ func TestStallIsArmedByABufferOnly889(t *testing.T) { if m.stalledStreams() != 0 || m.NextWriteDeadline() != 0 { t.Fatalf("a write that fit the window left %d records, hint %d", m.stalledStreams(), m.NextWriteDeadline()) } - before := time.Now().UnixNano() + before := mono.Now() stalled889(m, 3, 100) - after := time.Now().UnixNano() + after := mono.Now() if m.stalledStreams() != 1 { t.Fatalf("%d records after a buffer filled, want 1", m.stalledStreams()) } @@ -154,7 +156,7 @@ func TestExpiredWritesFloor889(t *testing.T) { func TestExpiredWritesWindows889(t *testing.T) { _, m, _ := wdSetup889(t) s := stalled889(m, 1, 1<<22) - start := time.Now().UnixNano() + start := mono.Now() if out := m.ExpiredWrites(start+int64(wt889)/2, make([]uint32, 0, 4)); len(out) != 0 { t.Fatalf("expired %v half a window in", out) } @@ -275,7 +277,7 @@ func TestExpiredWritesBatch889(t *testing.T) { stalled889(m, uint32(1+2*i), 100) time.Sleep(2 * time.Millisecond) // a slow runner (the coverage build): the records are not armed in one instant } - start := time.Now().UnixNano() // after the records: every window is judged for all of them (CI at 7e7f5ce: 2 of 40 armed after start+1ms missed window 1) + start := mono.Now() // after the records: every window is judged for all of them (CI at 7e7f5ce: 2 of 40 armed after start+1ms missed window 1) // The connection holds 4000 bytes, under one floor: its tail is held for the idle // windows (r9 F-A2) rather than reset at the first one, and the batch limit is shown // in the window after, when 40 are due. diff --git a/internal/protocol/h2/stream/write_rate_889_test.go b/internal/protocol/h2/stream/write_rate_889_test.go index cf3a3c92d..5399f958d 100644 --- a/internal/protocol/h2/stream/write_rate_889_test.go +++ b/internal/protocol/h2/stream/write_rate_889_test.go @@ -5,6 +5,8 @@ import ( "sync/atomic" "testing" "time" + + "github.com/goceleris/celeris/internal/mono" ) // The refinements of the minimum rate that keep it from cutting a slow reader that @@ -26,7 +28,7 @@ func windowEnd889(m *Manager, start int64, n int) []uint32 { func TestWriteSurplusCarriesOverUpToTheBank889(t *testing.T) { _, m, _ := wdSetup889(t) s := stalled889(m, 1, 8<<20) - start := time.Now().UnixNano() + start := mono.Now() s.SetWindowSize(3 << 20) // 3 MiB at once, in window 1 m.FlushOutbound(s, discard889) // Window 1 is held on the release, the bank is capped at writeBankWindows floors: @@ -59,7 +61,7 @@ func TestWriteAverageRateIsTheGuarantee889(t *testing.T) { _, m, _ := wdSetup889(t) m.initWin.Store(1 << 20) // the stream stalls with a megabyte in flight: a full seed s := stalled889(m, 1, 64<<20) - start := time.Now().UnixNano() + start := mono.Now() expiredAt := 0 for w := 1; w <= 200 && expiredAt == 0; w++ { if (w-1)%tc.every == tc.every-1 { @@ -98,7 +100,7 @@ func TestWriteSeedCoversTheFirstWindow889(t *testing.T) { _, m, _ := wdSetup889(t) m.initWin.Store(tc.initWin) stalled889(m, 1, 8<<20) // window 0: the stream sent initWin bytes - start := time.Now().UnixNano() + start := mono.Now() held := 0 for w := 1; w <= 10; w++ { if out := windowEnd889(m, start, w); len(out) != 0 { @@ -120,7 +122,7 @@ func TestWriteDribblerIsResetWithinTwoWindowsOfADefaultWindow889(t *testing.T) { _, m, _ := wdSetup889(t) m.initWin.Store(65535) s := stalled889(m, 1, 4<<20) - start := time.Now().UnixNano() + start := mono.Now() n := 0 for w := 1; w <= 5 && n == 0; w++ { for i := 0; i < 10; i++ { // 10 one-byte grants a window @@ -149,7 +151,7 @@ func TestWriteIdleStreamOnAHealthyConnIsBounded889(t *testing.T) { } t.Run("zero-release-is-held-K-windows", func(t *testing.T) { m, a, _ := setup(t) - start := time.Now().UnixNano() + start := mono.Now() for w := 1; w <= writeIdleWindows+1; w++ { a.SetWindowSize(70000) m.FlushOutbound(a, discard889) // the sibling moves the connection above the floor @@ -164,7 +166,7 @@ func TestWriteIdleStreamOnAHealthyConnIsBounded889(t *testing.T) { }) t.Run("a-dribble-is-no-share", func(t *testing.T) { m, a, b := setup(t) - start := time.Now().UnixNano() + start := mono.Now() for w := 1; w <= writeIdleWindows+1; w++ { a.SetWindowSize(70000) m.FlushOutbound(a, discard889) @@ -180,7 +182,7 @@ func TestWriteIdleStreamOnAHealthyConnIsBounded889(t *testing.T) { }) t.Run("its-share-holds-it-for-ever", func(t *testing.T) { m, a, b := setup(t) - start := time.Now().UnixNano() + start := mono.Now() for w := 1; w <= 30; w++ { a.SetWindowSize(40000) m.FlushOutbound(a, discard889) @@ -193,7 +195,7 @@ func TestWriteIdleStreamOnAHealthyConnIsBounded889(t *testing.T) { }) t.Run("a-slow-connection-is-the-verdict-at-once", func(t *testing.T) { m, a, b := setup(t) - start := time.Now().UnixNano() + start := mono.Now() a.SetWindowSize(20000) m.FlushOutbound(a, discard889) b.SetWindowSize(20000) @@ -215,7 +217,7 @@ func TestExpiredWritesJudgesTheRecordItFoundDue889(t *testing.T) { m.mu.Lock() // hold the read of the streams' windows done := make(chan []uint32) - go func() { done <- m.ExpiredWrites(time.Now().UnixNano(), make([]uint32, 0, 32)) }() + go func() { done <- m.ExpiredWrites(mono.Now(), make([]uint32, 0, 32)) }() time.Sleep(50 * time.Millisecond) // Meanwhile a pool goroutine's flush takes the whole buffer and it buffers a new tail. s.SetWindowSize(1 << 20) @@ -249,10 +251,10 @@ func TestHugeTimeoutsDoNotOverflow889(t *testing.T) { _, m, _ := wdSetup889(t) m.SetWriteTimeout(never) stalled889(m, 1, 5000) - if h := m.NextWriteDeadline(); h <= time.Now().UnixNano() { + if h := m.NextWriteDeadline(); h <= mono.Now() { t.Fatalf("hint %d is not in the future", h) } - if out := m.ExpiredWrites(time.Now().UnixNano()+int64(time.Hour), make([]uint32, 0, 4)); len(out) != 0 { + if out := m.ExpiredWrites(mono.Now()+int64(time.Hour), make([]uint32, 0, 4)); len(out) != 0 { t.Fatalf("expired %v an hour in", out) } }) @@ -261,11 +263,11 @@ func TestHugeTimeoutsDoNotOverflow889(t *testing.T) { m.SetRequestTimeout(never) s := m.CreateStream(1) s.SetState(StateOpen) - m.ArmRequest(s, time.Now().UnixNano()) - if h := m.NextRequestDeadline(); h <= time.Now().UnixNano() { + m.ArmRequest(s, mono.Now()) + if h := m.NextRequestDeadline(); h <= mono.Now() { t.Fatalf("request deadline hint %d is not in the future", h) } - if out := m.ExpiredRequests(time.Now().UnixNano()+int64(time.Hour), make([]uint32, 0, 4)); len(out) != 0 { + if out := m.ExpiredRequests(mono.Now()+int64(time.Hour), make([]uint32, 0, 4)); len(out) != 0 { t.Fatalf("expired %v an hour in", out) } }) @@ -283,7 +285,7 @@ func TestHugeTimeoutsDoNotOverflow889(t *testing.T) { func TestWriteFloorFollowsWhatTheStreamHolds889(t *testing.T) { _, m, _ := wdSetup889(t) s := stalled889(m, 1, 100000) // held 100000: floor 65536 - start := time.Now().UnixNano() + start := mono.Now() s.SetWindowSize(70000) // 70000 released, 30000 left m.FlushOutbound(s, discard889) if out := windowEnd889(m, start, 1); len(out) != 0 { @@ -309,14 +311,14 @@ func TestWriteTailGraceIsBounded889(t *testing.T) { t.Run("nothing-released-is-held-for-the-idle-windows", func(t *testing.T) { _, m, _ := wdSetup889(t) stalled889(m, 1, 1000) - if w := firstReset889(m, 1, time.Now().UnixNano(), 10, nil); w != writeIdleWindows+1 { + if w := firstReset889(m, 1, mono.Now(), 10, nil); w != writeIdleWindows+1 { t.Fatalf("reset in window %d, want %d (held for %d)", w, writeIdleWindows+1, writeIdleWindows) } }) t.Run("half-every-window-is-bounded", func(t *testing.T) { _, m, _ := wdSetup889(t) s := stalled889(m, 1, 1000) - start := time.Now().UnixNano() + start := mono.Now() for w := 1; w <= writeIdleWindows+1; w++ { s.SetWindowSize(500) // half of the 1000 held at the window's start... m.FlushOutbound(s, discard889) @@ -349,7 +351,7 @@ func TestWriteConnTailGrace889(t *testing.T) { stalled889(m, uint32(1+2*i), 100) time.Sleep(2 * time.Millisecond) // a slow runner (the coverage build): the records are not armed in one instant } - start := time.Now().UnixNano() // after the records, so that window 1 is judged for all of them + start := mono.Now() // after the records, so that window 1 is judged for all of them c := m.CreateStream(99) c.SetState(StateOpen) c.SetWindowSize(1 << 20) @@ -402,7 +404,7 @@ func TestWriteUnreadStreamBounds889(t *testing.T) { _, m, _ := wdSetup889(t) m.initWin.Store(tc.initWin) stalled889(m, 1, 8<<20) - if w := firstReset889(m, 1, time.Now().UnixNano(), 20, nil); w != tc.alone { + if w := firstReset889(m, 1, mono.Now(), 20, nil); w != tc.alone { t.Fatalf("alone: reset in window %d, want %d", w, tc.alone) } _, m2, _ := wdSetup889(t) @@ -413,7 +415,7 @@ func TestWriteUnreadStreamBounds889(t *testing.T) { a.SetWindowSize(0) a.BufferOutbound(make([]byte, 64<<20), true) _ = b - w := firstReset889(m2, 1, time.Now().UnixNano(), 20, func(int) { a.SetWindowSize(70000); m2.FlushOutbound(a, discard889) }) + w := firstReset889(m2, 1, mono.Now(), 20, func(int) { a.SetWindowSize(70000); m2.FlushOutbound(a, discard889) }) if w != tc.sib { t.Fatalf("with a sibling that moves the connection: reset in window %d, want %d", w, tc.sib) } @@ -433,7 +435,7 @@ func TestWriteDribblerBounds889(t *testing.T) { _, m, _ := wdSetup889(t) m.initWin.Store(tc.initWin) s := stalled889(m, 1, 8<<20) - w := firstReset889(m, 1, time.Now().UnixNano(), 20, func(int) { + w := firstReset889(m, 1, mono.Now(), 20, func(int) { for i := 0; i < 10; i++ { s.SetWindowSize(1) m.FlushOutbound(s, discard889) @@ -458,7 +460,7 @@ func TestWriteIdleCountRestarts889(t *testing.T) { move := func(m *Manager, a *Stream) { a.SetWindowSize(70000); m.FlushOutbound(a, discard889) } t.Run("share", func(t *testing.T) { m, a, b := setup(t) - got := firstReset889(m, 1, time.Now().UnixNano(), 20, func(w int) { + got := firstReset889(m, 1, mono.Now(), 20, func(w int) { move(m, a) if w == 2 { // one window in which it takes its share b.SetWindowSize(20000) @@ -471,7 +473,7 @@ func TestWriteIdleCountRestarts889(t *testing.T) { }) t.Run("floor", func(t *testing.T) { m, a, b := setup(t) - got := firstReset889(m, 1, time.Now().UnixNano(), 20, func(w int) { + got := firstReset889(m, 1, mono.Now(), 20, func(w int) { move(m, a) if w == 2 { b.SetWindowSize(70000) @@ -484,7 +486,7 @@ func TestWriteIdleCountRestarts889(t *testing.T) { }) t.Run("starved", func(t *testing.T) { m, a, b := setup(t) - got := firstReset889(m, 1, time.Now().UnixNano(), 20, func(w int) { + got := firstReset889(m, 1, mono.Now(), 20, func(w int) { if w == 3 { // the conn window is shut and b's own has room: starved; a sibling takes the grant atomic.StoreInt32(&m.connectionWindow, 0) m.connHigh.Store(0) // nothing in flight at the connection level (the allowance has its own tests) @@ -513,7 +515,7 @@ func TestWriteShareScalesWithTheStreamsAndHalves889(t *testing.T) { for i := 2; i < n; i++ { stalled889(m, uint32(1+2*i), 64<<20) } - return firstReset889(m, 1, time.Now().UnixNano(), 12, func(int) { + return firstReset889(m, 1, mono.Now(), 12, func(int) { a.SetWindowSize(70000) m.FlushOutbound(a, discard889) b.SetWindowSize(int32(release)) @@ -545,7 +547,7 @@ func TestWriteSeedIsWhatIsInFlight889(t *testing.T) { s.SetWindowSize(1<<20 - 100000) // 100000 bytes sent, not granted back s.BufferOutbound(make([]byte, 8<<20), true) // 100000 covers window 1 (above the floor) and not window 2. - if w := firstReset889(m, 1, time.Now().UnixNano(), 10, nil); w != 2 { + if w := firstReset889(m, 1, mono.Now(), 10, nil); w != 2 { t.Fatalf("reset in window %d, want 2: the seed is what is in flight, not the initial window", w) } } @@ -572,7 +574,7 @@ func TestWriteBankIsSpentByAShareWindow889(t *testing.T) { _, m, _ := wdSetup889(t) b := stalled889(m, 1, 64<<20) a := stalled889(m, 3, 64<<20) - start := time.Now().UnixNano() + start := mono.Now() // Window 1: b releases 95536 (above the floor: 30000 banked) and a moves the conn. b.SetWindowSize(95536) m.FlushOutbound(b, discard889) @@ -661,7 +663,7 @@ func TestWriteUnreadSmallRemainderBounds889(t *testing.T) { _, m, _ := wdSetup889(t) m.initWin.Store(tc.initWin) stalled889(m, 1, held) - if w := firstReset889(m, 1, time.Now().UnixNano(), 40, nil); w != tc.want { + if w := firstReset889(m, 1, mono.Now(), 40, nil); w != tc.want { t.Fatalf("reset in window %d, want %d (0 means not within 40)", w, tc.want) } }) @@ -677,7 +679,7 @@ func TestWriteBankSurvivesASmallRenewal889(t *testing.T) { t.Run(order, func(t *testing.T) { _, m, _ := wdSetup889(t) s := stalled889(m, 1, 320<<10) - start := time.Now().UnixNano() + start := mono.Now() s.SetWindowSize(312 << 10) m.FlushOutbound(s, discard889) if order == "buffer-before-renewal" { @@ -701,7 +703,7 @@ func TestWriteDribblerWithASmallRestSpendsItsBank889(t *testing.T) { _, m, _ := wdSetup889(t) m.initWin.Store(1 << 20) // 256 KiB of bank s := stalled889(m, 1, 1000) - got := firstReset889(m, 1, time.Now().UnixNano(), 40, func(int) { + got := firstReset889(m, 1, mono.Now(), 40, func(int) { s.SetWindowSize(1) m.FlushOutbound(s, discard889) s.BufferOutbound([]byte{0}, true) @@ -720,7 +722,7 @@ func TestWriteBankIsNotADebt889(t *testing.T) { _, m, _ := wdSetup889(t) m.initWin.Store(20000) s := stalled889(m, 1, 1000) - got := firstReset889(m, 1, time.Now().UnixNano(), 12, func(w int) { + got := firstReset889(m, 1, mono.Now(), 12, func(w int) { if w == 2 { s.BufferOutbound(make([]byte, 1<<20), true) s.SetWindowSize(135000) @@ -743,7 +745,7 @@ func TestWriteSmallConnShareHoldClearsIdle889(t *testing.T) { c := m.CreateStream(99) c.SetState(StateOpen) c.SetWindowSize(1 << 30) - start := time.Now().UnixNano() + start := mono.Now() got := firstReset889(m, 1, start, 12, func(int) { a.SetWindowSize(3000) // more than its share (10000 / (2 x 2)), less than half of what it holds m.FlushOutbound(a, discard889) diff --git a/internal/protocol/h2/stream/write_wait.go b/internal/protocol/h2/stream/write_wait.go index b8d9f8e44..c4d164d7b 100644 --- a/internal/protocol/h2/stream/write_wait.go +++ b/internal/protocol/h2/stream/write_wait.go @@ -7,6 +7,8 @@ import ( "sync" "sync/atomic" "time" + + "github.com/goceleris/celeris/internal/mono" ) // The pool handler's wait for window as a minimum rate (celeris#1170, stage 3 of @@ -45,7 +47,7 @@ import ( type SendWait struct { m *Manager id uint32 - start int64 // when the wait first blocked (wall clock ns) + start int64 // when the wait first blocked (internal/mono ns) made bool // the record was made (and not just re-made) failed bool // judged and reset by the loop-blocked fallback // Loops is the server's watch on its event loops, set by the caller (the conn @@ -148,7 +150,7 @@ func (m *Manager) beginWait(s *Stream, id uint32, w *SendWait, rest int, gen uin m.stallMu.Lock() st := m.stalls[id] if st == nil { - now := time.Now().UnixNano() // read only when a record is made, not for every wait + now := mono.Now() // read only when a record is made, not for every wait if m.stalls == nil { m.stalls = make(map[uint32]*stall) } @@ -160,7 +162,7 @@ func (m *Manager) beginWait(s *Stream, id uint32, w *SendWait, rest int, gen uin end := st.armedAt + wt m.stallMu.Unlock() if w.start == 0 { - w.start = time.Now().UnixNano() + w.start = mono.Now() } w.m, w.id, w.made = m, id, true return end @@ -185,7 +187,7 @@ func (m *Manager) judgeWait(s *Stream, id uint32, w *SendWait, rest int) (waitVe return waitCancelled, 0 } wt := int64(m.wrTimeout) - now := time.Now().UnixNano() + now := mono.Now() if lw := w.Loops; lw != nil { if idx, age, stuck := lw.stuck(now, loopStuckFor(wt)); !stuck { lw.noteClear() @@ -293,7 +295,7 @@ func (s *Stream) awaitSendWindowRate(m *Manager, id uint32, done <-chan struct{} end := m.beginWait(s, id, w, rest, gen) var expired <-chan time.Time if end != 0 { - d := time.Until(time.Unix(0, end)) + d := mono.Until(end) if sw.timer == nil { sw.timer = time.NewTimer(d) } else { @@ -315,7 +317,7 @@ func (s *Stream) awaitSendWindowRate(m *Manager, id uint32, done <-chan struct{} case waitExpired: return os.ErrDeadlineExceeded default: - sw.timer.Reset(time.Until(time.Unix(0, next))) + sw.timer.Reset(mono.Until(next)) return errStillWaiting } } @@ -415,7 +417,7 @@ func (w *LoopWatch) Slot() *LoopSlot { return s } -// Enter stamps the start of a batch at now (wall clock ns). +// Enter stamps the start of a batch at now (internal/mono ns). func (s *LoopSlot) Enter(now int64) { if s != nil { s.since.Store(now) diff --git a/internal/protocol/h2/stream/write_wait_1170_test.go b/internal/protocol/h2/stream/write_wait_1170_test.go index ed574f203..f6d5740ee 100644 --- a/internal/protocol/h2/stream/write_wait_1170_test.go +++ b/internal/protocol/h2/stream/write_wait_1170_test.go @@ -10,6 +10,8 @@ import ( "time" "golang.org/x/net/http2" + + "github.com/goceleris/celeris/internal/mono" ) // celeris#1170: a pool handler's wait for window is a record in the stall books, @@ -258,12 +260,12 @@ func TestWaiterRecordBooks1170(t *testing.T) { t.Fatalf("NoteWaitSent credited a buffered record: %d", rel) } // The sweep skips it, in the verdicts and in the hint. - if out := m.ExpiredWrites(time.Now().UnixNano()+int64(2*time.Hour), make([]uint32, 0, 8)); len(out) != 1 || out[0] != other.ID { + if out := m.ExpiredWrites(mono.Now()+int64(2*time.Hour), make([]uint32, 0, 8)); len(out) != 1 || out[0] != other.ID { t.Fatalf("the sweep expired %v, want only the buffered stream %d", out, other.ID) } m.dropStall(other.ID) if m.hints.WrDL.Load() != 0 { - _ = m.ExpiredWrites(time.Now().UnixNano(), make([]uint32, 0, 8)) + _ = m.ExpiredWrites(mono.Now(), make([]uint32, 0, 8)) } if h := m.NextWriteDeadline(); h != 0 { t.Fatalf("hint %d with only a waiter: the sweep would spin", h) @@ -319,7 +321,7 @@ func TestWaiterAndBufferNeverHaveTwoJudges1170(t *testing.T) { if v, _ := m.judgeWait(s, s.ID, &w, 300000); v != waitHeld { t.Fatalf("the waiter's judge decided %v on the sweep's record", v) } - if out := m.ExpiredWrites(time.Now().UnixNano()+int64(wait1170)*2, make([]uint32, 0, 4)); len(out) != 1 { + if out := m.ExpiredWrites(mono.Now()+int64(wait1170)*2, make([]uint32, 0, 4)); len(out) != 1 { t.Fatalf("the sweep expired %v, want the stream (it judges now)", out) } } @@ -478,7 +480,7 @@ func TestUnreadStreamBoundWithAQueueAhead1170(t *testing.T) { a.SetState(StateOpen) a.SetWindowSize(0) a.BufferOutbound(make([]byte, 64<<20), true) - w := firstReset889(m, 1, time.Now().UnixNano(), 60, func(int) { + w := firstReset889(m, 1, mono.Now(), 60, func(int) { a.SetWindowSize(100000) // the sibling takes 100000 a window: the connection moves m.FlushOutbound(a, discard889) m.connectionWindow = int32(1<<30+65535) - int32(tc.inflight) // the client returns what it reads @@ -501,7 +503,7 @@ func TestIdleAllowanceIsFixedWhenTheRunBegins1170(t *testing.T) { a.SetState(StateOpen) a.SetWindowSize(0) a.BufferOutbound(make([]byte, 64<<20), true) - w := firstReset889(m, 1, time.Now().UnixNano(), 60, func(win int) { + w := firstReset889(m, 1, mono.Now(), 60, func(win int) { a.SetWindowSize(100000) m.FlushOutbound(a, discard889) if win > 1 { @@ -534,7 +536,7 @@ func TestWaiterLoopBlockedFallback1170(t *testing.T) { // A loop that is always this far into a batch (it re-enters as time passes). quit := make(chan struct{}) defer close(quit) - stamp := func() { slot.Enter(time.Now().Add(-tc.stuck).UnixNano()) } + stamp := func() { slot.Enter((mono.Now() + int64(-tc.stuck))) } stamp() go func() { for { @@ -569,7 +571,7 @@ func TestWaiterLoopBlockedFallback1170(t *testing.T) { func TestLoopWatch1170(t *testing.T) { lw := NewLoopWatch() a, b := lw.Slot(), lw.Slot() - now := time.Now().UnixNano() + now := mono.Now() if lw.BlockedFor(now, 1) { t.Fatal("idle loops read as stuck") } @@ -601,7 +603,7 @@ func TestIdleRunThatBeginsInATailKeepsAnAllowance1170(t *testing.T) { b := &stall{held: 10, pending: 10, connHeld: 20} m.stalls = map[uint32]*stall{1: a, 3: b} m.stallN.Store(2) - now := time.Now().UnixNano() + now := mono.Now() m.stallMu.Lock() defer m.stallMu.Unlock() h1 := m.holdsLocked(a, false, now) @@ -627,7 +629,7 @@ func TestIdleAllowanceIsTheLargestOfTheRun1170(t *testing.T) { other := &stall{held: 1 << 20, pending: 1 << 20, connHeld: 8 << 20} m.stalls = map[uint32]*stall{1: st, 3: other} m.stallN.Store(2) - now := time.Now().UnixNano() + now := mono.Now() m.stallMu.Lock() defer m.stallMu.Unlock() for w := 1; w <= 60; w++ { @@ -663,7 +665,7 @@ func TestEmptyWindowsOfAMovingConnectionAreHeldUpToTheHorizon1170(t *testing.T) m.initWin.Store(0) a := stalled889(m, 1, 64<<20) b := stalled889(m, 3, 64<<20) - start := time.Now().UnixNano() + start := mono.Now() return firstReset889(m, 1, start, 40, func(w int) { a.SetWindowSize(20000) m.FlushOutbound(a, discard889) @@ -712,7 +714,7 @@ func TestLoopWatchTellsAnEpisodeOnce1170(t *testing.T) { lw.SetLogger(slog.New(&h)) slot := lw.Slot() wt := int64(time.Second) - now := time.Now().UnixNano() + now := mono.Now() for i := 0; i < 5; i++ { // five judgements that see the loop stuck: one line slot.Enter(now - int64(time.Second)) if idx, _, ok := lw.stuck(now, loopStuckFor(wt)); ok { @@ -750,7 +752,7 @@ func TestHorizonWindowsAreIdleWindows1170(t *testing.T) { stalled889(m, 1, 64<<20) b := stalled889(m, 3, 64<<20) m.connectionWindow = 1<<30 - 16<<20 - return firstReset889(m, 1, time.Now().UnixNano(), 80, func(w int) { + return firstReset889(m, 1, mono.Now(), 80, func(w int) { if moves(w) { b.SetWindowSize(70000) m.FlushOutbound(b, discard889) @@ -787,7 +789,7 @@ func TestIdleAllowanceStartsAgainWithEachRun1170(t *testing.T) { a := stalled889(m, 1, 64<<20) b := stalled889(m, 3, 64<<20) m.connectionWindow = 1<<30 - 16<<20 // 16 MiB in flight: the first run has the cap - got := firstReset889(m, 1, time.Now().UnixNano(), 80, func(w int) { + got := firstReset889(m, 1, mono.Now(), 80, func(w int) { b.SetWindowSize(70000) // the connection moves in every window m.FlushOutbound(b, discard889) switch w {