forked from couchbase/go-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpools.go
More file actions
318 lines (275 loc) · 8.05 KB
/
pools.go
File metadata and controls
318 lines (275 loc) · 8.05 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package couchbase
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"runtime"
"sort"
"strings"
)
// The HTTP Client To Use
var HttpClient = http.DefaultClient
// Auth callback gets the auth username and password for the given
// bucket.
type AuthHandler interface {
GetCredentials() (string, string)
}
type RestPool struct {
Name string `json:"name"`
StreamingURI string `json:"streamingUri"`
URI string `json:"uri"`
}
type Pools struct {
ComponentsVersion map[string]string `json:"componentsVersion,omitempty"`
ImplementationVersion string `json:"implementationVersion"`
IsAdmin bool `json:"isAdminCreds"`
UUID string `json:"uuid"`
Pools []RestPool `json:"pools"`
}
// A computer in a cluster running the couchbase software.
type Node struct {
ClusterCompatibility int `json:"clusterCompatibility"`
ClusterMembership string `json:"clusterMembership"`
CouchAPIBase string `json:"couchApiBase"`
Hostname string `json:"hostname"`
InterestingStats map[string]float64 `json:"interestingStats,omitempty"`
MCDMemoryAllocated float64 `json:"mcdMemoryAllocated"`
MCDMemoryReserved float64 `json:"mcdMemoryReserved"`
MemoryFree float64 `json:"memoryFree"`
MemoryTotal float64 `json:"memoryTotal"`
OS string `json:"os"`
Ports map[string]int `json:"ports"`
Status string `json:"status"`
Uptime int `json:"uptime,string"`
Version string `json:"version"`
ThisNode bool `json:"thisNode,omitempty"`
}
// A pool of nodes and buckets.
type Pool struct {
BucketMap map[string]Bucket
Nodes []Node
BucketURL map[string]string `json:"buckets"`
client Client
}
// An individual bucket. Herein lives the most useful stuff.
type Bucket struct {
AuthType string `json:"authType"`
Capabilities []string `json:"bucketCapabilities"`
CapabilitiesVersion string `json:"bucketCapabilitiesVer"`
Type string `json:"bucketType"`
Name string `json:"name"`
NodeLocator string `json:"nodeLocator"`
Nodes []Node `json:"nodes"`
Quota map[string]float64 `json:"quota,omitempty"`
Replicas int `json:"replicaNumber"`
Password string `json:"saslPassword"`
URI string `json:"uri"`
StreamingURI string `json:"streamingUri"`
LocalRandomKeyURI string `json:"localRandomKeyUri,omitempty"`
UUID string `json:"uuid"`
DDocs struct {
URI string `json:"uri"`
} `json:"ddocs,omitempty"`
VBucketServerMap struct {
HashAlgorithm string `json:"hashAlgorithm"`
NumReplicas int `json:"numReplicas"`
ServerList []string `json:"serverList"`
VBucketMap [][]int `json:"vBucketMap"`
} `json:"vBucketServerMap"`
BasicStats map[string]interface{} `json:"basicStats,omitempty"`
Controllers map[string]interface{} `json:"controllers,omitempty"`
pool *Pool
connections []*connectionPool
commonSufix string
auth AuthHandler
}
// Get the (sorted) list of memcached node addresses (hostname:port).
func (b Bucket) NodeAddresses() []string {
rv := make([]string, len(b.VBucketServerMap.ServerList))
copy(rv, b.VBucketServerMap.ServerList)
sort.Strings(rv)
return rv
}
// Get the longest common suffix of all host:port strings in the node list.
func (b Bucket) CommonAddressSuffix() string {
input := []string{}
for _, n := range b.Nodes {
input = append(input, n.Hostname)
}
return FindCommonSuffix(input)
}
// The couchbase client gives access to all the things.
type Client struct {
BaseURL *url.URL
Info Pools
Statuses [256]uint64
}
func maybeAddAuth(req *http.Request, ah AuthHandler) {
if ah != nil {
user, pass := ah.GetCredentials()
req.Header.Set("Authorization", "Basic "+
base64.StdEncoding.EncodeToString([]byte(user+":"+pass)))
}
}
func (c *Client) parseURLResponse(path string, ah AuthHandler,
out interface{}) error {
u := *c.BaseURL
u.User = nil
if q := strings.Index(path, "?"); q > 0 {
u.Path = path[:q]
u.RawQuery = path[q+1:]
} else {
u.Path = path
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
maybeAddAuth(req, ah)
res, err := HttpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
bod, _ := ioutil.ReadAll(io.LimitReader(res.Body, 512))
return fmt.Errorf("HTTP error %v getting %q: %s",
res.Status, u.String(), bod)
}
d := json.NewDecoder(res.Body)
if err = d.Decode(&out); err != nil {
return err
}
return nil
}
type basicAuth struct {
u, p string
}
func (b basicAuth) GetCredentials() (string, string) {
return b.u, b.p
}
// Connect to a couchbase cluster.
func Connect(baseU string) (c Client, err error) {
c.BaseURL, err = url.Parse(baseU)
if err != nil {
return
}
return c, c.parseURLResponse("/pools",
basicAuthFromURL(c.BaseURL), &c.Info)
}
func (b *Bucket) refresh() (err error) {
pool := b.pool
err = pool.client.parseURLResponse(b.URI, b.auth, b)
if err != nil {
return err
}
b.pool = pool
for i := range b.connections {
b.connections[i] = newConnectionPool(
b.VBucketServerMap.ServerList[i], b.auth, 4)
}
return nil
}
func basicAuthFromURL(u *url.URL) (ah AuthHandler) {
if user := u.User; user != nil {
pw, _ := user.Password()
ah = basicAuth{user.Username(), pw}
}
return
}
func (p *Pool) refresh() (err error) {
p.BucketMap = make(map[string]Bucket)
ah := basicAuthFromURL(p.client.BaseURL)
buckets := []Bucket{}
err = p.client.parseURLResponse(p.BucketURL["uri"], ah, &buckets)
if err != nil {
return err
}
for _, b := range buckets {
b.pool = p
b.auth = p.getDefaultAuth(b.Name)
b.connections = make([]*connectionPool, len(b.VBucketServerMap.ServerList))
p.BucketMap[b.Name] = b
}
return nil
}
// Get a pool from within the couchbase cluster (usually "default").
func (c *Client) GetPool(name string) (p Pool, err error) {
var poolURI string
for _, p := range c.Info.Pools {
if p.Name == name {
poolURI = p.URI
}
}
if poolURI == "" {
return p, errors.New("No pool named " + name)
}
err = c.parseURLResponse(poolURI, basicAuthFromURL(c.BaseURL), &p)
p.client = *c
err = p.refresh()
return
}
// Mark this bucket as no longer needed, closing connections it may have open.
func (b *Bucket) Close() {
if b.connections != nil {
for _, c := range b.connections {
if c != nil {
c.Close()
}
}
b.connections = nil
}
}
func bucket_finalizer(b *Bucket) {
if b.connections != nil {
log.Printf("Warning: Finalizing a bucket with active connections.")
}
}
func (p *Pool) getDefaultAuth(name string) AuthHandler {
var pw string
if p.client.BaseURL.User != nil {
pw, _ = p.client.BaseURL.User.Password()
}
return &basicAuth{name, pw}
}
// Get a bucket from within this pool.
func (p *Pool) GetBucket(name string) (*Bucket, error) {
rv, ok := p.BucketMap[name]
if !ok {
return nil, errors.New("No bucket named " + name)
}
runtime.SetFinalizer(&rv, bucket_finalizer)
err := rv.refresh()
if err != nil {
return nil, err
}
rv.auth = p.getDefaultAuth(name)
return &rv, nil
}
// Get the pool to which this bucket belongs.
func (b *Bucket) GetPool() *Pool {
return b.pool
}
// Get the client from which we got this pool.
func (p *Pool) GetClient() *Client {
return &p.client
}
// Convenience function for getting a named bucket from a URL
func GetBucket(endpoint, poolname, bucketname string) (*Bucket, error) {
var err error
client, err := Connect(endpoint)
if err != nil {
return nil, err
}
pool, err := client.GetPool(poolname)
if err != nil {
return nil, err
}
return pool.GetBucket(bucketname)
}