/
metakv.go
423 lines (368 loc) · 11.2 KB
/
metakv.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// @author Couchbase <info@couchbase.com>
// @copyright 2014 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package metakv provides simple KV API to some "metadata store". In
// current implementation it is metadata store that keeps values in
// ns_server's ns_config.
//
// Sub-namespace that is under directory "/checkpoints/" is going to
// be implemented specially. It is intended for xdcr checkpoints and
// will not be replicated between nodes in first version of this
// package.
package metakv
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/couchbase/cbauth"
"github.com/couchbase/cbauth/utils"
)
// ErrRevMismatch error is returned from Set and Delete when there is
// rev mismatch
var ErrRevMismatch = errors.New("Rev mismatch")
var errNotFound = errors.New("Not found")
// KVEntry struct represents kv entry returned from ListAllChildren and
// used as a parameter in Callback
type KVEntry struct {
Path string
Value []byte
Rev interface{}
Sensitive bool
}
// Callback type describes functions that receive mutations from
// RunObserveChildren.
type Callback func(entry KVEntry) error
// RevCreate is a special revision which when passed to Set will change
// Set to Add.
var RevCreate = &struct{}{}
type store struct {
url *url.URL
client *http.Client
}
const uaMetaKvSuffix = "metakv"
const uaMetaKvVersion = ""
var userAgent = utils.MakeUserAgent(uaMetaKvSuffix, uaMetaKvVersion)
var defaultStore = initDefaultStore()
func initDefaultStore() *store {
c := *http.DefaultClient
c.Transport = cbauth.WrapHTTPTransport(http.DefaultTransport, nil)
authURL := os.Getenv("CBAUTH_REVRPC_URL")
u, err := url.Parse(authURL)
if err == nil {
u.RawQuery = ""
u.Fragment = ""
u.Path = "/_metakv"
u.User = nil
}
return &store{url: u, client: &c}
}
func doCallInner(s *store, method, path string, values url.Values) (resp *http.Response, err error) {
var body io.Reader
if method == "PUT" && values != nil {
body = strings.NewReader(values.Encode())
}
url := *s.url
url.Path += path
req, err := http.NewRequest(method, url.String(), body)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
if method != "PUT" && values != nil {
req.URL.RawQuery = values.Encode()
}
req.Header.Set("User-Agent", userAgent)
r, err := s.client.Do(req)
if err != nil {
return r, err
}
if r.StatusCode == http.StatusConflict {
return r, ErrRevMismatch
}
if r.StatusCode == http.StatusNotFound {
return r, errNotFound
}
if r.StatusCode != 200 {
return r, fmt.Errorf("ns_server _metakv returned: %s", r.Status)
}
return r, err
}
func doCall(s *store, method, path string, values url.Values) (body []byte, err error) {
r, err := doCallInner(s, method, path, values)
if r != nil {
defer r.Body.Close()
}
if err != nil {
return nil, err
}
return ioutil.ReadAll(r.Body)
}
func doJSONCall(s *store, method, path string, values url.Values, place interface{}) error {
body, err := doCall(s, method, path, values)
if err != nil {
return err
}
return json.Unmarshal(body, place)
}
type kvEntry struct {
Path string
Value []byte
Rev []byte
Sensitive bool
}
type callbackInt func(entry kvEntry) error
func assertValidPathPrefix(path string) {
if path[0] != '/' {
panic("path must begin with /")
}
}
func assertValidPath(path string) {
assertValidPathPrefix(path)
if strings.HasSuffix(path, "/") {
panic("path must not end with \"/\"")
}
}
func assertValidDirPath(path string) {
assertValidPathPrefix(path)
if !strings.HasSuffix(path, "/") {
panic("dirpath must end with \"/\"")
}
}
// Get returns matching value and revision for given key. Returns nil
// value, nil rev and nil error when given path doesn't exist.
func (s *store) get(path string) (value []byte, rev interface{}, err error) {
assertValidPath(path)
var kve kvEntry
err = doJSONCall(s, "GET", path, nil, &kve)
if err == errNotFound {
return nil, nil, nil
}
if err != nil {
return nil, nil, err
}
rev = nil
if kve.Rev != nil {
rev = kve.Rev
}
return kve.Value, rev, nil
}
func mutate(s *store, method string, path string, value []byte, rev interface{}, create bool, sensitive bool) error {
values := url.Values{
"value": {string(value)},
}
if create || rev == RevCreate {
rev = nil
values.Set("create", "1")
}
if rev != nil {
revBytes, ok := rev.([]byte)
if !ok {
return ErrRevMismatch
}
values.Set("rev", string(revBytes))
}
if sensitive {
values.Set("sensitive", "true")
} else {
values.Set("sensitive", "false")
}
_, err := doCall(s, method, path, values)
return err
}
// Set updates given key-value pair. If non-nil, rev is a form of CAS
// value used to detect races with concurrent mutators in typical
// read-modify-write cases. Rev is supposed to be same value that is
// returned from get.
func (s *store) set(path string, value []byte, rev interface{}, sensitive bool) error {
assertValidPath(path)
return mutate(s, "PUT", path, value, rev, false, sensitive)
}
// Add creates given kv pair. Which must not exist in storage
// yet. ErrRevMismatch is returned if pair with such key exists.
func (s *store) add(path string, value []byte, sensitive bool) error {
assertValidPath(path)
return mutate(s, "PUT", path, value, nil, true, sensitive)
}
// Delete deletes given key.
func (s *store) delete(path string, rev interface{}) error {
assertValidPath(path)
return mutate(s, "DELETE", path, nil, rev, false, false)
}
// Recursive Delete deletes all keys that are children of given directory path.
func (s *store) recursiveDelete(dirpath string) error {
assertValidDirPath(dirpath)
return mutate(s, "DELETE", dirpath, nil, nil, false, false)
}
// IterateChildren invokes given callback on every kv-pair that's
// child of given directory path. Path must end on "/".
func (s *store) iterateChildren(dirpath string, callback callbackInt) error {
return doRunObserveChildren(s, dirpath, callback, nil)
}
// RunObserveChildren invokes gen callback on every kv-pair that is
// child of given directory path and then on every mutation of
// affected keys. Deletions will be signalled by passing nil to value
// argument of callback. Returns only when cancel channel is closed or
// when children callback returns error. If exit is due to cancel
// channel being closed returned error is nil. Otherwise error is
// non-nil. Path must end on "/".
func (s *store) runObserveChildren(dirpath string, callback callbackInt,
cancel <-chan struct{}) error {
if cancel == nil {
return nil
}
return doRunObserveChildren(s, dirpath, callback, cancel)
}
func doRunObserveChildren(s *store, dirpath string, callback callbackInt,
cancel <-chan struct{}) error {
assertValidDirPath(dirpath)
values := url.Values{}
if cancel != nil {
values.Set("feed", "continuous")
}
r, err := doCallInner(s, "GET", dirpath, values)
if r != nil {
defer r.Body.Close()
}
if err != nil {
return err
}
errChan := make(chan error, 1)
kveChan := make(chan kvEntry)
go func() {
dec := json.NewDecoder(r.Body)
var kve kvEntry
for {
err := dec.Decode(&kve)
if err != nil {
errChan <- err
close(kveChan)
return
}
kveChan <- kve
}
}()
effCancel := cancel
if cancel == nil {
effCancel = make(chan struct{})
}
readLoop:
for {
select {
case kve, ok := <-kveChan:
if !ok {
err = <-errChan
break readLoop
}
err = callback(kve)
if err != nil {
return err
}
case err = <-errChan:
break readLoop
case <-effCancel:
r.Body.Close()
// ensure that worker goroutine is done
for range kveChan {
}
return nil
}
}
if cancel == nil && err == io.EOF {
err = nil
}
return err
}
// Get returns matching value and revision for given key. Returns nil
// value, nil rev and nil error when given path doesn't exist.
func Get(path string) (value []byte, rev interface{}, err error) {
return defaultStore.get(path)
}
// Set updates given key-value pair. If non-nil, rev is a form of CAS
// value used to detect races with concurrent mutators in typical
// read-modify-write cases. Rev is supposed to be same value that is
// returned from get.
func Set(path string, value []byte, rev interface{}) error {
return defaultStore.set(path, value, rev, false)
}
// SetSensitive is Set for storing sensitive info.
func SetSensitive(path string, value []byte, rev interface{}) error {
return defaultStore.set(path, value, rev, true)
}
// Add creates given kv pair. Which must not exist in storage
// yet. ErrRevMismatch is returned if pair with such key exists.
func Add(path string, value []byte) error {
return defaultStore.add(path, value, false)
}
// AddSensitive is Add for storing sensitive info.
func AddSensitive(path string, value []byte) error {
return defaultStore.add(path, value, true)
}
// Delete deletes given key.
func Delete(path string, rev interface{}) error {
return defaultStore.delete(path, rev)
}
// RecursiveDelete deletes all keys that are children of given directory path.
func RecursiveDelete(dirpath string) error {
return defaultStore.recursiveDelete(dirpath)
}
// IterateChildren invokes given callback on every kv-pair that's
// child of given directory path. Path must end on "/".
func IterateChildren(dirpath string, callback Callback) error {
return defaultStore.iterateChildren(dirpath,
func(e kvEntry) error {
return callback(KVEntry{e.Path, e.Value, e.Rev,
e.Sensitive})
})
}
// RunObserveChildren invokes gen callback on every kv-pair that is
// child of given directory path and then on every mutation of
// affected keys. Deletions will be signalled by passing nil to value
// argument of callback. Returns only when cancel channel is closed or
// when children callback returns error. If exit is due to cancel
// channel being closed returned error is nil. Otherwise error is
// non-nil. Path must end on "/".
func RunObserveChildren(dirpath string, callback Callback,
cancel <-chan struct{}) error {
return defaultStore.runObserveChildren(dirpath,
func(e kvEntry) error {
return callback(KVEntry{e.Path, e.Value, e.Rev,
e.Sensitive})
}, cancel)
}
// ListAllChildren returns all child entries of given "directory" node.
func ListAllChildren(dirpath string) (entries []KVEntry, err error) {
return defaultStore.listAllChildren(dirpath)
}
// ListAllChildren returns all child entries of given "directory" node.
func (s *store) listAllChildren(dirpath string) (entries []KVEntry, err error) {
// nil could be used here, but then empty list becomes nil and
// in my testing code it means json null is returned rather
// than empty json array.
entries = make([]KVEntry, 0, 16)
err = s.iterateChildren(dirpath,
func(e kvEntry) error {
entries = append(entries,
KVEntry{e.Path, e.Value, e.Rev, e.Sensitive})
return nil
})
return
}