-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicator.go
418 lines (347 loc) · 10.1 KB
/
replicator.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
package replicator
import (
"context"
"errors"
"fmt"
"time"
"github.com/goydb/replicator/client"
"github.com/goydb/replicator/logger"
)
var (
ErrAbort = errors.New("abort replication")
ErrReplicationCompleted = errors.New("replication completed")
)
// Replicator implements the couchdb replication protocol:
// https://docs.couchdb.org/en/stable/replication/protocol.html
type Replicator struct {
// name used to generate the replication id
name string
job *Job
source *client.Client
target *client.Client
sourceInfo, targetInfo *client.Info
replicationID string
sourceLastSeq string
diffResp client.DiffResponse
sourceRepLog, targetRepLog *client.ReplicationLog
currentHistory *client.History
logger logger.Logger
}
func NewReplicator(name string, job *Job) (*Replicator, error) {
source, err := client.NewClient(job.Source)
if err != nil {
return nil, err
}
target, err := client.NewClient(job.Target)
if err != nil {
return nil, err
}
return &Replicator{
name: name,
job: job,
logger: new(logger.Noop),
source: source,
target: target,
}, nil
}
func (r *Replicator) SetLogger(logger logger.Logger) {
r.logger = logger
r.source.SetLogger(logger)
r.target.SetLogger(logger)
}
func (t *Replicator) logErrf(format string, args ...interface{}) error {
e := fmt.Errorf(format, args...)
t.logger.Error(e.Error())
return e
}
func (r *Replicator) Run(ctx context.Context) error {
r.logger.Debug("VerifyPeers")
err := r.VerifyPeers(ctx)
if err != nil {
return r.logErrf("verify peers failed: %w", err)
}
r.logger.Debug("GetPeersInformation")
err = r.GetPeersInformation(ctx)
if err != nil {
return r.logErrf("get peers information failed: %w", err)
}
r.logger.Debug("FindCommonAncestry")
err = r.FindCommonAncestry(ctx)
if err != nil {
return r.logErrf("find common ancestry failed: %w", err)
}
for replicate := true; replicate; replicate = r.job.Continuous {
r.logger.Debugf("Replication will start since: %s", r.sourceLastSeq)
r.currentHistory = &client.History{
StartTime: client.Time(time.Now()),
StartLastSeq: r.sourceLastSeq,
SessionID: r.replicationID,
}
r.logger.Debug("LocateChangedDocuments")
lastSeq, err := r.LocateChangedDocuments(ctx)
if err != nil {
return r.logErrf("locate changed documents failed: %w", err)
}
r.logger.Debugf("ReplicateChanges (lastSeq: %q)", lastSeq)
err = r.ReplicateChanges(ctx, lastSeq)
if err != nil {
return r.logErrf("replicate changes failed: %w", err)
}
}
return nil
}
// VerifyPeers
// https://docs.couchdb.org/en/stable/replication/protocol.html#verify-peers
func (r *Replicator) VerifyPeers(ctx context.Context) error {
// Check Source Existence
err := r.source.Check(ctx)
if err != nil {
return err
}
// Check Target Existence
err = r.target.Check(ctx)
if err == nil { // 200 OK
return nil
}
// only 404 Not Found can be handled
if !errors.Is(err, client.ErrNotFound) {
return err
}
// Create Target?
if !r.job.CreateTarget {
return err
}
// Create Target
return r.target.Create(ctx)
}
// GetPeersInformation
// https://docs.couchdb.org/en/stable/replication/protocol.html#get-peers-information
func (r *Replicator) GetPeersInformation(ctx context.Context) error {
var err error
// Get Source Information
r.sourceInfo, err = r.source.Info(ctx)
if err != nil {
return err
}
// Get Target Information
r.targetInfo, err = r.target.Info(ctx)
if err != nil {
return err
}
return nil
}
// FindCommonAncestry
// https://docs.couchdb.org/en/stable/replication/protocol.html#find-common-ancestry
func (r *Replicator) FindCommonAncestry(ctx context.Context) error {
// Generate Replication ID
id := r.buildReplicationID()
// Get Replication Log from Source
sourceRepLog, err := r.source.GetReplicationLog(ctx, id)
if err != nil && !errors.Is(err, client.ErrNotFound) {
return err
}
if sourceRepLog == nil {
sourceRepLog = new(client.ReplicationLog)
}
// Get Replication Log from Target
targetRepLog, err := r.target.GetReplicationLog(ctx, id)
if err != nil && !errors.Is(err, client.ErrNotFound) {
return err
}
if targetRepLog == nil {
targetRepLog = new(client.ReplicationLog)
}
// Compare Replication Logs
err = r.CompareReplicationLogs(ctx, sourceRepLog, targetRepLog)
if err != nil {
return err
}
r.sourceRepLog = sourceRepLog
r.targetRepLog = targetRepLog
return nil
}
// Locate Changed Documents
// https://docs.couchdb.org/en/stable/replication/protocol.html#locate-changed-documents
func (r *Replicator) LocateChangedDocuments(ctx context.Context) (string, error) {
start:
time.Sleep(time.Second)
// Listen to Changes Feed
changes, err := r.source.Changes(ctx, client.ChangeOptions{
Since: r.sourceLastSeq,
Heartbeat: r.job.HeartbeatOrFallback(),
})
if err != nil {
return "", err
}
// No more changes
r.logger.Debugf("Changes: %d", len(changes.Results))
if len(changes.Results) == 0 {
if r.job.Continuous {
goto start
} else {
return "", ErrReplicationCompleted // Replication Completed
}
}
// Read Batch of Changes
diff := make(client.RevDiffRequest)
for _, change := range changes.Results {
for _, rev := range change.Changes {
diff[change.ID] = append(diff[change.ID], rev.Rev)
}
}
r.currentHistory.MissingFound += len(diff)
// Compare Documents Revisions
diffResp, err := r.target.RevDiff(ctx, diff)
if err != nil {
return "", err
}
r.currentHistory.MissingChecked += len(diffResp)
// Any Differences Found?
r.logger.Debugf("Differences: %d", len(diffResp))
if len(diffResp) == 0 { // No
goto start
}
r.diffResp = diffResp
return changes.LastSeq, nil
}
// MB10 10 MB
const MB10 = 10 * (1024 ^ 2)
// ReplicateChanges
// https://docs.couchdb.org/en/stable/replication/protocol.html#replicate-changes
func (r *Replicator) ReplicateChanges(ctx context.Context, lastSeq string) error {
var stack client.Stack
for docID, diff := range r.diffResp {
// Fetch Next Changed Document
doc, err := r.source.GetDocumentComplete(ctx, docID, diff)
if err != nil {
return err
}
r.currentHistory.DocsRead++
r.logger.Debugf("Document size: %d has attachments: %v revision: %q", doc.Size(), doc.HasChangedAttachments(), doc.Data["_rev"])
// Document Has Changed Attachments?
if doc.HasChangedAttachments() {
// Are They Big Enough?
if doc.Size() > MB10 {
// Update Document on Target
err := r.target.UploadDocumentWithAttachments(ctx, doc)
if err != nil {
r.currentHistory.DocWriteFailures++
return err
}
r.currentHistory.DocsWritten++
continue
} else {
err := doc.InlineAttachments()
if err != nil {
return err
}
}
}
// Put Document Into the Stack
stack = append(stack, doc)
// Stack is Full?
if stack.Size() > MB10 {
err := r.replicateChangesBulk(ctx, stack)
if err != nil {
return err
}
}
}
// stack too small but changes available? push rest
if len(stack) > 0 {
err := r.replicateChangesBulk(ctx, stack)
if err != nil {
return err
}
}
r.currentHistory.SessionID = r.replicationID
r.currentHistory.EndLastSeq = lastSeq
r.currentHistory.EndTime = client.Time(time.Now())
if r.currentHistory.DocsWritten > 0 {
err := r.recordReplicationCheckpoint(ctx, r.sourceRepLog, lastSeq)
if err != nil {
return err
}
err = r.recordReplicationCheckpoint(ctx, r.targetRepLog, lastSeq)
if err != nil {
return err
}
}
r.currentHistory = nil
return nil
}
// Reset resets the replicator state at the source and target database
func (r *Replicator) Reset(ctx context.Context) error {
id := r.buildReplicationID()
err := r.source.RemoveReplicationCheckpoint(ctx, id)
if err != nil {
return err
}
err = r.target.RemoveReplicationCheckpoint(ctx, id)
if err != nil {
return err
}
return nil
}
func (r *Replicator) buildReplicationID() string {
if r.replicationID == "" {
id := r.job.GenerateReplicationID(r.name)
r.logger.Debugf("Replication ID %q", id)
r.replicationID = id
}
return r.replicationID
}
func (r *Replicator) replicateChangesBulk(ctx context.Context, stack client.Stack) error {
// Upload Stack of Documents to Target
err := r.target.BulkDocs(ctx, &stack)
if err != nil {
r.currentHistory.DocWriteFailures += len(stack)
return err
}
r.currentHistory.DocsWritten += len(stack)
// Ensure in Commit
err = r.target.EnsureFullCommit(ctx)
if err != nil {
return err
}
return nil
}
func (r *Replicator) recordReplicationCheckpoint(ctx context.Context, repLog *client.ReplicationLog, lastSeq string) error {
repLog.ID = r.replicationID
repLog.ReplicationIDVersion = 3
repLog.SessionID = r.replicationID
repLog.SourceLastSeq = lastSeq
repLog.History = append(r.targetRepLog.History, r.currentHistory)
// Record Replication Checkpoint
err := r.source.RecordReplicationCheckpoint(ctx, repLog, r.replicationID)
if err != nil {
return err
}
return nil
}
const NoVersion = "0"
// 2.4.2.3.3. Compare Replication Logs
func (r *Replicator) CompareReplicationLogs(ctx context.Context, source, target *client.ReplicationLog) error {
// If the Replication Logs are successfully retrieved from both Source and Target then the Replicator MUST determine their common ancestry by following the next algorithm:
if source == nil || target == nil {
r.sourceLastSeq = NoVersion
return nil
}
// Compare session_id values for the chronological last session - if they match both Source and Target have a common Replication history and it seems to be valid. Use source_last_seq value for the startup Checkpoint
if source.SessionID == target.SessionID && source.SourceLastSeq != "" {
r.sourceLastSeq = source.SourceLastSeq
return nil
}
// In case of mismatch, iterate over the history collection to search for the latest (chronologically) common session_id for Source and Target. Use value of recorded_seq field as startup Checkpoint
for _, sl := range source.History {
for _, tl := range target.History {
if sl.SessionID == tl.SessionID {
r.sourceLastSeq = sl.RecordedSeq
return nil
}
}
}
// If Source and Target has no common ancestry, the Replicator MUST run Full Replication.
r.sourceLastSeq = NoVersion
return nil
}