forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agentops_dura.go
207 lines (180 loc) · 5.03 KB
/
agentops_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
package gocbcore
import (
"encoding/binary"
"github.com/opentracing/opentracing-go"
)
// ObserveOptions encapsulates the parameters for a ObserveEx operation.
type ObserveOptions struct {
Key []byte
ReplicaIdx int
TraceContext opentracing.SpanContext
}
// ObserveResult encapsulates the result of a ObserveEx operation.
type ObserveResult struct {
KeyState KeyState
Cas Cas
}
// ObserveExCallback is invoked upon completion of a ObserveEx operation.
type ObserveExCallback func(*ObserveResult, error)
// ObserveEx retrieves the current CAS and persistence state for a document.
func (agent *Agent) ObserveEx(opts ObserveOptions, cb ObserveExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("ObserveEx", opts.TraceContext)
if agent.bucketType() != bktTypeCouchbase {
tracer.Finish()
return nil, ErrNotSupported
}
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
if len(resp.Value) < 4 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
keyLen := int(binary.BigEndian.Uint16(resp.Value[2:]))
if len(resp.Value) != 2+2+keyLen+1+8 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
keyState := KeyState(resp.Value[2+2+keyLen])
cas := binary.BigEndian.Uint64(resp.Value[2+2+keyLen+1:])
tracer.Finish()
cb(&ObserveResult{
KeyState: keyState,
Cas: Cas(cas),
}, nil)
}
vbId := agent.KeyToVbucket(opts.Key)
keyLen := len(opts.Key)
valueBuf := make([]byte, 2+2+keyLen)
binary.BigEndian.PutUint16(valueBuf[0:], vbId)
binary.BigEndian.PutUint16(valueBuf[2:], uint16(keyLen))
copy(valueBuf[4:], opts.Key)
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdObserve,
Datatype: 0,
Cas: 0,
Extras: nil,
Key: nil,
Value: valueBuf,
Vbucket: vbId,
},
ReplicaIdx: opts.ReplicaIdx,
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}
// ObserveVbOptions encapsulates the parameters for a ObserveVbEx operation.
type ObserveVbOptions struct {
VbId uint16
VbUuid VbUuid
ReplicaIdx int
TraceContext opentracing.SpanContext
}
// ObserveVbResult encapsulates the result of a ObserveVbEx operation.
type ObserveVbResult struct {
DidFailover bool
VbId uint16
VbUuid VbUuid
PersistSeqNo SeqNo
CurrentSeqNo SeqNo
OldVbUuid VbUuid
LastSeqNo SeqNo
}
// ObserveVbExCallback is invoked upon completion of a ObserveVbEx operation.
type ObserveVbExCallback func(*ObserveVbResult, error)
// ObserveVbEx retrieves the persistence state sequence numbers for a particular VBucket
// and includes additional details not included by the basic version.
func (agent *Agent) ObserveVbEx(opts ObserveVbOptions, cb ObserveVbExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("ObserveVbEx", nil)
if agent.bucketType() != bktTypeCouchbase {
tracer.Finish()
return nil, ErrNotSupported
}
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
if len(resp.Value) < 1 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
formatType := resp.Value[0]
if formatType == 0 {
// Normal
if len(resp.Value) < 27 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
vbId := binary.BigEndian.Uint16(resp.Value[1:])
vbUuid := binary.BigEndian.Uint64(resp.Value[3:])
persistSeqNo := binary.BigEndian.Uint64(resp.Value[11:])
currentSeqNo := binary.BigEndian.Uint64(resp.Value[19:])
tracer.Finish()
cb(&ObserveVbResult{
DidFailover: false,
VbId: vbId,
VbUuid: VbUuid(vbUuid),
PersistSeqNo: SeqNo(persistSeqNo),
CurrentSeqNo: SeqNo(currentSeqNo),
}, nil)
return
} else if formatType == 1 {
// Hard Failover
if len(resp.Value) < 43 {
cb(nil, ErrProtocol)
return
}
vbId := binary.BigEndian.Uint16(resp.Value[1:])
vbUuid := binary.BigEndian.Uint64(resp.Value[3:])
persistSeqNo := binary.BigEndian.Uint64(resp.Value[11:])
currentSeqNo := binary.BigEndian.Uint64(resp.Value[19:])
oldVbUuid := binary.BigEndian.Uint64(resp.Value[27:])
lastSeqNo := binary.BigEndian.Uint64(resp.Value[35:])
tracer.Finish()
cb(&ObserveVbResult{
DidFailover: true,
VbId: vbId,
VbUuid: VbUuid(vbUuid),
PersistSeqNo: SeqNo(persistSeqNo),
CurrentSeqNo: SeqNo(currentSeqNo),
OldVbUuid: VbUuid(oldVbUuid),
LastSeqNo: SeqNo(lastSeqNo),
}, nil)
return
} else {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
}
valueBuf := make([]byte, 8)
binary.BigEndian.PutUint64(valueBuf[0:], uint64(opts.VbUuid))
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdObserveSeqNo,
Datatype: 0,
Cas: 0,
Extras: nil,
Key: nil,
Value: valueBuf,
Vbucket: opts.VbId,
},
ReplicaIdx: opts.ReplicaIdx,
Callback: handler,
RootTraceContext: tracer.RootContext(),
}
return agent.dispatchOp(req)
}