forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
247 lines (228 loc) · 6.24 KB
/
scheduler.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
// 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"
"fmt"
"net/http"
"time"
"github.com/go-kivik/couchdb/v4/chttp"
"github.com/go-kivik/kivik/v4/driver"
)
type schedulerDoc struct {
Database string `json:"database"`
DocID string `json:"doc_id"`
ReplicationID string `json:"id"`
Source string `json:"source"`
Target string `json:"target"`
StartTime time.Time `json:"start_time"`
LastUpdated time.Time `json:"last_updated"`
State string `json:"state"`
Info repInfo `json:"info"`
}
type repInfo struct {
Error error
DocsRead int64 `json:"docs_read"`
DocsWritten int64 `json:"docs_written"`
DocWriteFailures int64 `json:"doc_write_failures"`
Pending int64 `json:"changes_pending"`
}
func (i *repInfo) UnmarshalJSON(data []byte) error {
switch {
case string(data) == "null":
return nil
case bytes.HasPrefix(data, []byte(`{"error":`)):
var e struct {
Error *replicationError `json:"error"`
}
if err := json.Unmarshal(data, &e); err != nil {
return err
}
i.Error = e.Error
case data[0] == '{':
type repInfoClone repInfo
var x repInfoClone
if err := json.Unmarshal(data, &x); err != nil {
return err
}
*i = repInfo(x)
default:
var e replicationError
if err := json.Unmarshal(data, &e); err != nil {
return err
}
i.Error = &e
}
return nil
}
type schedulerReplication struct {
docID string
database string
replicationID string
source string
target string
startTime time.Time
lastUpdated time.Time
state string
info repInfo
*db
}
var _ driver.Replication = &schedulerReplication{}
func (c *client) schedulerSupported(ctx context.Context) (bool, error) {
c.sdMU.Lock()
defer c.sdMU.Unlock()
if c.schedulerDetected != nil {
return *c.schedulerDetected, nil
}
resp, err := c.DoReq(ctx, http.MethodHead, "_scheduler/jobs", nil)
if err != nil {
return false, err
}
var supported bool
switch resp.StatusCode {
case http.StatusBadRequest:
// 1.6.x, 1.7.x
supported = false
case http.StatusNotFound:
// 2.0.x
supported = false
case http.StatusOK, http.StatusUnauthorized:
// 2.1.x +
supported = true
default:
// Assume not supported
supported = false
}
c.schedulerDetected = &supported
return supported, nil
}
func (c *client) newSchedulerReplication(doc *schedulerDoc) *schedulerReplication {
rep := &schedulerReplication{
db: &db{
client: c,
dbName: doc.Database,
},
}
rep.setFromDoc(doc)
return rep
}
func (r *schedulerReplication) setFromDoc(doc *schedulerDoc) {
if r.source == "" {
r.docID = doc.DocID
r.database = doc.Database
r.replicationID = doc.ReplicationID
r.source = doc.Source
r.target = doc.Target
r.startTime = doc.StartTime
}
r.lastUpdated = doc.LastUpdated
r.state = doc.State
r.info = doc.Info
}
func (c *client) fetchSchedulerReplication(ctx context.Context, docID string) (*schedulerReplication, error) {
rep := &schedulerReplication{
docID: docID,
database: "_replicator",
db: &db{
client: c,
dbName: "_replicator",
},
}
for rep.source == "" {
if err := rep.update(ctx); err != nil {
return rep, err
}
time.Sleep(100 * time.Millisecond)
}
return rep, nil
}
func (r *schedulerReplication) StartTime() time.Time { return r.startTime }
func (r *schedulerReplication) EndTime() time.Time {
if r.state == "failed" || r.state == "completed" {
return r.lastUpdated
}
return time.Time{}
}
func (r *schedulerReplication) Err() error { return r.info.Error }
func (r *schedulerReplication) ReplicationID() string { return r.replicationID }
func (r *schedulerReplication) Source() string { return r.source }
func (r *schedulerReplication) Target() string { return r.target }
func (r *schedulerReplication) State() string { return r.state }
func (r *schedulerReplication) Update(ctx context.Context, rep *driver.ReplicationInfo) error {
if err := r.update(ctx); err != nil {
return err
}
rep.DocWriteFailures = r.info.DocWriteFailures
rep.DocsRead = r.info.DocsRead
rep.DocsWritten = r.info.DocsWritten
return nil
}
func (r *schedulerReplication) 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
}
// isBug1000 detects a race condition bug in CouchDB 2.1.x so the attempt can
// be retried. See https://github.com/apache/couchdb/issues/1000
func isBug1000(err error) bool {
if err == nil {
return false
}
cerr, ok := err.(*chttp.HTTPError)
if !ok {
// should never happen
return false
}
if cerr.Response.StatusCode != http.StatusInternalServerError {
return false
}
return cerr.Reason == "function_clause"
}
func (r *schedulerReplication) update(ctx context.Context) error {
path := fmt.Sprintf("/_scheduler/docs/%s/%s", r.database, chttp.EncodeDocID(r.docID))
var doc schedulerDoc
if _, err := r.db.Client.DoJSON(ctx, http.MethodGet, path, nil, &doc); err != nil {
if isBug1000(err) {
return r.update(ctx)
}
return err
}
r.setFromDoc(&doc)
return nil
}
func (c *client) getReplicationsFromScheduler(ctx context.Context, options map[string]interface{}) ([]driver.Replication, error) {
params, err := optionsToParams(options)
if err != nil {
return nil, err
}
var result struct {
Docs []schedulerDoc `json:"docs"`
}
path := "/_scheduler/docs"
if params != nil {
path = path + "?" + params.Encode()
}
if _, err = c.DoJSON(ctx, http.MethodGet, path, nil, &result); err != nil {
return nil, err
}
reps := make([]driver.Replication, 0, len(result.Docs))
for _, row := range result.Docs {
rep := c.newSchedulerReplication(&row)
reps = append(reps, rep)
}
return reps, nil
}