-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcccpcfgcontroller.go
227 lines (186 loc) · 5.66 KB
/
cccpcfgcontroller.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package gocbcore
import (
"errors"
"math/rand"
"sync"
"time"
"github.com/couchbase/gocbcore/v10/memd"
)
type cccpConfigController struct {
muxer dispatcher
cfgMgr *configManagementComponent
confCccpPollPeriod time.Duration
cccpFetcher *cccpConfigFetcher
looperStopSig chan struct{}
fetchErr error
errLock sync.Mutex
isFallbackErrorFn func(error) bool
noConfigFoundFn func(error)
}
func newCCCPConfigController(props cccpPollerProperties, muxer dispatcher, cfgMgr *configManagementComponent,
isFallbackErrorFn func(error) bool, noConfigFoundFn func(error)) *cccpConfigController {
return &cccpConfigController{
muxer: muxer,
cfgMgr: cfgMgr,
confCccpPollPeriod: props.confCccpPollPeriod,
cccpFetcher: props.cccpConfigFetcher,
looperStopSig: make(chan struct{}),
isFallbackErrorFn: isFallbackErrorFn,
noConfigFoundFn: noConfigFoundFn,
}
}
type cccpPollerProperties struct {
confCccpPollPeriod time.Duration
cccpConfigFetcher *cccpConfigFetcher
}
func (ccc *cccpConfigController) Error() error {
ccc.errLock.Lock()
defer ccc.errLock.Unlock()
return ccc.fetchErr
}
func (ccc *cccpConfigController) setError(err error) {
ccc.errLock.Lock()
ccc.fetchErr = err
ccc.errLock.Unlock()
}
func (ccc *cccpConfigController) Stop() {
logInfof("CCCP Looper stopping")
close(ccc.looperStopSig)
}
// Reset must never be called concurrently with the Stop or whilst the poll loop is running.
func (ccc *cccpConfigController) Reset() {
ccc.looperStopSig = make(chan struct{})
}
func (ccc *cccpConfigController) DoLoop() error {
if err := ccc.doLoop(); err != nil {
logInfof("CCCP Looper errored")
return err
}
logInfof("CCCP Looper stopped")
return nil
}
func (ccc *cccpConfigController) doLoop() error {
tickTime := ccc.confCccpPollPeriod
logInfof("CCCP Looper starting.")
nodeIdx := -1
// The first time that we loop we want to skip any sleep so that we can try get a config and bootstrapped ASAP.
firstLoop := true
for {
if !firstLoop {
// Wait for either the agent to be shut down, or our tick time to expire
select {
case <-ccc.looperStopSig:
return nil
case <-time.After(tickTime):
}
}
firstLoop = false
iter, err := ccc.muxer.PipelineSnapshot()
if err != nil {
// If we have an error it indicates the client is shut down.
break
}
numNodes := iter.NumPipelines()
if numNodes == 0 {
logInfof("CCCPPOLL: No nodes available to poll, returning upstream")
return errNoCCCPHosts
}
if nodeIdx < 0 || nodeIdx > numNodes {
nodeIdx = rand.Intn(numNodes) // #nosec G404
}
var foundConfig *cfgBucket
var configAlreadyLatest bool
var fallbackErr error
var wasCancelled bool
var numNodesSupportNotifs int
iter.Iterate(nodeIdx, func(pipeline *memdPipeline) bool {
nodeIdx = (nodeIdx + 1) % numNodes
if pipeline.SupportsFeature(memd.FeatureClustermapChangeNotificationBrief) {
numNodesSupportNotifs++
return false
}
cccpBytes, err := ccc.getClusterConfig(pipeline)
if err != nil {
if ccc.isFallbackErrorFn(err) {
fallbackErr = err
return false
}
// Only log the error at warn if it's unexpected.
// If we cancelled the request or we're shutting down the connection then it's not really unexpected.
if errors.Is(err, ErrRequestCanceled) || errors.Is(err, ErrShutdown) {
wasCancelled = true
logDebugf("CCCPPOLL: CCCP request was cancelled or connection was shutdown: %v", err)
return true
}
// This error is checked by WaitUntilReady when no config has been seen.
ccc.setError(err)
logWarnf("CCCPPOLL: Failed to retrieve CCCP config. %s", err)
return false
}
fallbackErr = nil
ccc.setError(nil)
if len(cccpBytes) > 0 {
logDebugf("CCCPPOLL: Got Block: %s", string(cccpBytes))
hostName, err := hostFromHostPort(pipeline.Address())
if err != nil {
logWarnf("CCCPPOLL: Failed to parse source address. %s", err)
return false
}
bk, err := parseConfig(cccpBytes, hostName)
if err != nil {
logWarnf("CCCPPOLL: Failed to parse CCCP config. %v", err)
return false
}
foundConfig = bk
} else {
configAlreadyLatest = true
}
return true
})
if fallbackErr != nil {
// This error is indicative of a memcached bucket which we can't handle so return the error.
logInfof("CCCPPOLL: CCCP not supported, returning error upstream.")
return fallbackErr
}
if numNodesSupportNotifs == numNodes {
continue
}
if configAlreadyLatest {
logDebugf("CCCPPOLL: Received empty config")
continue
}
if foundConfig == nil {
// Only log the error at warn if it's unexpected.
// If we cancelled the request then we're shutting down or request was requeued and this isn't unexpected.
if wasCancelled {
logDebugf("CCCPPOLL: CCCP request was cancelled.")
} else {
logWarnf("CCCPPOLL: Failed to retrieve config from any node.")
ccc.noConfigFoundFn(err)
}
continue
}
logDebugf("CCCPPOLL: Received new config")
ccc.cfgMgr.OnNewConfig(foundConfig)
}
return nil
}
func (ccc *cccpConfigController) getClusterConfig(pipeline *memdPipeline) ([]byte, error) {
revID, revEpoch := ccc.cfgMgr.CurrentRev()
cfg, err := ccc.cccpFetcher.GetClusterConfig(pipeline, revID, revEpoch, ccc.looperStopSig)
if err != nil {
if errors.Is(err, ErrTimeout) {
// We've timed out so lets check underlying connections to see if they're responsible.
clients := pipeline.Clients()
for _, cli := range clients {
err := cli.Error()
if err != nil {
logDebugf("Found error in pipeline client %p/%s: %v", cli, cli.address, err)
return nil, err
}
}
}
return nil, err
}
return cfg, nil
}