-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathkvopmanager.go
335 lines (269 loc) · 7.03 KB
/
kvopmanager.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
package gocb
import (
"time"
gocbcore "github.com/couchbase/gocbcore/v9"
"github.com/couchbase/gocbcore/v9/memd"
"github.com/pkg/errors"
)
type kvOpManager struct {
parent *Collection
signal chan struct{}
err error
wasResolved bool
mutationToken *MutationToken
span RequestSpan
documentID string
transcoder Transcoder
timeout time.Duration
deadline time.Time
bytes []byte
flags uint32
persistTo uint
replicateTo uint
durabilityLevel DurabilityLevel
retryStrategy *retryStrategyWrapper
cancelCh chan struct{}
impersonate []byte
operationName string
createdTime time.Time
meter Meter
}
func (m *kvOpManager) getTimeout() time.Duration {
if m.timeout > 0 {
if m.durabilityLevel > 0 && m.timeout < durabilityTimeoutFloor {
m.timeout = durabilityTimeoutFloor
logWarnf("Durable operation in use so timeout value coerced up to %s", m.timeout.String())
}
return m.timeout
}
defaultTimeout := m.parent.timeoutsConfig.KVTimeout
if m.durabilityLevel > DurabilityLevelMajority || m.persistTo > 0 {
defaultTimeout = m.parent.timeoutsConfig.KVDurableTimeout
}
if m.durabilityLevel > 0 && defaultTimeout < durabilityTimeoutFloor {
defaultTimeout = durabilityTimeoutFloor
logWarnf("Durable operation in user so timeout value coerced up to %s", defaultTimeout.String())
}
return defaultTimeout
}
func (m *kvOpManager) SetDocumentID(id string) {
m.documentID = id
}
func (m *kvOpManager) SetCancelCh(cancelCh chan struct{}) {
m.cancelCh = cancelCh
}
func (m *kvOpManager) SetTimeout(timeout time.Duration) {
m.timeout = timeout
}
func (m *kvOpManager) SetTranscoder(transcoder Transcoder) {
if transcoder == nil {
transcoder = m.parent.transcoder
}
m.transcoder = transcoder
}
func (m *kvOpManager) SetValue(val interface{}) {
if m.err != nil {
return
}
if m.transcoder == nil {
m.err = errors.New("Expected a transcoder to be specified first")
return
}
espan := m.parent.startKvOpTrace("request_encoding", m.span.Context(), true)
defer espan.End()
bytes, flags, err := m.transcoder.Encode(val)
if err != nil {
m.err = err
return
}
m.bytes = bytes
m.flags = flags
}
func (m *kvOpManager) SetDuraOptions(persistTo, replicateTo uint, level DurabilityLevel) {
if persistTo != 0 || replicateTo != 0 {
if !m.parent.useMutationTokens {
m.err = makeInvalidArgumentsError("cannot use observe based durability without mutation tokens")
return
}
if level > 0 {
m.err = makeInvalidArgumentsError("cannot mix observe based durability and synchronous durability")
return
}
}
m.persistTo = persistTo
m.replicateTo = replicateTo
m.durabilityLevel = level
}
func (m *kvOpManager) SetRetryStrategy(retryStrategy RetryStrategy) {
wrapper := m.parent.retryStrategyWrapper
if retryStrategy != nil {
wrapper = newRetryStrategyWrapper(retryStrategy)
}
m.retryStrategy = wrapper
}
func (m *kvOpManager) SetImpersonate(user []byte) {
m.impersonate = user
}
func (m *kvOpManager) Finish(noMetrics bool) {
m.span.End()
if !noMetrics {
recorder, err := m.meter.ValueRecorder(meterNameCBOperations, map[string]string{
meterAttribServiceKey: meterValueServiceKV,
meterAttribOperationKey: m.operationName,
})
if err != nil {
logDebugf("Failed to create value recorder: %v", err)
return
}
recorder.RecordValue(uint64(time.Since(m.createdTime).Microseconds()))
}
}
func (m *kvOpManager) TraceSpanContext() RequestSpanContext {
return m.span.Context()
}
func (m *kvOpManager) TraceSpan() RequestSpan {
return m.span
}
func (m *kvOpManager) DocumentID() []byte {
return []byte(m.documentID)
}
func (m *kvOpManager) CollectionName() string {
return m.parent.name()
}
func (m *kvOpManager) ScopeName() string {
return m.parent.ScopeName()
}
func (m *kvOpManager) BucketName() string {
return m.parent.bucketName()
}
func (m *kvOpManager) ValueBytes() []byte {
return m.bytes
}
func (m *kvOpManager) ValueFlags() uint32 {
return m.flags
}
func (m *kvOpManager) Transcoder() Transcoder {
return m.transcoder
}
func (m *kvOpManager) DurabilityLevel() memd.DurabilityLevel {
return memd.DurabilityLevel(m.durabilityLevel)
}
func (m *kvOpManager) DurabilityTimeout() time.Duration {
if m.durabilityLevel == 0 {
return 0
}
timeout := m.getTimeout()
duraTimeout := time.Duration(float64(timeout) * 0.9)
if duraTimeout < durabilityTimeoutFloor {
duraTimeout = durabilityTimeoutFloor
}
return duraTimeout
}
func (m *kvOpManager) Deadline() time.Time {
if m.deadline.IsZero() {
timeout := m.getTimeout()
m.deadline = time.Now().Add(timeout)
}
return m.deadline
}
func (m *kvOpManager) RetryStrategy() *retryStrategyWrapper {
return m.retryStrategy
}
func (m *kvOpManager) Impersonate() []byte {
return m.impersonate
}
func (m *kvOpManager) CheckReadyForOp() error {
if m.err != nil {
return m.err
}
if m.getTimeout() == 0 {
return errors.New("op manager had no timeout specified")
}
return nil
}
func (m *kvOpManager) NeedsObserve() bool {
return m.persistTo > 0 || m.replicateTo > 0
}
func (m *kvOpManager) EnhanceErr(err error) error {
return maybeEnhanceCollKVErr(err, nil, m.parent, m.documentID)
}
func (m *kvOpManager) EnhanceMt(token gocbcore.MutationToken) *MutationToken {
if token.VbUUID != 0 {
return &MutationToken{
token: token,
bucketName: m.BucketName(),
}
}
return nil
}
func (m *kvOpManager) Reject() {
m.signal <- struct{}{}
}
func (m *kvOpManager) Resolve(token *MutationToken) {
m.wasResolved = true
m.mutationToken = token
m.signal <- struct{}{}
}
func (m *kvOpManager) Wait(op gocbcore.PendingOp, err error) error {
if err != nil {
return err
}
if m.err != nil {
op.Cancel()
}
select {
case <-m.signal:
// Good to go
case <-m.cancelCh:
op.Cancel()
<-m.signal
}
if m.wasResolved && (m.persistTo > 0 || m.replicateTo > 0) {
if m.mutationToken == nil {
return errors.New("expected a mutation token")
}
return m.parent.waitForDurability(
m.span,
m.documentID,
m.mutationToken.token,
m.replicateTo,
m.persistTo,
m.Deadline(),
m.cancelCh,
m.impersonate,
)
}
return nil
}
func (c *Collection) newKvOpManager(opName string, parentSpan RequestSpan) *kvOpManager {
var tracectx RequestSpanContext
if parentSpan != nil {
tracectx = parentSpan.Context()
}
span := c.startKvOpTrace(opName, tracectx, false)
return &kvOpManager{
parent: c,
signal: make(chan struct{}, 1),
span: span,
operationName: opName,
createdTime: time.Now(),
meter: c.meter,
}
}
func durationToExpiry(dura time.Duration) uint32 {
// If the duration is 0, that indicates never-expires
if dura == 0 {
return 0
}
// If the duration is less than one second, we must force the
// value to 1 to avoid accidentally making it never expire.
if dura < 1*time.Second {
return 1
}
if dura < 30*24*time.Hour {
// Translate into a uint32 in seconds.
return uint32(dura / time.Second)
}
// Send the duration as a unix timestamp of now plus duration.
return uint32(time.Now().Add(dura).Unix())
}