Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions internal/conn/h2_sweep_889_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) }()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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'}
Expand All @@ -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})
})
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/mono/mono.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions internal/mono/mono_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
32 changes: 1 addition & 31 deletions internal/mono/scan_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
4 changes: 3 additions & 1 deletion internal/protocol/h2/stream/deadline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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))
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/protocol/h2/stream/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion internal/protocol/h2/stream/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 10 additions & 8 deletions internal/protocol/h2/stream/request_deadline_889_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/protocol/h2/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions internal/protocol/h2/stream/write_deadline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 7 additions & 5 deletions internal/protocol/h2/stream/write_deadline_889_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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())
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading