/
kvopmanager_ps.go
284 lines (223 loc) · 6.22 KB
/
kvopmanager_ps.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
package gocb
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"google.golang.org/grpc/status"
"github.com/couchbase/goprotostellar/genproto/kv_v1"
)
// Contains information only useful to protostellar
type kvOpManagerPs struct {
parent *Collection
err error
span RequestSpan
documentID string
transcoder Transcoder
timeout time.Duration
bytes []byte
flags uint32
durabilityLevel *kv_v1.DurabilityLevel
retryStrategy RetryStrategy
ctx context.Context
isIdempotent bool
operationID string
operationName string
createdTime time.Time
meter *meterWrapper
compressor *compressor
}
func (m *kvOpManagerPs) getTimeout() time.Duration {
if m.timeout > 0 {
if m.durabilityLevel != nil && 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 != nil && *m.durabilityLevel > kv_v1.DurabilityLevel_DURABILITY_LEVEL_MAJORITY {
defaultTimeout = m.parent.timeoutsConfig.KVDurableTimeout
}
if m.durabilityLevel != nil && *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 *kvOpManagerPs) SetDocumentID(id string) {
m.documentID = id
}
func (m *kvOpManagerPs) SetTimeout(timeout time.Duration) {
m.timeout = timeout
}
func (m *kvOpManagerPs) SetTranscoder(transcoder Transcoder) {
if transcoder == nil {
transcoder = m.parent.transcoder
}
m.transcoder = transcoder
}
func (m *kvOpManagerPs) 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 *kvOpManagerPs) SetDuraOptions(level DurabilityLevel) {
if level == DurabilityLevelUnknown {
level = DurabilityLevelNone
}
m.durabilityLevel, m.err = level.toProtostellar()
if level > DurabilityLevelNone {
levelStr, err := level.toManagementAPI()
if err != nil {
logDebugf("Could not convert durability level to string: %v", err)
return
}
m.span.SetAttribute(spanAttribDBDurability, levelStr)
}
}
func (m *kvOpManagerPs) SetRetryStrategy(retryStrategy RetryStrategy) {
strat := m.parent.retryStrategyWrapper.wrapped
if retryStrategy != nil {
strat = retryStrategy
}
m.retryStrategy = strat
}
func (m *kvOpManagerPs) SetContext(ctx context.Context) {
if ctx == nil {
ctx = context.Background()
}
m.ctx = ctx
}
func (m *kvOpManagerPs) SetIsIdempotent(idempotent bool) {
m.isIdempotent = idempotent
}
func (m *kvOpManagerPs) Finish(noMetrics bool) {
m.span.End()
if !noMetrics {
m.meter.ValueRecord(meterValueServiceKV, m.operationName, m.createdTime)
}
}
func (m *kvOpManagerPs) TraceSpanContext() RequestSpanContext {
return m.span.Context()
}
func (m *kvOpManagerPs) TraceSpan() RequestSpan {
return m.span
}
func (m *kvOpManagerPs) DocumentID() string {
return m.documentID
}
func (m *kvOpManagerPs) CollectionName() string {
return m.parent.name()
}
func (m *kvOpManagerPs) ScopeName() string {
return m.parent.ScopeName()
}
func (m *kvOpManagerPs) BucketName() string {
return m.parent.bucketName()
}
func (m *kvOpManagerPs) Value() ([]byte, bool) {
return m.compressor.Compress(m.bytes)
}
// ValueBytes is necessary for now because protostellar doesn't support compression for all commands.
func (m *kvOpManagerPs) ValueBytes() []byte {
return m.bytes
}
func (m *kvOpManagerPs) ValueFlags() uint32 {
return m.flags
}
func (m *kvOpManagerPs) Transcoder() Transcoder {
return m.transcoder
}
func (m *kvOpManagerPs) DurabilityLevel() *kv_v1.DurabilityLevel {
return m.durabilityLevel
}
func (m *kvOpManagerPs) CompressionEnabled() *kv_v1.CompressionEnabled {
if m.compressor.CompressionEnabled {
compressionEnabledOptional := kv_v1.CompressionEnabled_COMPRESSION_ENABLED_OPTIONAL
return &compressionEnabledOptional
}
return nil
}
func (m *kvOpManagerPs) RetryStrategy() RetryStrategy {
return m.retryStrategy
}
func (m *kvOpManagerPs) IsIdempotent() bool {
return m.isIdempotent
}
func (m *kvOpManagerPs) 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 *kvOpManagerPs) OpName() string {
return m.operationName
}
func (m *kvOpManagerPs) CreatedAt() time.Time {
return m.createdTime
}
func (m *kvOpManagerPs) Tracer() RequestTracer {
return m.parent.tracer
}
func (m *kvOpManagerPs) RetryInfo() retriedRequestInfo {
return nil
}
func (m *kvOpManagerPs) SetRetryRequest(req *retriableRequestPs) {
// we don't store this as we don't need it.
}
func (m *kvOpManagerPs) OperationID() string {
return m.operationID
}
func (m *kvOpManagerPs) Context() context.Context {
return m.ctx
}
func (m *kvOpManagerPs) Timeout() time.Duration {
return m.getTimeout()
}
func (m *kvOpManagerPs) RetryReasonFor(err error) RetryReason {
if errors.Is(err, ErrDocumentLocked) {
return KVLockedRetryReason
} else if errors.Is(err, ErrServiceNotAvailable) {
return ServiceNotAvailableRetryReason
}
return nil
}
func (m *kvOpManagerPs) EnhanceErrorStatus(st *status.Status, readOnly bool) error {
return mapPsErrorStatusToGocbError(st, readOnly)
}
func (m *kvOpManagerPs) EnhanceErr(err error, readOnly bool) error {
return mapPsErrorToGocbError(err, readOnly)
}
func newKvOpManagerPs(c *Collection, opName string, parentSpan RequestSpan) *kvOpManagerPs {
var tracectx RequestSpanContext
if parentSpan != nil {
tracectx = parentSpan.Context()
}
span := c.startKvOpTrace(opName, tracectx, false)
return &kvOpManagerPs{
parent: c,
span: span,
operationName: opName,
createdTime: time.Now(),
meter: c.meter,
operationID: uuid.NewString()[:6],
compressor: c.compressor,
}
}