2017-10-23 08:08:35 +00:00
|
|
|
// Package ae provides an anti-entropy mechanism for the local state.
|
2017-08-28 12:17:09 +00:00
|
|
|
package ae
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"math"
|
2017-10-23 08:08:33 +00:00
|
|
|
"sync/atomic"
|
2017-08-28 12:17:09 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/lib"
|
|
|
|
)
|
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
const (
|
|
|
|
// This scale factor means we will add a minute after we cross 128 nodes,
|
|
|
|
// another at 256, another at 512, etc. By 8192 nodes, we will scale up
|
|
|
|
// by a factor of 8.
|
|
|
|
//
|
|
|
|
// If you update this, you may need to adjust the tuning of
|
|
|
|
// CoordinateUpdatePeriod and CoordinateUpdateMaxBatchSize.
|
|
|
|
aeScaleThreshold = 128
|
|
|
|
|
|
|
|
syncStaggerIntv = 3 * time.Second
|
|
|
|
syncRetryIntv = 15 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
// aeScale is used to scale the time interval at which anti-entropy updates take
|
|
|
|
// place. It is used to prevent saturation as the cluster size grows.
|
|
|
|
func aeScale(d time.Duration, n int) time.Duration {
|
|
|
|
// Don't scale until we cross the threshold
|
|
|
|
if n <= aeScaleThreshold {
|
|
|
|
return d
|
2017-08-28 12:17:09 +00:00
|
|
|
}
|
2017-10-23 08:08:35 +00:00
|
|
|
|
|
|
|
mult := math.Ceil(math.Log2(float64(n))-math.Log2(aeScaleThreshold)) + 1.0
|
|
|
|
return time.Duration(mult) * d
|
2017-08-28 12:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type StateSyncer struct {
|
2017-10-23 08:08:35 +00:00
|
|
|
// paused is used to check if we are paused. Must be the first
|
|
|
|
// element due to a go bug.
|
|
|
|
// todo(fs): which bug? still relevant?
|
2017-10-23 08:08:33 +00:00
|
|
|
paused int32
|
|
|
|
|
2017-08-28 12:17:09 +00:00
|
|
|
// State contains the data that needs to be synchronized.
|
2017-10-23 08:08:33 +00:00
|
|
|
State interface {
|
2017-10-23 08:08:34 +00:00
|
|
|
UpdateSyncState() error
|
2017-10-23 08:08:33 +00:00
|
|
|
SyncChanges() error
|
|
|
|
}
|
2017-08-28 12:17:09 +00:00
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
// Interval is the time between two sync runs.
|
2017-08-28 12:17:09 +00:00
|
|
|
Interval time.Duration
|
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
// ClusterSize returns the number of members in the cluster.
|
|
|
|
// todo(fs): we use this for staggering but what about a random number?
|
2017-08-28 12:17:09 +00:00
|
|
|
ClusterSize func() int
|
|
|
|
|
2017-10-23 08:08:33 +00:00
|
|
|
// ShutdownCh is closed when the application is shutting down.
|
|
|
|
ShutdownCh chan struct{}
|
2017-08-28 12:17:09 +00:00
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
// ConsulCh contains data when a new consul server has been added to the cluster.
|
|
|
|
ConsulCh chan struct{}
|
2017-08-28 12:17:09 +00:00
|
|
|
|
2017-10-23 08:08:33 +00:00
|
|
|
// TriggerCh contains data when a sync should run immediately.
|
|
|
|
TriggerCh chan struct{}
|
2017-08-28 12:17:09 +00:00
|
|
|
|
2017-10-23 08:08:33 +00:00
|
|
|
Logger *log.Logger
|
2017-08-28 12:17:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
// Pause is used to pause state synchronization, this can be
|
|
|
|
// used to make batch changes
|
|
|
|
func (ae *StateSyncer) Pause() {
|
|
|
|
atomic.AddInt32(&ae.paused, 1)
|
|
|
|
}
|
2017-08-28 12:17:09 +00:00
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
// Resume is used to resume state synchronization
|
|
|
|
func (ae *StateSyncer) Resume() {
|
|
|
|
paused := atomic.AddInt32(&ae.paused, -1)
|
|
|
|
if paused < 0 {
|
|
|
|
panic("unbalanced State.Resume() detected")
|
|
|
|
}
|
|
|
|
ae.changeMade()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Paused is used to check if we are paused
|
|
|
|
func (ae *StateSyncer) Paused() bool {
|
|
|
|
return atomic.LoadInt32(&ae.paused) > 0
|
|
|
|
}
|
2017-08-28 12:17:09 +00:00
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
func (ae *StateSyncer) changeMade() {
|
|
|
|
select {
|
|
|
|
case ae.TriggerCh <- struct{}{}:
|
|
|
|
default:
|
2017-10-23 08:08:33 +00:00
|
|
|
}
|
2017-10-23 08:08:35 +00:00
|
|
|
}
|
2017-10-23 08:08:33 +00:00
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
// antiEntropy is a long running method used to perform anti-entropy
|
|
|
|
// between local and remote state.
|
|
|
|
func (ae *StateSyncer) Run() {
|
|
|
|
SYNC:
|
|
|
|
// Sync our state with the servers
|
2017-08-28 12:17:09 +00:00
|
|
|
for {
|
2017-10-23 08:08:35 +00:00
|
|
|
err := ae.State.UpdateSyncState()
|
2017-10-23 08:08:35 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2017-10-23 08:08:35 +00:00
|
|
|
ae.Logger.Printf("[ERR] agent: failed to sync remote state: %v", err)
|
2017-10-23 08:08:35 +00:00
|
|
|
select {
|
2017-10-23 08:08:35 +00:00
|
|
|
case <-ae.ConsulCh:
|
|
|
|
// Stagger the retry on leader election, avoid a thundering heard
|
2017-10-23 08:08:35 +00:00
|
|
|
select {
|
2017-10-23 08:08:35 +00:00
|
|
|
case <-time.After(lib.RandomStagger(aeScale(syncStaggerIntv, ae.ClusterSize()))):
|
|
|
|
case <-ae.ShutdownCh:
|
2017-10-23 08:08:32 +00:00
|
|
|
return
|
2017-08-28 12:17:09 +00:00
|
|
|
}
|
2017-10-23 08:08:35 +00:00
|
|
|
case <-time.After(syncRetryIntv + lib.RandomStagger(aeScale(syncRetryIntv, ae.ClusterSize()))):
|
|
|
|
case <-ae.ShutdownCh:
|
2017-10-23 08:08:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 08:08:35 +00:00
|
|
|
// Force-trigger AE to pickup any changes
|
|
|
|
ae.changeMade()
|
|
|
|
|
|
|
|
// Schedule the next full sync, with a random stagger
|
|
|
|
aeIntv := aeScale(ae.Interval, ae.ClusterSize())
|
|
|
|
aeIntv = aeIntv + lib.RandomStagger(aeIntv)
|
|
|
|
aeTimer := time.After(aeIntv)
|
2017-10-23 08:08:35 +00:00
|
|
|
|
|
|
|
// Wait for sync events
|
|
|
|
for {
|
|
|
|
select {
|
2017-10-23 08:08:35 +00:00
|
|
|
case <-aeTimer:
|
|
|
|
goto SYNC
|
|
|
|
case <-ae.TriggerCh:
|
|
|
|
// Skip the sync if we are paused
|
|
|
|
if ae.Paused() {
|
2017-10-23 08:08:35 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-10-23 08:08:35 +00:00
|
|
|
if err := ae.State.SyncChanges(); err != nil {
|
|
|
|
ae.Logger.Printf("[ERR] agent: failed to sync changes: %v", err)
|
2017-10-23 08:08:32 +00:00
|
|
|
}
|
2017-10-23 08:08:35 +00:00
|
|
|
case <-ae.ShutdownCh:
|
2017-10-23 08:08:35 +00:00
|
|
|
return
|
2017-08-28 12:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|