forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replication.go
331 lines (301 loc) · 9 KB
/
replication.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
// 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 couchdb
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/go-kivik/couchdb/v4/chttp"
kivik "github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/driver"
)
type replicationError struct {
status int
reason string
}
func (re *replicationError) Error() string {
return re.reason
}
func (re *replicationError) StatusCode() int {
return re.status
}
func (re *replicationError) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &re.reason); err != nil {
return err
}
switch (strings.SplitN(re.reason, ":", 2))[0] {
case "db_not_found":
re.status = http.StatusNotFound
case "timeout":
re.status = http.StatusRequestTimeout
case "unauthorized":
re.status = http.StatusUnauthorized
default:
re.status = http.StatusInternalServerError
}
return nil
}
type replicationStateTime time.Time
func (t *replicationStateTime) UnmarshalJSON(data []byte) error {
input := string(bytes.Trim(data, `"`))
if ts, err := time.Parse(time.RFC3339, input); err == nil {
*t = replicationStateTime(ts)
return nil
}
// Fallback for really old versions of CouchDB
if seconds, err := strconv.ParseInt(input, 10, 64); err == nil {
epochTime := replicationStateTime(time.Unix(seconds, 0).UTC())
*t = epochTime
return nil
}
return &kivik.Error{HTTPStatus: http.StatusBadGateway, Err: fmt.Errorf("kivik: '%s' does not appear to be a valid timestamp", string(data))}
}
type replication struct {
docID string
replicationID string
source string
target string
startTime time.Time
endTime time.Time
state string
err error
// mu protects the above values
mu sync.RWMutex
*db
}
var _ driver.Replication = &replication{}
func (c *client) fetchReplication(ctx context.Context, docID string) *replication {
rep := c.newReplication(docID)
rep.db = &db{client: c, dbName: "_replicator"}
// Do an update to get the initial state, but don't fail if there's an error
// at this stage, because we successfully created the replication doc.
_ = rep.updateMain(ctx)
return rep
}
func (c *client) newReplication(docID string) *replication {
return &replication{
docID: docID,
db: &db{
client: c,
dbName: "_replicator",
},
}
}
func (r *replication) readLock() func() {
r.mu.RLock()
return r.mu.RUnlock
}
func (r *replication) ReplicationID() string { defer r.readLock()(); return r.replicationID }
func (r *replication) Source() string { defer r.readLock()(); return r.source }
func (r *replication) Target() string { defer r.readLock()(); return r.target }
func (r *replication) StartTime() time.Time { defer r.readLock()(); return r.startTime }
func (r *replication) EndTime() time.Time { defer r.readLock()(); return r.endTime }
func (r *replication) State() string { defer r.readLock()(); return r.state }
func (r *replication) Err() error { defer r.readLock()(); return r.err }
func (r *replication) Update(ctx context.Context, state *driver.ReplicationInfo) error {
if err := r.updateMain(ctx); err != nil {
return err
}
if r.State() == "complete" {
state.Progress = 100
return nil
}
info, err := r.updateActiveTasks(ctx)
if err != nil {
if kivik.StatusCode(err) == http.StatusNotFound {
// not listed in _active_tasks (because the replication is done, or
// hasn't yet started), but this isn't an error
return nil
}
return err
}
state.DocWriteFailures = info.DocWriteFailures
state.DocsRead = info.DocsRead
state.DocsWritten = info.DocsWritten
// state.progress = info.Progress
return nil
}
type activeTask struct {
Type string `json:"type"`
ReplicationID string `json:"replication_id"`
DocsWritten int64 `json:"docs_written"`
DocsRead int64 `json:"docs_read"`
DocWriteFailures int64 `json:"doc_write_failures"`
}
func (r *replication) updateActiveTasks(ctx context.Context) (*activeTask, error) {
resp, err := r.client.DoReq(ctx, http.MethodGet, "/_active_tasks", nil)
if err != nil {
return nil, err
}
if err = chttp.ResponseError(resp); err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var tasks []*activeTask
if err = json.NewDecoder(resp.Body).Decode(&tasks); err != nil {
return nil, &kivik.Error{HTTPStatus: http.StatusBadGateway, Err: err}
}
for _, task := range tasks {
if task.Type != "replication" {
continue
}
repIDparts := strings.SplitN(task.ReplicationID, "+", 2)
if repIDparts[0] != r.replicationID {
continue
}
return task, nil
}
return nil, &kivik.Error{HTTPStatus: http.StatusNotFound, Err: errors.New("task not found")}
}
// updateMain updates the "main" fields: those stored directly in r.
func (r *replication) updateMain(ctx context.Context) error {
doc, err := r.getReplicatorDoc(ctx)
if err != nil {
return err
}
r.setFromReplicatorDoc(doc)
return nil
}
func (r *replication) getReplicatorDoc(ctx context.Context) (*replicatorDoc, error) {
row, err := r.db.Get(ctx, r.docID, nil)
if err != nil {
return nil, err
}
var doc replicatorDoc
err = json.NewDecoder(row.Body).Decode(&doc)
return &doc, err
}
func (r *replication) setFromReplicatorDoc(doc *replicatorDoc) {
r.mu.Lock()
defer r.mu.Unlock()
switch kivik.ReplicationState(doc.State) {
case kivik.ReplicationStarted:
r.startTime = time.Time(doc.StateTime)
case kivik.ReplicationError, kivik.ReplicationComplete:
r.endTime = time.Time(doc.StateTime)
}
r.state = doc.State
if doc.Error != nil {
r.err = doc.Error
} else {
r.err = nil
}
if r.source == "" {
r.source = doc.Source
}
if r.target == "" {
r.target = doc.Target
}
if r.replicationID == "" {
r.replicationID = doc.ReplicationID
}
}
func (r *replication) Delete(ctx context.Context) error {
_, rev, err := r.GetMeta(ctx, r.docID, nil)
if err != nil {
return err
}
_, err = r.db.Delete(ctx, r.docID, rev, nil)
return err
}
type replicatorDoc struct {
DocID string `json:"_id"`
ReplicationID string `json:"_replication_id"`
Source string `json:"source"`
Target string `json:"target"`
State string `json:"_replication_state"`
StateTime replicationStateTime `json:"_replication_state_time"`
Error *replicationError `json:"_replication_state_reason,omitempty"`
}
func (c *client) GetReplications(ctx context.Context, options map[string]interface{}) ([]driver.Replication, error) {
scheduler, err := c.schedulerSupported(ctx)
if err != nil {
return nil, err
}
if scheduler {
return c.getReplicationsFromScheduler(ctx, options)
}
return c.legacyGetReplications(ctx, options)
}
func (c *client) legacyGetReplications(ctx context.Context, options map[string]interface{}) ([]driver.Replication, error) {
if options == nil {
options = map[string]interface{}{}
}
delete(options, "conflicts")
delete(options, "update_seq")
options["include_docs"] = true
params, err := optionsToParams(options)
if err != nil {
return nil, err
}
var result struct {
Rows []struct {
Doc replicatorDoc `json:"doc"`
} `json:"rows"`
}
path := "/_replicator/_all_docs?" + params.Encode()
if _, err = c.DoJSON(ctx, http.MethodGet, path, nil, &result); err != nil {
return nil, err
}
reps := make([]driver.Replication, 0, len(result.Rows))
for _, row := range result.Rows {
if row.Doc.DocID == "_design/_replicator" {
continue
}
rep := c.newReplication(row.Doc.DocID)
rep.setFromReplicatorDoc(&row.Doc)
reps = append(reps, rep)
}
return reps, nil
}
func (c *client) Replicate(ctx context.Context, targetDSN, sourceDSN string, options map[string]interface{}) (driver.Replication, error) {
if options == nil {
options = make(map[string]interface{})
}
// Allow overriding source and target with options, i.e. for auth options
if _, ok := options["source"]; !ok {
options["source"] = sourceDSN
}
if _, ok := options["target"]; !ok {
options["target"] = targetDSN
}
if t := options["target"]; t == "" {
return nil, missingArg("targetDSN")
}
if s := options["source"]; s == "" {
return nil, missingArg("sourceDSN")
}
scheduler, err := c.schedulerSupported(ctx)
if err != nil {
return nil, err
}
opts := &chttp.Options{
Body: chttp.EncodeBody(options),
}
var repStub struct {
ID string `json:"id"`
}
if _, e := c.Client.DoJSON(ctx, http.MethodPost, "/_replicator", opts, &repStub); e != nil {
return nil, e
}
if scheduler {
return c.fetchSchedulerReplication(ctx, repStub.ID)
}
return c.fetchReplication(ctx, repStub.ID), nil
}