2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
2019-01-08 16:43:14 +00:00
|
|
|
"context"
|
2014-08-20 20:45:34 +00:00
|
|
|
"fmt"
|
2020-01-28 23:50:41 +00:00
|
|
|
"io"
|
2014-08-20 20:45:34 +00:00
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2022-10-21 19:58:06 +00:00
|
|
|
|
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2014-08-20 20:45:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// retryInterval is the base retry value
|
|
|
|
retryInterval = 5 * time.Second
|
|
|
|
|
|
|
|
// maximum back off time, this is to prevent
|
|
|
|
// exponential runaway
|
|
|
|
maxBackoffTime = 180 * time.Second
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
// Name used with hclog Logger. We do not add this to the logging package
|
|
|
|
// because we do not want to pull in the root consul module.
|
|
|
|
watchLoggerName = "watch"
|
2014-08-20 20:45:34 +00:00
|
|
|
)
|
|
|
|
|
2018-06-01 00:22:14 +00:00
|
|
|
func (p *Plan) Run(address string) error {
|
|
|
|
return p.RunWithConfig(address, nil)
|
|
|
|
}
|
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
// Run is used to run a watch plan
|
2018-06-01 00:22:14 +00:00
|
|
|
func (p *Plan) RunWithConfig(address string, conf *consulapi.Config) error {
|
2020-07-29 18:33:36 +00:00
|
|
|
logger := p.Logger
|
|
|
|
if logger == nil {
|
|
|
|
logger = newWatchLogger(p.LogOutput)
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
// Setup the client
|
|
|
|
p.address = address
|
2018-05-31 21:07:36 +00:00
|
|
|
if conf == nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
conf = consulapi.DefaultConfigWithLogger(logger)
|
2018-05-31 21:07:36 +00:00
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
conf.Address = address
|
|
|
|
conf.Datacenter = p.Datacenter
|
2014-08-20 23:45:37 +00:00
|
|
|
conf.Token = p.Token
|
2014-08-20 20:45:34 +00:00
|
|
|
client, err := consulapi.NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to connect to agent: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
return p.RunWithClientAndHclog(client, logger)
|
2018-04-05 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunWithClientAndLogger runs a watch plan using an external client and
|
2020-01-28 23:50:41 +00:00
|
|
|
// hclog.Logger instance. Using this, the plan's Datacenter, Token and LogOutput
|
2018-04-05 16:15:43 +00:00
|
|
|
// fields are ignored and the passed client is expected to be configured as
|
|
|
|
// needed.
|
2020-01-28 23:50:41 +00:00
|
|
|
func (p *Plan) RunWithClientAndHclog(client *consulapi.Client, logger hclog.Logger) error {
|
|
|
|
var watchLogger hclog.Logger
|
|
|
|
if logger == nil {
|
|
|
|
watchLogger = newWatchLogger(nil)
|
|
|
|
} else {
|
|
|
|
watchLogger = logger.Named(watchLoggerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.client = client
|
|
|
|
|
|
|
|
// Loop until we are canceled
|
|
|
|
failures := 0
|
|
|
|
OUTER:
|
|
|
|
for !p.shouldStop() {
|
|
|
|
// Invoke the handler
|
|
|
|
blockParamVal, result, err := p.Watcher(p)
|
|
|
|
|
|
|
|
// Check if we should terminate since the function
|
|
|
|
// could have blocked for a while
|
|
|
|
if p.shouldStop() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle an error in the watch function
|
|
|
|
if err != nil {
|
|
|
|
// Perform an exponential backoff
|
|
|
|
failures++
|
|
|
|
if blockParamVal == nil {
|
|
|
|
p.lastParamVal = nil
|
|
|
|
} else {
|
|
|
|
p.lastParamVal = blockParamVal.Next(p.lastParamVal)
|
|
|
|
}
|
|
|
|
retry := retryInterval * time.Duration(failures*failures)
|
|
|
|
if retry > maxBackoffTime {
|
|
|
|
retry = maxBackoffTime
|
|
|
|
}
|
|
|
|
watchLogger.Error("Watch errored", "type", p.Type, "error", err, "retry", retry)
|
|
|
|
select {
|
|
|
|
case <-time.After(retry):
|
|
|
|
continue OUTER
|
|
|
|
case <-p.stopCh:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the failures
|
|
|
|
failures = 0
|
|
|
|
|
|
|
|
// If the index is unchanged do nothing
|
|
|
|
if p.lastParamVal != nil && p.lastParamVal.Equal(blockParamVal) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the index, look for change
|
|
|
|
oldParamVal := p.lastParamVal
|
|
|
|
p.lastParamVal = blockParamVal.Next(oldParamVal)
|
|
|
|
if oldParamVal != nil && reflect.DeepEqual(p.lastResult, result) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the updated result
|
|
|
|
p.lastResult = result
|
|
|
|
// If a hybrid handler exists use that
|
|
|
|
if p.HybridHandler != nil {
|
|
|
|
p.HybridHandler(blockParamVal, result)
|
|
|
|
} else if p.Handler != nil {
|
|
|
|
idx, ok := blockParamVal.(WaitIndexVal)
|
|
|
|
if !ok {
|
|
|
|
watchLogger.Error("Handler only supports index-based " +
|
|
|
|
" watches but non index-based watch run. Skipping Handler.")
|
|
|
|
}
|
|
|
|
p.Handler(uint64(idx), result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:58:06 +00:00
|
|
|
// Deprecated: Use RunwithClientAndHclog
|
2020-01-28 23:50:41 +00:00
|
|
|
func (p *Plan) RunWithClientAndLogger(client *consulapi.Client, logger *log.Logger) error {
|
2018-04-05 16:15:43 +00:00
|
|
|
|
|
|
|
p.client = client
|
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
// Loop until we are canceled
|
|
|
|
failures := 0
|
2014-08-20 22:18:08 +00:00
|
|
|
OUTER:
|
2014-08-20 20:45:34 +00:00
|
|
|
for !p.shouldStop() {
|
|
|
|
// Invoke the handler
|
2018-04-05 16:15:43 +00:00
|
|
|
blockParamVal, result, err := p.Watcher(p)
|
2014-08-20 20:45:34 +00:00
|
|
|
|
|
|
|
// Check if we should terminate since the function
|
|
|
|
// could have blocked for a while
|
|
|
|
if p.shouldStop() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle an error in the watch function
|
|
|
|
if err != nil {
|
|
|
|
// Perform an exponential backoff
|
|
|
|
failures++
|
2018-04-05 16:15:43 +00:00
|
|
|
if blockParamVal == nil {
|
|
|
|
p.lastParamVal = nil
|
|
|
|
} else {
|
|
|
|
p.lastParamVal = blockParamVal.Next(p.lastParamVal)
|
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
retry := retryInterval * time.Duration(failures*failures)
|
|
|
|
if retry > maxBackoffTime {
|
|
|
|
retry = maxBackoffTime
|
|
|
|
}
|
2018-03-21 15:56:14 +00:00
|
|
|
logger.Printf("[ERR] consul.watch: Watch (type: %s) errored: %v, retry in %v",
|
2014-08-21 20:09:13 +00:00
|
|
|
p.Type, err, retry)
|
2014-08-20 20:45:34 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(retry):
|
2014-08-20 22:18:08 +00:00
|
|
|
continue OUTER
|
2014-08-20 20:45:34 +00:00
|
|
|
case <-p.stopCh:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the failures
|
|
|
|
failures = 0
|
|
|
|
|
|
|
|
// If the index is unchanged do nothing
|
2018-04-05 16:15:43 +00:00
|
|
|
if p.lastParamVal != nil && p.lastParamVal.Equal(blockParamVal) {
|
2014-08-20 20:45:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the index, look for change
|
2018-04-05 16:15:43 +00:00
|
|
|
oldParamVal := p.lastParamVal
|
|
|
|
p.lastParamVal = blockParamVal.Next(oldParamVal)
|
|
|
|
if oldParamVal != nil && reflect.DeepEqual(p.lastResult, result) {
|
2014-08-20 20:45:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the updated result
|
|
|
|
p.lastResult = result
|
2018-04-25 19:41:26 +00:00
|
|
|
// If a hybrid handler exists use that
|
|
|
|
if p.HybridHandler != nil {
|
|
|
|
p.HybridHandler(blockParamVal, result)
|
|
|
|
} else if p.Handler != nil {
|
|
|
|
idx, ok := blockParamVal.(WaitIndexVal)
|
|
|
|
if !ok {
|
|
|
|
logger.Printf("[ERR] consul.watch: Handler only supports index-based " +
|
|
|
|
" watches but non index-based watch run. Skipping Handler.")
|
|
|
|
}
|
|
|
|
p.Handler(uint64(idx), result)
|
2014-08-20 22:18:08 +00:00
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is used to stop running the watch plan
|
2017-04-21 00:46:29 +00:00
|
|
|
func (p *Plan) Stop() {
|
2014-08-20 20:45:34 +00:00
|
|
|
p.stopLock.Lock()
|
|
|
|
defer p.stopLock.Unlock()
|
|
|
|
if p.stop {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.stop = true
|
2017-06-26 20:52:03 +00:00
|
|
|
if p.cancelFunc != nil {
|
|
|
|
p.cancelFunc()
|
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
close(p.stopCh)
|
|
|
|
}
|
|
|
|
|
2017-04-21 00:46:29 +00:00
|
|
|
func (p *Plan) shouldStop() bool {
|
2014-08-20 20:45:34 +00:00
|
|
|
select {
|
|
|
|
case <-p.stopCh:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 19:52:41 +00:00
|
|
|
|
2019-01-08 16:43:14 +00:00
|
|
|
func (p *Plan) setCancelFunc(cancel context.CancelFunc) {
|
|
|
|
p.stopLock.Lock()
|
|
|
|
defer p.stopLock.Unlock()
|
|
|
|
if p.shouldStop() {
|
|
|
|
// The watch is stopped and execute the new cancel func to stop watchFactory
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.cancelFunc = cancel
|
|
|
|
}
|
|
|
|
|
2017-06-24 19:52:41 +00:00
|
|
|
func (p *Plan) IsStopped() bool {
|
|
|
|
p.stopLock.Lock()
|
|
|
|
defer p.stopLock.Unlock()
|
|
|
|
return p.stop
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
func newWatchLogger(output io.Writer) hclog.Logger {
|
|
|
|
return hclog.New(&hclog.LoggerOptions{
|
|
|
|
Name: watchLoggerName,
|
|
|
|
Output: output,
|
|
|
|
})
|
|
|
|
}
|