forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket_dura.go
259 lines (227 loc) · 6.54 KB
/
bucket_dura.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
package gocb
import (
"github.com/couchbase/gocb/gocbcore"
)
func (b *Bucket) observeOnceCas(key []byte, cas Cas, forDelete bool, repId int, commCh chan uint) (pendingOp, error) {
return b.client.Observe(key, repId,
func(ks gocbcore.KeyState, obsCas gocbcore.Cas, err error) {
if err != nil {
commCh <- 0
return
}
didReplicate := false
didPersist := false
if ks == gocbcore.KeyStatePersisted {
if !forDelete {
if Cas(obsCas) == cas {
if repId != 0 {
didReplicate = true
}
didPersist = true
}
}
} else if ks == gocbcore.KeyStateNotPersisted {
if !forDelete {
if Cas(obsCas) == cas {
if repId != 0 {
didReplicate = true
}
}
}
} else if ks == gocbcore.KeyStateDeleted {
if forDelete {
didReplicate = true
}
} else {
if forDelete {
didReplicate = true
didPersist = true
}
}
var out uint
if didReplicate {
out |= 1
}
if didPersist {
out |= 2
}
commCh <- out
})
}
func (b *Bucket) observeOnceSeqNo(key []byte, mt MutationToken, repId int, commCh chan uint) (pendingOp, error) {
return b.client.ObserveSeqNo(key, mt.VbUuid, repId,
func(currentSeqNo gocbcore.SeqNo, persistSeqNo gocbcore.SeqNo, err error) {
if err != nil {
commCh <- 0
return
}
didReplicate := currentSeqNo >= mt.SeqNo
didPersist := persistSeqNo >= mt.SeqNo
var out uint
if didReplicate {
out |= 1
}
if didPersist {
out |= 2
}
commCh <- out
})
}
func (b *Bucket) observeOne(key []byte, mt MutationToken, cas Cas, forDelete bool, repId int, replicaCh, persistCh chan bool) {
observeOnce := func(commCh chan uint) (pendingOp, error) {
if mt.VbUuid != 0 && mt.SeqNo != 0 {
return b.observeOnceSeqNo(key, mt, repId, commCh)
} else {
return b.observeOnceCas(key, cas, forDelete, repId, commCh)
}
}
sentReplicated := false
sentPersisted := false
failMe := func() {
if !sentReplicated {
replicaCh <- false
sentReplicated = true
}
if !sentPersisted {
persistCh <- false
sentPersisted = true
}
}
timeoutTmr := acquireTimer(b.duraTimeout)
commCh := make(chan uint)
for {
op, err := observeOnce(commCh)
if err != nil {
releaseTimer(timeoutTmr, false)
failMe()
return
}
select {
case val := <-commCh:
// Got Value
if (val&1) != 0 && !sentReplicated {
replicaCh <- true
sentReplicated = true
}
if (val&2) != 0 && !sentPersisted {
persistCh <- true
sentPersisted = true
}
waitTmr := acquireTimer(b.duraPollTimeout)
select {
case <-waitTmr.C:
releaseTimer(waitTmr, true)
// Fall through to outside for loop
case <-timeoutTmr.C:
releaseTimer(waitTmr, false)
releaseTimer(timeoutTmr, true)
failMe()
return
}
case <-timeoutTmr.C:
// Timed out
op.Cancel()
releaseTimer(timeoutTmr, true)
failMe()
return
}
}
}
func (b *Bucket) durability(key string, cas Cas, mt MutationToken, replicaTo, persistTo uint, forDelete bool) error {
numServers := b.client.NumReplicas() + 1
if replicaTo > uint(numServers-1) || persistTo > uint(numServers) {
return &clientError{"Not enough replicas to match durability requirements."}
}
keyBytes := []byte(key)
replicaCh := make(chan bool)
persistCh := make(chan bool)
for repId := 0; repId < numServers; repId++ {
go b.observeOne(keyBytes, mt, cas, forDelete, repId, replicaCh, persistCh)
}
results := int(0)
replicas := uint(0)
persists := uint(0)
for {
select {
case rV := <-replicaCh:
if rV {
replicas++
}
results++
case pV := <-persistCh:
if pV {
persists++
}
results++
}
if replicas >= replicaTo && persists >= persistTo {
return nil
} else if results == ((numServers * 2) - 1) {
return &clientError{"Failed to meet durability requirements in time."}
}
}
}
// Touches a document, specifying a new expiry time for it. Additionally checks document durability.
func (b *Bucket) TouchDura(key string, cas Cas, expiry uint32, replicateTo, persistTo uint) (Cas, error) {
cas, mt, err := b.touch(key, cas, expiry)
if err != nil {
return cas, err
}
return cas, b.durability(key, cas, mt, replicateTo, persistTo, false)
}
// Removes a document from the bucket. Additionally checks document durability.
func (b *Bucket) RemoveDura(key string, cas Cas, replicateTo, persistTo uint) (Cas, error) {
cas, mt, err := b.remove(key, cas)
if err != nil {
return cas, err
}
return cas, b.durability(key, cas, mt, replicateTo, persistTo, true)
}
// Inserts or replaces a document in the bucket. Additionally checks document durability.
func (b *Bucket) UpsertDura(key string, value interface{}, expiry uint32, replicateTo, persistTo uint) (Cas, error) {
cas, mt, err := b.upsert(key, value, expiry)
if err != nil {
return cas, err
}
return cas, b.durability(key, cas, mt, replicateTo, persistTo, false)
}
// Inserts a new document to the bucket. Additionally checks document durability.
func (b *Bucket) InsertDura(key string, value interface{}, expiry uint32, replicateTo, persistTo uint) (Cas, error) {
cas, mt, err := b.insert(key, value, expiry)
if err != nil {
return cas, err
}
return cas, b.durability(key, cas, mt, replicateTo, persistTo, false)
}
// Replaces a document in the bucket. Additionally checks document durability.
func (b *Bucket) ReplaceDura(key string, value interface{}, cas Cas, expiry uint32, replicateTo, persistTo uint) (Cas, error) {
cas, mt, err := b.replace(key, value, cas, expiry)
if err != nil {
return cas, err
}
return cas, b.durability(key, cas, mt, replicateTo, persistTo, false)
}
// Appends a string value to a document. Additionally checks document durability.
func (b *Bucket) AppendDura(key, value string, replicateTo, persistTo uint) (Cas, error) {
cas, mt, err := b.append(key, value)
if err != nil {
return cas, err
}
return cas, b.durability(key, cas, mt, replicateTo, persistTo, false)
}
// Prepends a string value to a document. Additionally checks document durability.
func (b *Bucket) PrependDura(key, value string, replicateTo, persistTo uint) (Cas, error) {
cas, mt, err := b.prepend(key, value)
if err != nil {
return cas, err
}
return cas, b.durability(key, cas, mt, replicateTo, persistTo, false)
}
// Performs an atomic addition or subtraction for an integer document. Additionally checks document durability.
func (b *Bucket) CounterDura(key string, delta, initial int64, expiry uint32, replicateTo, persistTo uint) (uint64, Cas, error) {
val, cas, mt, err := b.counter(key, delta, initial, expiry)
if err != nil {
return val, cas, err
}
return val, cas, b.durability(key, cas, mt, replicateTo, persistTo, false)
}