-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicate.go
443 lines (405 loc) · 10.4 KB
/
replicate.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// 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 xkivik
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/go-kivik/kivik/v4"
)
// ReplicationResult represents the result of a replication.
type ReplicationResult struct {
DocWriteFailures int `json:"doc_write_failures"`
DocsRead int `json:"docs_read"`
DocsWritten int `json:"docs_written"`
EndTime time.Time `json:"end_time"`
MissingChecked int `json:"missing_checked"`
MissingFound int `json:"missing_found"`
StartTime time.Time `json:"start_time"`
}
type resultWrapper struct {
*ReplicationResult
mu sync.Mutex
}
func (r *resultWrapper) read() {
r.mu.Lock()
r.DocsRead++
r.mu.Unlock()
}
func (r *resultWrapper) missingChecked() {
r.mu.Lock()
r.MissingChecked++
r.mu.Unlock()
}
func (r *resultWrapper) missingFound() {
r.mu.Lock()
r.MissingFound++
r.mu.Unlock()
}
func (r *resultWrapper) writeError() {
r.mu.Lock()
r.DocWriteFailures++
r.mu.Unlock()
}
func (r *resultWrapper) write() {
r.mu.Lock()
r.DocsWritten++
r.mu.Unlock()
}
const (
eventSecurity = "security"
eventChanges = "changes"
eventChange = "change"
eventRevsDiff = "revsdiff"
eventDocument = "document"
)
// ReplicationEvent is an event emitted by the Replicate function, which
// represents a single read or write event, and its status.
type ReplicationEvent struct {
// Type is the event type. Options are:
//
// - "security" -- Relates to the _security document.
// - "changes" -- Relates to the changes feed.
// - "change" -- Relates to a single change.
// - "revsdiff" -- Relates to reading the revs diff.
// - "document" -- Relates to a specific document.
Type string
// Read is true if the event relates to a read operation.
Read bool
// DocID is the relevant document ID, if any.
DocID string
// Error is the error associated with the event, if any.
Error error
// Changes is the list of changed revs, for a "change" event.
Changes []string
}
// EventCallback is a function that receives replication events.
type EventCallback func(ReplicationEvent)
// WithEventCallback adds an EventCallback function to the context, which will
// be used by the Replicate function, to emit events, useful for debugging or
// logging.
func WithEventCallback(ctx context.Context, cb EventCallback) context.Context {
return context.WithValue(ctx, callbackKey, cb)
}
func callback(ctx context.Context) EventCallback {
cb, _ := ctx.Value(callbackKey).(EventCallback)
if cb == nil {
cb = func(ReplicationEvent) {}
}
return cb
}
type contextKey struct{ name string }
var callbackKey = &contextKey{"event_callback"}
type multiOptions []kivik.Option
var _ kivik.Option = (multiOptions)(nil)
func (o multiOptions) Apply(t interface{}) {
for _, opt := range o {
if opt != nil {
opt.Apply(t)
}
}
}
// Replicate performs a replication from source to target, using a limited
// version of the CouchDB replication protocol.
//
// The following options are supported:
//
// filter (string) - The name of a filter function.
// doc_ids (array of string) - Array of document IDs to be synchronized.
// copy_security (bool) - When true, the security object is read from the
// source, and copied to the target, before the
// replication. Use with caution! The security object
// is not versioned, and will be unconditionally
// overwritten!
func Replicate(ctx context.Context, target, source *kivik.DB, options ...kivik.Option) (*ReplicationResult, error) {
result := &resultWrapper{
ReplicationResult: &ReplicationResult{
StartTime: time.Now(),
},
}
defer func() {
result.EndTime = time.Now()
}()
opts := map[string]interface{}{}
multiOptions(options).Apply(opts)
cb := callback(ctx)
if _, sec := opts["copy_security"].(bool); sec {
if err := copySecurity(ctx, target, source, cb); err != nil {
return result.ReplicationResult, err
}
}
group, ctx := errgroup.WithContext(ctx)
changes := make(chan *change)
group.Go(func() error {
defer close(changes)
return readChanges(ctx, source, changes, multiOptions(options), cb)
})
diffs := make(chan *revDiff)
group.Go(func() error {
defer close(diffs)
return readDiffs(ctx, target, changes, diffs, cb)
})
docs := make(chan *Document)
group.Go(func() error {
defer close(docs)
return readDocs(ctx, source, diffs, docs, result, cb)
})
group.Go(func() error {
return storeDocs(ctx, target, docs, result, cb)
})
return result.ReplicationResult, group.Wait()
}
func copySecurity(ctx context.Context, target, source *kivik.DB, cb EventCallback) error {
sec, err := source.Security(ctx)
cb(ReplicationEvent{
Type: eventSecurity,
Read: true,
Error: err,
})
if err != nil {
return fmt.Errorf("read security: %w", err)
}
err = target.SetSecurity(ctx, sec)
cb(ReplicationEvent{
Type: eventSecurity,
Read: false,
Error: err,
})
if err != nil {
return fmt.Errorf("set security: %w", err)
}
return nil
}
type change struct {
ID string
Changes []string
}
func readChanges(ctx context.Context, db *kivik.DB, results chan<- *change, options kivik.Option, cb EventCallback) error {
changes := db.Changes(ctx, options, kivik.Param("feed", "normal"), kivik.Param("style", "all_docs"))
cb(ReplicationEvent{
Type: eventChanges,
Read: true,
})
defer changes.Close() // nolint: errcheck
for changes.Next() {
ch := &change{
ID: changes.ID(),
Changes: changes.Changes(),
}
cb(ReplicationEvent{
Type: eventChange,
DocID: ch.ID,
Read: true,
Changes: ch.Changes,
})
select {
case <-ctx.Done():
return ctx.Err()
case results <- ch:
}
}
if err := changes.Err(); err != nil {
cb(ReplicationEvent{
Type: eventChanges,
Read: true,
Error: err,
})
return fmt.Errorf("read changes feed: %w", err)
}
return nil
}
type revDiff struct {
ID string `json:"-"`
Missing []string `json:"missing"`
PossibleAncestors []string `json:"possible_ancestors"`
}
const rdBatchSize = 10
func readDiffs(ctx context.Context, db *kivik.DB, ch <-chan *change, results chan<- *revDiff, cb EventCallback) error {
for {
revMap := map[string][]string{}
var change *change
var ok bool
loop:
for {
select {
case <-ctx.Done():
return ctx.Err()
case change, ok = <-ch:
if !ok {
break loop
}
revMap[change.ID] = change.Changes
if len(revMap) >= rdBatchSize {
break loop
}
}
}
if len(revMap) == 0 {
return nil
}
diffs := db.RevsDiff(ctx, revMap)
err := diffs.Err()
cb(ReplicationEvent{
Type: eventRevsDiff,
Read: true,
Error: err,
})
if err != nil {
return err
}
defer diffs.Close() // nolint: errcheck
for diffs.Next() {
var val revDiff
if err := diffs.ScanValue(&val); err != nil {
cb(ReplicationEvent{
Type: eventRevsDiff,
Read: true,
Error: err,
})
return err
}
val.ID, _ = diffs.ID()
cb(ReplicationEvent{
Type: eventRevsDiff,
Read: true,
DocID: val.ID,
})
select {
case <-ctx.Done():
return ctx.Err()
case results <- &val:
}
}
if err := diffs.Err(); err != nil {
cb(ReplicationEvent{
Type: eventRevsDiff,
Read: true,
Error: err,
})
return fmt.Errorf("read revs diffs: %w", err)
}
}
}
func readDocs(ctx context.Context, db *kivik.DB, diffs <-chan *revDiff, results chan<- *Document, result *resultWrapper, cb EventCallback) error {
for {
var rd *revDiff
var ok bool
select {
case <-ctx.Done():
return ctx.Err()
case rd, ok = <-diffs:
if !ok {
return nil
}
for _, rev := range rd.Missing {
result.missingChecked()
d, err := readDoc(ctx, db, rd.ID, rev)
cb(ReplicationEvent{
Type: eventDocument,
Read: true,
DocID: rd.ID,
Error: err,
})
if err != nil {
return fmt.Errorf("read doc %s: %w", rd.ID, err)
}
result.read()
result.missingFound()
select {
case <-ctx.Done():
return ctx.Err()
case results <- d:
}
}
}
}
}
func readDoc(ctx context.Context, db *kivik.DB, docID, rev string) (*Document, error) {
doc := new(Document)
row := db.Get(ctx, docID, kivik.Params(map[string]interface{}{
"rev": rev,
"revs": true,
"attachments": true,
}))
if err := row.ScanDoc(&doc); err != nil {
return nil, err
}
// TODO: It seems silly this is necessary... I need better attachment
// handling in kivik.
if atts, _ := row.Attachments(); atts != nil {
for {
att, err := atts.Next()
if err != nil {
if err != io.EOF {
return nil, err
}
break
}
var content []byte
switch att.ContentEncoding {
case "":
var err error
content, err = io.ReadAll(att.Content)
if err != nil {
return nil, err
}
if err := att.Content.Close(); err != nil {
return nil, err
}
case "gzip":
zr, err := gzip.NewReader(att.Content)
if err != nil {
return nil, err
}
content, err = io.ReadAll(zr)
if err != nil {
return nil, err
}
if err := zr.Close(); err != nil {
return nil, err
}
if err := att.Content.Close(); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("Unknown encoding '%s' for attachment '%s'", att.ContentEncoding, att.Filename)
}
att.Stub = false
att.Follows = false
att.Content = io.NopCloser(bytes.NewReader(content))
doc.Attachments.Set(att.Filename, att)
}
}
return doc, nil
}
func storeDocs(ctx context.Context, db *kivik.DB, docs <-chan *Document, result *resultWrapper, cb EventCallback) error {
for doc := range docs {
_, err := db.Put(ctx, doc.ID, doc, kivik.Param("new_edits", false))
cb(ReplicationEvent{
Type: "document",
Read: false,
DocID: doc.ID,
Error: err,
})
if err != nil {
result.writeError()
return fmt.Errorf("store doc %s: %w", doc.ID, err)
}
result.write()
}
return nil
}