-
Notifications
You must be signed in to change notification settings - Fork 104
/
collection_dura.go
189 lines (164 loc) · 4.28 KB
/
collection_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
package gocb
import (
"context"
"sync"
"time"
gocbcore "github.com/couchbase/gocbcore/v10"
)
func (p *kvProviderCore) observeOnceSeqNo(
ctx context.Context,
c *Collection,
trace RequestSpan,
docID string,
mt gocbcore.MutationToken,
replicaIdx int,
cancelCh chan struct{},
timeout time.Duration,
user string,
) (didReplicate, didPersist bool, errOut error) {
observedOpm := newKvOpManagerCore(c, "observe_once", trace, p)
defer observedOpm.Finish(true)
observedOpm.SetDocumentID(docID)
observedOpm.SetCancelCh(cancelCh)
observedOpm.SetTimeout(timeout)
observedOpm.SetImpersonate(user)
observedOpm.SetContext(ctx)
err := observedOpm.Wait(p.agent.ObserveVb(gocbcore.ObserveVbOptions{
VbID: mt.VbID,
VbUUID: mt.VbUUID,
ReplicaIdx: replicaIdx,
TraceContext: observedOpm.TraceSpanContext(),
Deadline: observedOpm.Deadline(),
User: observedOpm.Impersonate(),
}, func(res *gocbcore.ObserveVbResult, err error) {
if err != nil || res == nil {
errOut = observedOpm.EnhanceErr(err)
observedOpm.Reject()
return
}
didReplicate = res.CurrentSeqNo >= mt.SeqNo
didPersist = res.PersistSeqNo >= mt.SeqNo
observedOpm.Resolve(nil)
}))
if err == nil {
errOut = err
}
return
}
func (p *kvProviderCore) observeOne(
ctx context.Context,
c *Collection,
trace RequestSpan,
docID string,
mt gocbcore.MutationToken,
replicaIdx int,
replicaCh, persistCh, cancelCh chan struct{},
timeout time.Duration,
user string,
) {
sentReplicated := false
sentPersisted := false
calc := gocbcore.ExponentialBackoff(10*time.Microsecond, 100*time.Millisecond, 0)
retries := uint32(0)
ObserveLoop:
for {
select {
case <-cancelCh:
break ObserveLoop
default:
// not cancelled yet
}
didReplicate, didPersist, err := p.observeOnceSeqNo(ctx, c, trace, docID, mt, replicaIdx, cancelCh, timeout, user)
if err != nil {
logDebugf("ObserveOnce failed unexpected: %s", err)
return
}
if didReplicate && !sentReplicated {
replicaCh <- struct{}{}
sentReplicated = true
}
if didPersist && !sentPersisted {
persistCh <- struct{}{}
sentPersisted = true
}
// If we've got persisted and replicated, we can just stop
if sentPersisted && sentReplicated {
break ObserveLoop
}
waitTmr := gocbcore.AcquireTimer(calc(retries))
retries++
select {
case <-waitTmr.C:
gocbcore.ReleaseTimer(waitTmr, true)
case <-cancelCh:
gocbcore.ReleaseTimer(waitTmr, false)
}
}
}
func (p *kvProviderCore) waitForDurability(
ctx context.Context,
c *Collection,
trace RequestSpan,
docID string,
mt gocbcore.MutationToken,
replicateTo uint,
persistTo uint,
deadline time.Time,
cancelCh chan struct{},
user string,
) error {
observeOpm := newKvOpManagerCore(c, "observe", trace, p)
defer observeOpm.Finish(true)
observeOpm.SetDocumentID(docID)
snapshot, err := p.snapshotProvider.WaitForConfigSnapshot(ctx, deadline)
if err != nil {
return err
}
numReplicas, err := snapshot.NumReplicas()
if err != nil {
return err
}
numServers := numReplicas + 1
if replicateTo > uint(numServers-1) || persistTo > uint(numServers) {
return observeOpm.EnhanceErr(ErrDurabilityImpossible)
}
subOpCancelCh := make(chan struct{}, 1)
replicaCh := make(chan struct{}, numServers)
persistCh := make(chan struct{}, numServers)
// If we cancel the sub ops then we need to wait for cancellation to complete before we exit, otherwise
// we will attempt to close our span before the child spans complete.
var wg sync.WaitGroup
for replicaIdx := 0; replicaIdx < numServers; replicaIdx++ {
wg.Add(1)
go func(ridx int) {
p.observeOne(ctx, c, observeOpm.TraceSpan(), docID, mt, ridx, replicaCh, persistCh, subOpCancelCh,
time.Until(deadline), user)
wg.Done()
}(replicaIdx)
}
numReplicated := uint(0)
numPersisted := uint(0)
for {
select {
case <-replicaCh:
numReplicated++
case <-persistCh:
numPersisted++
case <-time.After(time.Until(deadline)):
// deadline exceeded
close(subOpCancelCh)
wg.Wait()
return observeOpm.EnhanceErr(ErrAmbiguousTimeout)
case <-cancelCh:
// parent asked for cancellation
close(subOpCancelCh)
wg.Wait()
return observeOpm.EnhanceErr(ErrRequestCanceled)
}
if numReplicated >= replicateTo && numPersisted >= persistTo {
close(subOpCancelCh)
wg.Wait()
return nil
}
}
}