Skip to content

Commit

Permalink
server: only sleep to ensure clock monotonicity on server restart
Browse files Browse the repository at this point in the history
This was broken in #72276 in an effort to fix a previous regression.
It recovers the previous behavior of only sleeping when a server is
being restarted, but doing so even if a max HLC upper bound has not
been persisted.

This fixes a regression in the speed of logic tests:
```
// before
ok  	github.com/cockroachdb/cockroach/pkg/sql/opt/exec/execbuilder	53.891s

// after
ok  	github.com/cockroachdb/cockroach/pkg/sql/opt/exec/execbuilder	40.891s
```
  • Loading branch information
nvanbenschoten committed Nov 17, 2021
1 parent 9856ebf commit 315c349
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1590,20 +1590,25 @@ func (s *Server) PreStart(ctx context.Context) error {
}
})

// NB: if this store is freshly initialized (or no upper bound was
// persisted), hlcUpperBound will be zero.
hlcUpperBound, err := kvserver.ReadMaxHLCUpperBound(ctx, s.engines)
if err != nil {
return errors.Wrap(err, "reading max HLC upper bound")
}
// If the server is being restarted, sleep to ensure monotonicity of the HLC
// clock. This can be skipped on server bootstrap because the server has never
// been used before.
var hlcUpperBoundExists bool
if !initialStart {
hlcUpperBound, err := kvserver.ReadMaxHLCUpperBound(ctx, s.engines)
if err != nil {
return errors.Wrap(err, "reading max HLC upper bound")
}
hlcUpperBoundExists = hlcUpperBound > 0

ensureClockMonotonicity(
ctx,
s.clock,
s.startTime,
hlcUpperBound,
s.clock.SleepUntil,
)
ensureClockMonotonicity(
ctx,
s.clock,
s.startTime,
hlcUpperBound,
s.clock.SleepUntil,
)
}

// Record a walltime that is lower than the lowest hlc timestamp this current
// instance of the node can use. We do not use startTime because it is lower
Expand Down Expand Up @@ -1642,7 +1647,7 @@ func (s *Server) PreStart(ctx context.Context) error {
log.Event(ctx, "started node")
if err := s.startPersistingHLCUpperBound(
ctx,
hlcUpperBound > 0,
hlcUpperBoundExists,
func(t int64) error { /* function to persist upper bound of HLC to all stores */
return s.node.SetHLCUpperBound(context.Background(), t)
},
Expand Down

0 comments on commit 315c349

Please sign in to comment.