-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfigwatcher_memd.go
More file actions
184 lines (152 loc) · 4.22 KB
/
configwatcher_memd.go
File metadata and controls
184 lines (152 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package gocbcorex
import (
"context"
"sync"
"time"
"github.com/couchbase/gocbcorex/contrib/cbconfig"
"github.com/couchbase/gocbcorex/memdx"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)
type ConfigWatcherMemdConfig struct {
Endpoints []string
}
type ConfigWatcherMemdOptions struct {
Logger *zap.Logger
ClientProvider KvEndpointClientProvider
PollingPeriod time.Duration
}
type configWatcherMemdState struct {
endpoints []string
}
type ConfigWatcherMemd struct {
logger *zap.Logger
clientProvider KvEndpointClientProvider
pollingPeriod time.Duration
lock sync.Mutex
state *configWatcherMemdState
}
func NewConfigWatcherMemd(config *ConfigWatcherMemdConfig, opts *ConfigWatcherMemdOptions) (*ConfigWatcherMemd, error) {
return &ConfigWatcherMemd{
logger: opts.Logger,
clientProvider: opts.ClientProvider,
pollingPeriod: opts.PollingPeriod,
state: &configWatcherMemdState{
endpoints: config.Endpoints,
},
}, nil
}
func (w *ConfigWatcherMemd) Reconfigure(config *ConfigWatcherMemdConfig) error {
w.lock.Lock()
w.state = &configWatcherMemdState{
endpoints: config.Endpoints,
}
w.lock.Unlock()
return nil
}
func configWatcherMemd_pollOne(
ctx context.Context,
logger *zap.Logger,
clientProvider KvEndpointClientProvider,
endpoint string,
) (*ParsedConfig, error) {
client, err := clientProvider.GetEndpointClient(ctx, endpoint)
if err != nil {
return nil, err
}
logger.Debug("Polling for new config",
zap.String("endpoint", endpoint),
zap.String("endpoint", endpoint))
resp, err := client.GetClusterConfig(ctx, &memdx.GetClusterConfigRequest{})
if err != nil {
return nil, err
}
hostname := client.RemoteHostname()
config, err := cbconfig.ParseTerseConfig(resp.Config, hostname)
if err != nil {
return nil, err
}
logger.Debug("Poller fetched new config",
zap.Int("config", config.Rev),
zap.Int("configRevEpoch", config.RevEpoch))
parsedConfig, err := ConfigParser{}.ParseTerseConfig(config, hostname)
if err != nil {
return nil, err
}
return parsedConfig, nil
}
func (w *ConfigWatcherMemd) watchThread(ctx context.Context, outCh chan<- *ParsedConfig) {
var lastSentConfig *ParsedConfig
var recentEndpoints []string
allEndpointsFailed := true
for ctx.Err() == nil {
w.lock.Lock()
state := w.state
w.lock.Unlock()
// if there are no endpoints to poll, we need to sleep and wait
if len(state.endpoints) == 0 {
select {
case <-time.After(w.pollingPeriod):
case <-ctx.Done():
}
continue
}
// remove the most recently polled endpoints
var remainingEndpoints []string
for _, endpoint := range state.endpoints {
if !slices.Contains(recentEndpoints, endpoint) {
remainingEndpoints = append(remainingEndpoints, endpoint)
}
}
// if there are no endpoints left, we reset the lists
if len(remainingEndpoints) == 0 {
if allEndpointsFailed {
// if all the endpoints failed in a row, we do a sleep to ensure
// we don't loop for no reason
select {
case <-time.After(w.pollingPeriod):
case <-ctx.Done():
}
}
recentEndpoints = nil
allEndpointsFailed = true
continue
}
endpoint := remainingEndpoints[0]
recentEndpoints = append(recentEndpoints, endpoint)
pollCtx, cancel := context.WithTimeout(ctx, 2500*time.Millisecond)
parsedConfig, err := configWatcherMemd_pollOne(
pollCtx,
w.logger,
w.clientProvider,
endpoint)
cancel()
if err != nil {
w.logger.Debug("failed to poll config via cccp",
zap.Error(err),
zap.String("endpoint", endpoint))
continue
}
allEndpointsFailed = false
// we do some deduplication here to avoid spamming consumers with logs
// with this implementation which polls rather than streams.
if lastSentConfig != nil && parsedConfig.Compare(lastSentConfig) <= 0 {
// we already dispatched an identical config
} else {
outCh <- parsedConfig
lastSentConfig = parsedConfig
}
// after successfully receiving a configuration, we wait 5 seconds
// before polling the next server.
select {
case <-time.After(w.pollingPeriod):
case <-ctx.Done():
}
}
close(outCh)
}
func (w *ConfigWatcherMemd) Watch(ctx context.Context) <-chan *ParsedConfig {
outCh := make(chan *ParsedConfig, 1)
go w.watchThread(ctx, outCh)
return outCh
}