-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkvendpointclientmanager.go
More file actions
260 lines (207 loc) · 6.22 KB
/
kvendpointclientmanager.go
File metadata and controls
260 lines (207 loc) · 6.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package gocbcorex
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
"go.uber.org/zap"
)
type KvEndpointClientManager interface {
KvClientProvider
KvEndpointClientProvider
UpdateEndpoints(endpoints map[string]KvTarget, addOnly bool)
UpdateAuth(newAuth KvClientAuth)
UpdateSelectedBucket(newBucket string)
Close() error
}
type NewKvClientPoolFunc func(opts *KvClientPoolOptions) (KvClientPool, error)
type KvEndpointClientManagerOptions struct {
Logger *zap.Logger
NewKvClientPool NewKvClientPoolFunc
OnCloseHandler func(KvEndpointClientManager)
NumPoolConnections uint
OnDemandConnect bool
ConnectTimeout time.Duration
ConnectErrThrottlePeriod time.Duration
BootstrapOpts KvClientBootstrapOptions
Endpoints map[string]KvTarget
Auth KvClientAuth
SelectedBucket string
}
type kvEndpointClientManagerState struct {
ClientPools map[string]KvClientPool
}
type kvEndpointClientManager struct {
logger *zap.Logger
newKvClientPool NewKvClientPoolFunc
onCloseHandler func(KvEndpointClientManager)
numPoolConnections uint
onDemandConnect bool
connectTimeout time.Duration
connectErrThrottlePeriod time.Duration
bootstrapOpts KvClientBootstrapOptions
lock sync.Mutex
auth KvClientAuth
selectedBucket string
clientPools map[string]KvClientPool
state atomic.Pointer[kvEndpointClientManagerState]
}
var _ (KvEndpointClientManager) = (*kvEndpointClientManager)(nil)
func NewKvEndpointClientManager(
opts *KvEndpointClientManagerOptions,
) (KvEndpointClientManager, error) {
if opts == nil {
opts = &KvEndpointClientManagerOptions{}
}
if opts.NumPoolConnections == 0 {
opts.NumPoolConnections = 1
}
logger := loggerOrNop(opts.Logger)
// We namespace the pool to improve debugging,
logger = logger.With(
zap.String("mgrId", uuid.NewString()[:8]),
)
newKvClientPool := opts.NewKvClientPool
if newKvClientPool == nil {
newKvClientPool = NewKvClientPool
}
mgr := &kvEndpointClientManager{
logger: logger,
newKvClientPool: newKvClientPool,
onCloseHandler: opts.OnCloseHandler,
numPoolConnections: opts.NumPoolConnections,
onDemandConnect: opts.OnDemandConnect,
connectTimeout: opts.ConnectTimeout,
connectErrThrottlePeriod: opts.ConnectErrThrottlePeriod,
bootstrapOpts: opts.BootstrapOpts,
clientPools: make(map[string]KvClientPool),
}
mgr.auth = opts.Auth
mgr.selectedBucket = opts.SelectedBucket
mgr.UpdateEndpoints(opts.Endpoints, false)
return mgr, nil
}
func (m *kvEndpointClientManager) UpdateEndpoints(endpoints map[string]KvTarget, addOnly bool) {
m.lock.Lock()
defer m.lock.Unlock()
oldPools := make(map[string]KvClientPool)
for poolKey, pool := range m.clientPools {
oldPools[poolKey] = pool
}
newPools := make(map[string]KvClientPool)
for endpoint, target := range endpoints {
var pool KvClientPool
oldPool := oldPools[endpoint]
if oldPool != nil {
oldPool.UpdateTarget(target)
pool = oldPool
delete(oldPools, endpoint)
} else {
newPool, err := m.newKvClientPool(&KvClientPoolOptions{
Logger: m.logger.Named("pool"),
NumConnections: m.numPoolConnections,
OnDemandConnect: m.onDemandConnect,
ConnectTimeout: m.connectTimeout,
ConnectErrThrottlePeriod: m.connectErrThrottlePeriod,
BootstrapOpts: m.bootstrapOpts,
Target: target,
Auth: m.auth,
SelectedBucket: m.selectedBucket,
})
if err != nil {
m.logger.Debug("failed to create pool", zap.Error(err))
continue
}
pool = newPool
}
newPools[endpoint] = pool
}
if addOnly {
// in add-only mode, we keep any existing pools that aren't in the new set
// this is useful for making sure all routers still work until we've updated
// the routers separately...
for endpoint, pool := range oldPools {
newPools[endpoint] = pool
delete(oldPools, endpoint)
}
}
for _, pool := range oldPools {
if err := pool.Close(); err != nil {
m.logger.Debug("failed to close pool", zap.Error(err))
}
}
m.clientPools = newPools
m.state.Store(&kvEndpointClientManagerState{
ClientPools: newPools,
})
}
func (m *kvEndpointClientManager) UpdateAuth(newAuth KvClientAuth) {
m.lock.Lock()
defer m.lock.Unlock()
for _, pool := range m.clientPools {
pool.UpdateAuth(newAuth)
}
}
func (m *kvEndpointClientManager) UpdateSelectedBucket(newBucket string) {
m.lock.Lock()
defer m.lock.Unlock()
for _, pool := range m.clientPools {
pool.UpdateSelectedBucket(newBucket)
}
}
func (m *kvEndpointClientManager) getState() (*kvEndpointClientManagerState, error) {
state := m.state.Load()
if state == nil {
return nil, errors.New("kv endpoint client manager is closed")
}
return state, nil
}
func (m *kvEndpointClientManager) GetClient(ctx context.Context) (KvClient, error) {
state, err := m.getState()
if err != nil {
return nil, err
}
// Just pick one at random for now
for _, pool := range state.ClientPools {
return pool.GetClient(ctx)
}
return nil, ErrInvalidEndpoint
}
func (m *kvEndpointClientManager) GetEndpointClient(ctx context.Context, endpoint string) (KvClient, error) {
if endpoint == "" {
return nil, placeholderError{"endpoint must be specified for GetEndpoint"}
}
state, err := m.getState()
if err != nil {
return nil, err
}
pool, ok := state.ClientPools[endpoint]
if !ok {
var validKeys []string
for validEndpoint := range state.ClientPools {
validKeys = append(validKeys, validEndpoint)
}
return nil, placeholderError{fmt.Sprintf("endpoint not known `%s` in %+v", endpoint, validKeys)}
}
return pool.GetClient(ctx)
}
func (m *kvEndpointClientManager) Close() error {
m.logger.Info("closing kv endpoint client manager")
m.state.Store(nil)
m.lock.Lock()
defer m.lock.Unlock()
for _, pool := range m.clientPools {
if err := pool.Close(); err != nil {
m.logger.Debug("Failed to close kv client pool", zap.Error(err))
}
}
m.clientPools = make(map[string]KvClientPool)
if m.onCloseHandler != nil {
m.onCloseHandler(m)
}
m.logger.Info("closed kv endpoint client manager")
return nil
}