github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/kvserverpb/liveness.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package kvserverpb
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    17  )
    18  
    19  // IsLive returns whether the node is considered live at the given time.
    20  //
    21  // NOTE: If one is interested whether the Liveness is valid currently, then the
    22  // timestamp passed in should be the known high-water mark of all the clocks of
    23  // the nodes in the cluster. For example, if the liveness expires at ts 100, our
    24  // physical clock is at 90, but we know that another node's clock is at 110,
    25  // then it's preferable (more consistent across nodes) for the liveness to be
    26  // considered expired. For that purpose, it's better to pass in
    27  // clock.Now().GoTime() rather than clock.PhysicalNow() - the former takes into
    28  // consideration clock signals from other nodes, the latter doesn't.
    29  func (l *Liveness) IsLive(now time.Time) bool {
    30  	expiration := timeutil.Unix(0, l.Expiration.WallTime)
    31  	return now.Before(expiration)
    32  }
    33  
    34  // IsDead returns true if the liveness expired more than threshold ago.
    35  //
    36  // Note that, because of threshold, IsDead() is not the inverse of IsLive().
    37  func (l *Liveness) IsDead(now time.Time, threshold time.Duration) bool {
    38  	expiration := timeutil.Unix(0, l.Expiration.WallTime)
    39  	deadAsOf := expiration.Add(threshold)
    40  	return !now.Before(deadAsOf)
    41  }