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
2 changes: 1 addition & 1 deletion internal/daemon/keepalive.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (d *Daemon) runKeepalive(ctx context.Context, wake <-chan os.Signal) {
case <-wake:
woken("signal")
case <-ticker.C:
if jump := watch.tick(); jump > interval {
if jump := watch.tick(); jump >= wakeSleepFloor {
log.Printf("wall clock moved %s further than monotonic time between ticks", jump)
woken("clock")
}
Expand Down
30 changes: 17 additions & 13 deletions internal/daemon/wake.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ import "time"
// length of the sleep. The signal makes the wake prompt; the clock makes it
// certain.

// wakeRepeatWindow is how long after a handled wake another sign of one counts
// as a repeat of it rather than a wake of its own. The runner's second signal
// follows its first by a few seconds and the clock is read once a tick; two
// ticks cover both with room for a late tick. It is measured on the wall
// clock, which unlike the monotonic one moves across a sleep, so two wakes
// with a sleep between them are never taken for one however close together
// they are in running time.
const wakeRepeatWindow = 2 * workloadKeepaliveInterval
// wakeSleepFloor is how far the wall clock has to get ahead of the monotonic one
// to mean a sleep. It clears a clock's ordinary corrections and stays under the
// shortest sleep there is: freezing a guest, snapshotting it and restoring it
// take seconds.
const wakeSleepFloor = time.Second

// clockReading is one look at both clocks: the wall clock, which the runner
// sets, and the monotonic clock, which sleeps through a pause.
Expand All @@ -45,7 +42,8 @@ func readClocks() clockReading {
type wakeWatch struct {
clocks func() clockReading
last clockReading
handled time.Time
handled clockReading
woken bool
}

func newWakeWatch(clocks func() clockReading) *wakeWatch {
Expand All @@ -66,14 +64,20 @@ func (w *wakeWatch) tick() time.Duration {
}

// admit reports whether a sign of a wake is a wake of its own rather than a
// repeat of the last one handled, and counts it as handled when it is.
// repeat of the last one handled, and counts it as handled when it is. Every
// wake has a sleep behind it, and that is the only thing that tells them apart:
// the signs of one wake have none between them however far apart they come, and
// two wakes have one however close together, since a turn can start, end and
// hibernate again inside a few seconds. A wall clock stepped back says nothing
// about a sleep, and an extra inbox read costs little, so it counts as a wake.
func (w *wakeWatch) admit() bool {
now := w.clocks()
if !w.handled.IsZero() {
if since := now.wall.Sub(w.handled); since >= 0 && since < wakeRepeatWindow {
if w.woken {
apart := now.wall.Sub(w.handled.wall) - (now.mono - w.handled.mono)
if apart.Abs() < wakeSleepFloor {
return false
}
}
w.handled = now.wall
w.woken, w.handled = true, now
return true
}
39 changes: 32 additions & 7 deletions internal/daemon/wake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ func TestWakeWatchAdmitFoldsRepeatsOfAWake(t *testing.T) {
clockReading{wall: wallAt(0), mono: 0},
clockReading{wall: wallAt(time.Hour), mono: time.Second},
clockReading{wall: wallAt(time.Hour + 3*time.Second), mono: 4 * time.Second},
clockReading{wall: wallAt(time.Hour + wakeRepeatWindow - time.Millisecond), mono: 5 * time.Second},
clockReading{wall: wallAt(time.Hour + wakeRepeatWindow), mono: 6 * time.Second},
clockReading{wall: wallAt(time.Hour + wakeRepeatWindow + time.Second), mono: 7 * time.Second},
clockReading{wall: wallAt(time.Minute), mono: 8 * time.Second},
clockReading{wall: wallAt(time.Hour + time.Minute), mono: 61 * time.Second},
))
if !watch.admit() {
t.Fatal("expected the first wake admitted")
Expand All @@ -73,14 +70,41 @@ func TestWakeWatchAdmitFoldsRepeatsOfAWake(t *testing.T) {
t.Fatal("expected the signal's repeat folded into the wake")
}
if watch.admit() {
t.Fatal("expected a repeat at the edge of the window folded into the wake")
t.Fatal("expected a sign with no sleep behind it folded into the wake, however late")
}
}

// A turn can start, end and hibernate again in seconds, so the next wake can
// follow closely on the wall clock. The sleep between them is what makes it a
// wake of its own, and missing it would leave the message that caused it unread.
func TestWakeWatchAdmitsAWakeSoonAfterTheLast(t *testing.T) {
watch := newWakeWatch(scriptedClocks(
clockReading{wall: wallAt(0), mono: 0},
clockReading{wall: wallAt(time.Hour), mono: time.Second},
// Five seconds of running, then a sleep of five.
clockReading{wall: wallAt(time.Hour + 10*time.Second), mono: 6 * time.Second},
clockReading{wall: wallAt(time.Hour + 13*time.Second), mono: 9 * time.Second},
))
if !watch.admit() {
t.Fatal("expected a wake at the end of the window admitted")
t.Fatal("expected the first wake admitted")
}
if !watch.admit() {
t.Fatal("expected a wake ten seconds after the last admitted, a sleep lying between them")
}
if watch.admit() {
t.Fatal("expected a repeat of the second wake folded into it")
}
}

func TestWakeWatchAdmitsAWakeBehindAClockSteppedBack(t *testing.T) {
watch := newWakeWatch(scriptedClocks(
clockReading{wall: wallAt(0), mono: 0},
clockReading{wall: wallAt(time.Hour), mono: time.Second},
clockReading{wall: wallAt(time.Minute), mono: 2 * time.Second},
))
if !watch.admit() {
t.Fatal("expected the first wake admitted")
}
if !watch.admit() {
t.Fatal("expected a wake behind a wall clock stepped back admitted")
}
Expand Down Expand Up @@ -325,7 +349,8 @@ func TestRunClockJumpWakes(t *testing.T) {
t.Fatalf("expected one resubscribe for the jump, got %d", got)
}

clocks.sleep(time.Hour)
// A sleep shorter than a tick is a sleep all the same.
clocks.sleep(wakeSleepFloor)
waitForSyncCalls(t, errCh, consumer.Calls, 3, "the clock jumped again")
if got := subscriber.resubscribed.Load(); got != 2 {
t.Fatalf("expected a second resubscribe for a second sleep, got %d", got)
Expand Down
20 changes: 16 additions & 4 deletions internal/subscriber/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func New(client notificationsClient, threadID string) *Subscriber {
func (s *Subscriber) Run(ctx context.Context) error {
s.startOnce.Do(func() { close(s.started) })
backoff := time.Second
reopened := false
for {
if ctx.Err() != nil {
return ctx.Err()
Expand All @@ -61,6 +62,13 @@ func (s *Subscriber) Run(ctx context.Context) error {
}
s.readyOnce.Do(func() { close(s.ready) })
backoff = time.Second
// Whatever was published while the stream was down was announced to
// nobody. The first stream has nothing before it: the daemon's initial
// read covers that.
if reopened {
s.signalWake()
}
reopened = true

for {
resp, err := stream.Recv()
Expand Down Expand Up @@ -101,14 +109,18 @@ func (s *Subscriber) Run(ctx context.Context) error {
if s.threadID != "" && (!ok || payloadThreadID != s.threadID) {
continue
}
select {
case s.wake <- struct{}{}:
default:
}
s.signalWake()
}
}
}

func (s *Subscriber) signalWake() {
select {
case s.wake <- struct{}{}:
default:
}
}

// waitOut sleeps out a failure's backoff under the attempt's context and
// returns the backoff the next failure gets: doubled, or back at the start when
// Resubscribe cut the wait short, because a wake is not another failure. The
Expand Down
42 changes: 42 additions & 0 deletions internal/subscriber/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ func TestSubscriberResubscribeReopensStream(t *testing.T) {
t.Fatal("expected the first stream to be dropped")
}

// The reopening itself asks for an inbox read; after that the stream
// carries messages as the first one did.
select {
case <-sub.Wake():
case <-time.After(200 * time.Millisecond):
t.Fatal("expected a wake for the reopening")
}
streams[1].responses <- makeMessageCreated("1f5a4e5c-350a-4f3a-8dda-0ef049b89d09")
select {
case <-sub.Wake():
Expand All @@ -289,6 +296,41 @@ func TestSubscriberResubscribeReopensStream(t *testing.T) {
waitForRun(t, done)
}

// A message published while the stream was down was announced to nobody, so a
// stream that reopens asks for an inbox read. The first stream has nothing
// before it and does not.
func TestSubscriberReopenedStreamAsksForAnInboxRead(t *testing.T) {
client := &wakeNotifications{}
sub := New(client, "")

ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() {
done <- sub.Run(ctx)
}()

select {
case <-sub.Ready():
case <-time.After(200 * time.Millisecond):
t.Fatal("expected the first stream")
}
select {
case <-sub.Wake():
t.Fatal("expected no wake for the first stream")
case <-time.After(50 * time.Millisecond):
}

sub.Resubscribe()
select {
case <-sub.Wake():
case <-time.After(500 * time.Millisecond):
t.Fatal("expected a wake for the reopened stream")
}

cancel()
waitForRun(t, done)
}

// A wake can land while the subscriber is waiting out a failure. The wait is
// for the failure, and the wake is not one: Resubscribe cuts it short and the
// stream is opened at once.
Expand Down
Loading