-
Notifications
You must be signed in to change notification settings - Fork 12
/
gocbcore_agents.go
256 lines (212 loc) · 7.59 KB
/
gocbcore_agents.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
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
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbgt
import (
"fmt"
"strconv"
"time"
log "github.com/couchbase/clog"
"github.com/couchbase/gocbcore/v10"
"github.com/couchbase/gocbcore/v10/memd"
)
const defaultOSOBackfillMode = false
const defaultInitialBootstrapNonTLS = true
var (
// GocbcoreConnectTimeout and GocbcoreKVConnectTimeout are timeouts used
// by gocbcore to connect to the cluster manager and KV
GocbcoreConnectTimeout = time.Duration(60 * time.Second)
GocbcoreKVConnectTimeout = time.Duration(7 * time.Second)
// GocbcoreAgentSetupTimeout is the time alloted for completing setup of
// a gocbcore.Agent or a gocbcore.DCPAgent, two factors ..
// - cluster state to be online
// - memcached service to be ready
GocbcoreAgentSetupTimeout = time.Duration(60 * time.Second)
// GocbcoreStatsTimeout is the time alloted to obtain a response from
// the server for a stats request.
GocbcoreStatsTimeout = time.Duration(60 * time.Second)
// GocbcoreConnectionBufferSize is the size of the buffer per connection
// allocated by gocbcore for receiving content from KV;
// overridden by kvConnectionBufferSize option.
GocbcoreConnectionBufferSize = uint(20 * 1024 * 1024) // 20MB (to match KV)
)
var errAgentSetupFailed = fmt.Errorf("agent setup failed")
// ----------------------------------------------------------------
// gocbcore's Agent it's AgentConfig
func setupAgentConfig(name, sourceName string,
auth gocbcore.AuthProvider, options map[string]string) *gocbcore.AgentConfig {
initialBootstrapNonTLS := defaultInitialBootstrapNonTLS
useCollections := true
connectionBufferSize := GocbcoreConnectionBufferSize
if options != nil {
if options["feedInitialBootstrapNonTLS"] == "true" {
initialBootstrapNonTLS = true
} else if options["feedInitialBootstrapNonTLS"] == "false" {
initialBootstrapNonTLS = false
}
if options["disableCollectionsSupport"] == "true" {
useCollections = false
}
if val, exists := options["kvConnectionBufferSize"]; exists {
// kvConnectionBufferSize is the buffer size to use for connections.
if valInt, err := strconv.Atoi(val); err == nil && valInt > 0 {
connectionBufferSize = uint(valInt)
}
}
}
return &gocbcore.AgentConfig{
UserAgent: name,
BucketName: sourceName,
SecurityConfig: gocbcore.SecurityConfig{
NoTLSSeedNode: initialBootstrapNonTLS,
Auth: auth,
},
IoConfig: gocbcore.IoConfig{
NetworkType: "default",
UseCollections: useCollections,
},
KVConfig: gocbcore.KVConfig{
ConnectTimeout: GocbcoreKVConnectTimeout,
ConnectionBufferSize: connectionBufferSize,
},
HTTPConfig: gocbcore.HTTPConfig{
MaxIdleConns: HttpTransportMaxIdleConns,
MaxIdleConnsPerHost: HttpTransportMaxIdleConnsPerHost,
ConnectTimeout: GocbcoreConnectTimeout,
},
}
}
func setupGocbcoreAgent(config *gocbcore.AgentConfig) (
*gocbcore.Agent, error) {
agent, err := gocbcore.CreateAgent(config)
if err != nil {
return nil, fmt.Errorf("%w, err: %v", errAgentSetupFailed, err)
}
options := gocbcore.WaitUntilReadyOptions{
DesiredState: gocbcore.ClusterStateOnline,
ServiceTypes: []gocbcore.ServiceType{gocbcore.MemdService},
RetryStrategy: &retryStrategy{},
}
signal := make(chan error, 1)
_, err = agent.WaitUntilReady(time.Now().Add(GocbcoreAgentSetupTimeout),
options, func(res *gocbcore.WaitUntilReadyResult, er error) {
signal <- er
})
if err == nil {
err = <-signal
}
if err != nil {
go agent.Close()
return nil, fmt.Errorf("%w, err: %v", errAgentSetupFailed, err)
}
log.Printf("gocbcore_utils: CreateAgent succeeded"+
" (agent: %p, bucket: %s, name: %s)",
agent, config.BucketName, config.UserAgent)
return agent, nil
}
// ----------------------------------------------------------------
// gocbcore's DCPAgent & it's DCPAgentConfig
func setupDCPAgentConfig(
name, bucketName string,
auth gocbcore.AuthProvider,
agentPriority gocbcore.DcpAgentPriority,
options map[string]string) *gocbcore.DCPAgentConfig {
config := &gocbcore.DCPAgentConfig{
UserAgent: name,
BucketName: bucketName,
SecurityConfig: gocbcore.SecurityConfig{
NoTLSSeedNode: defaultInitialBootstrapNonTLS,
Auth: auth,
},
IoConfig: gocbcore.IoConfig{
NetworkType: "default",
UseCollections: true,
},
KVConfig: gocbcore.KVConfig{
ConnectTimeout: GocbcoreKVConnectTimeout,
ConnectionBufferSize: GocbcoreConnectionBufferSize,
},
HTTPConfig: gocbcore.HTTPConfig{
MaxIdleConns: HttpTransportMaxIdleConns,
MaxIdleConnsPerHost: HttpTransportMaxIdleConnsPerHost,
ConnectTimeout: GocbcoreConnectTimeout,
},
DCPConfig: gocbcore.DCPConfig{
AgentPriority: agentPriority,
UseStreamID: true,
UseOSOBackfill: defaultOSOBackfillMode,
BackfillOrder: gocbcore.DCPBackfillOrderRoundRobin,
BufferSize: int(DCPFeedBufferSizeBytes),
},
}
if options["useOSOBackfill"] == "true" {
config.DCPConfig.UseOSOBackfill = true
} else if options["useOSOBackfill"] == "false" {
config.DCPConfig.UseOSOBackfill = false
}
if options["feedInitialBootstrapNonTLS"] == "true" {
config.SecurityConfig.NoTLSSeedNode = true
} else if options["feedInitialBootstrapNonTLS"] == "false" {
config.SecurityConfig.NoTLSSeedNode = false
}
if options["disableCollectionsSupport"] == "true" {
config.IoConfig.UseCollections = false
}
if options["disableStreamIDs"] == "true" {
config.DCPConfig.UseStreamID = false
}
if val, exists := options["kvConnectionWaitBackoff"]; exists {
// kvConnectionWaitBackoff is the period of time that the SDK will wait before
// reattempting connection to a node after bootstrap fails against that node.
if valDuration, err := time.ParseDuration(val); err == nil {
config.KVConfig.ServerWaitBackoff = valDuration
}
}
if val, exists := options["kvConnectionPoolSize"]; exists {
// kvConnectionPoolSize is the number of connections to create to each node.
if valInt, err := strconv.Atoi(val); err == nil && valInt > 0 {
config.KVConfig.PoolSize = valInt
}
}
if val, exists := options["kvConnectionBufferSize"]; exists {
// kvConnectionBufferSize is the buffer size to use for connections.
if valInt, err := strconv.Atoi(val); err == nil && valInt > 0 {
config.KVConfig.ConnectionBufferSize = uint(valInt)
}
}
return config
}
func setupGocbcoreDCPAgent(config *gocbcore.DCPAgentConfig,
connName string, flags memd.DcpOpenFlag) (
*gocbcore.DCPAgent, error) {
agent, err := gocbcore.CreateDcpAgent(config, connName, flags)
if err != nil {
return nil, fmt.Errorf("%w, err: %v", errAgentSetupFailed, err)
}
options := gocbcore.WaitUntilReadyOptions{
DesiredState: gocbcore.ClusterStateOnline,
ServiceTypes: []gocbcore.ServiceType{gocbcore.MemdService},
}
signal := make(chan error, 1)
_, err = agent.WaitUntilReady(time.Now().Add(GocbcoreAgentSetupTimeout),
options, func(res *gocbcore.WaitUntilReadyResult, er error) {
signal <- er
})
if err == nil {
err = <-signal
}
if err != nil {
log.Warnf("feed_dcp_gocbcore: CreateDcpAgent, err: %v (close DCPAgent: %p)",
err, agent)
go agent.Close()
return nil, fmt.Errorf("%w, err: %v", errAgentSetupFailed, err)
}
log.Printf("feed_dcp_gocbcore: CreateDcpAgent succeeded"+
" (agent: %p, bucketName: %s, name: %s)",
agent, config.BucketName, config.UserAgent)
return agent, nil
}