-
Notifications
You must be signed in to change notification settings - Fork 5
/
configbootstrap_http.go
156 lines (136 loc) · 3.63 KB
/
configbootstrap_http.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
package gocbcorex
import (
"context"
"errors"
"net/http"
"time"
"github.com/couchbase/gocbcorex/cbmgmtx"
"go.uber.org/zap"
)
type ConfigBoostrapHttpOptions struct {
Logger *zap.Logger
HttpRoundTripper http.RoundTripper
Endpoints []string
UserAgent string
Authenticator Authenticator
BucketName string
}
type ConfigBootstrapHttp struct {
logger *zap.Logger
httpRoundTripper http.RoundTripper
endpoints []string
userAgent string
authenticator Authenticator
bucketName string
}
func NewConfigBootstrapHttp(opts ConfigBoostrapHttpOptions) (*ConfigBootstrapHttp, error) {
return &ConfigBootstrapHttp{
logger: opts.Logger,
httpRoundTripper: opts.HttpRoundTripper,
endpoints: opts.Endpoints,
userAgent: opts.UserAgent,
authenticator: opts.Authenticator,
bucketName: opts.BucketName,
}, nil
}
func configBootstrapHttp_bootstrapOne(
ctx context.Context,
httpRoundTripper http.RoundTripper,
endpoint string,
userAgent string,
authenticator Authenticator,
bucketName string,
) (*ParsedConfig, string, error) {
hostport, err := getHostFromUri(endpoint)
if err != nil {
return nil, "", err
}
var parsedConfig *ParsedConfig
for {
username, password, err := authenticator.GetCredentials(ServiceTypeMgmt, hostport)
if err != nil {
return nil, "", err
}
hostOnly, err := hostFromHostPort(hostport)
if err != nil {
return nil, "", err
}
if bucketName == "" {
resp, err := cbmgmtx.Management{
Transport: httpRoundTripper,
UserAgent: userAgent,
Endpoint: endpoint,
Username: username,
Password: password,
}.GetTerseClusterConfig(ctx, &cbmgmtx.GetTerseClusterConfigOptions{})
if err != nil {
return nil, "", err
}
parsedConfig, err = ConfigParser{}.ParseTerseConfig(resp, hostOnly)
if err != nil {
return nil, "", err
}
} else {
resp, err := cbmgmtx.Management{
Transport: httpRoundTripper,
UserAgent: userAgent,
Endpoint: endpoint,
Username: username,
Password: password,
}.GetTerseBucketConfig(ctx, &cbmgmtx.GetTerseBucketConfigOptions{
BucketName: bucketName,
})
if err != nil {
return nil, "", err
}
parsedConfig, err = ConfigParser{}.ParseTerseConfig(resp, hostOnly)
if err != nil {
return nil, "", err
}
}
if parsedConfig.BucketType == bktTypeCouchbase && parsedConfig.VbucketMap == nil {
// This is a transient scenario that can occur when a bucket is initially warming
// up. Instead of failing bootstrap for this, we instead sleep for a bit and then
// try bootstrapping again.
select {
case <-ctx.Done():
err := ctx.Err()
if errors.Is(err, context.DeadlineExceeded) {
return nil, "", &contextualDeadline{"bucket warming up and has no vbucket map"}
} else {
return nil, "", err
}
case <-time.After(1 * time.Millisecond):
}
continue
}
break
}
networkType := NetworkTypeHeuristic{}.Identify(parsedConfig, hostport)
return parsedConfig, networkType, nil
}
func (w ConfigBootstrapHttp) Bootstrap(ctx context.Context) (*ParsedConfig, string, error) {
attemptErrs := make(map[string]error)
for _, endpoint := range w.endpoints {
var err error
parsedConfig, networkType, err := configBootstrapHttp_bootstrapOne(
ctx,
w.httpRoundTripper,
endpoint,
w.userAgent,
w.authenticator,
w.bucketName,
)
if err != nil {
if errors.Is(err, cbmgmtx.ErrBucketNotFound) {
return nil, "", err
}
attemptErrs[endpoint] = err
continue
}
return parsedConfig, networkType, nil
}
return nil, "", &BootstrapAllFailedError{
Errors: attemptErrs,
}
}