-
Notifications
You must be signed in to change notification settings - Fork 13
/
anon_slave.go
255 lines (210 loc) · 6.08 KB
/
anon_slave.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
package tarantool
import (
"io"
)
// AnonSlave connects to Tarantool >= 2.3.1 instance and subscribes for changes as anonymous replica.
// Tarantool instance acting as a master sees AnonSlave like anonymous replica.
// AnonSlave can't be used concurrently, route responses from returned channel instead.
type AnonSlave struct {
Slave
}
// NewAnonSlave returns new AnonSlave instance.
// URI is parsed by url package and therefore should contains any scheme supported by net.Dial.
func NewAnonSlave(uri string, opts ...Options) (as *AnonSlave, err error) {
s, err := NewSlave(uri, opts...)
if err != nil {
return nil, err
}
// check tarantool version. Anonymous replica support was added in Tarantool 2.3.1
if s.Version() < version2_3_1 {
return nil, ErrOldVersionAnon
}
return &AnonSlave{*s}, nil
}
// JoinWithSnap fetch snapshots from Master instance.
// Snapshot logs is available through the given out channel or returned PacketIterator.
// (In truth, Slave itself is returned in PacketIterator wrapper)
func (s *AnonSlave) JoinWithSnap(out ...chan *Packet) (it PacketIterator, err error) {
if err = s.fetchSnapshot(); err != nil {
return nil, err
}
// set iterator for the Next method
s.next = s.nextSnap
if s.isEmptyChan(out...) {
// no chan means synchronous snapshot scanning
return s, nil
}
defer close(out[0])
for s.HasNext() {
out[0] <- s.Packet()
}
return nil, s.Err()
}
// Join fetches snapshots using Master instance.
func (s *AnonSlave) Join() (err error) {
_, err = s.JoinWithSnap()
if err != nil {
return err
}
for s.HasNext() {
}
return s.Err()
}
// Subscribe for DML requests (insert, update, delete, replace, upsert) since vector clock.
// Variadic lsn is start vector clock. Each lsn is one clock in vector (sequentially).
// One lsn is enough for master-slave replica set.
// Subscribe sends requests asynchronously to out channel specified or use synchronous PacketIterator otherwise.
// For anonymous replica it is not necessary to call Join or JoinWithSnap before Subscribe.
func (s *AnonSlave) Subscribe(lsns ...uint64) (it PacketIterator, err error) {
if len(lsns) == 0 || len(lsns) >= VClockMax {
return nil, ErrVectorClock
}
if err = s.subscribe(lsns...); err != nil {
return nil, err
}
// set iterator for the Next method
s.next = s.nextXlog
// Start sending heartbeat messages to master
go s.heartbeat()
return s, nil
}
// Attach AnonSlave to Replica Set as an anonymous and subscribe for the new(!) DML requests.
// Attach calls Join and then Subscribe with VClock = s.VClock[1:]...
// If didn't call Join before Attach then you need to set VClock first either manually or using JoinWithSnap.
// Use out chan for asynchronous packet receiving or synchronous PacketIterator otherwise.
// If you need all requests in chan use JoinWithSnap(chan) and then s.Subscribe(s.VClock[1:]...).
func (s *AnonSlave) Attach(out ...chan *Packet) (it PacketIterator, err error) {
if err = s.Join(); err != nil {
return nil, err
}
// skip reserved zero index of the Vector Clock
if len(s.VClock) <= 1 {
return nil, ErrVectorClock
}
if it, err = s.Subscribe(s.VClock[1:]...); err != nil {
return nil, err
}
// no chan means synchronous dml request receiving
if s.isEmptyChan(out...) {
return it, nil
}
// consume new DML requests and send them to the given chan
go func(out chan *Packet) {
defer close(out)
for s.HasNext() {
out <- s.Packet()
}
}(out[0])
// return nil iterator to avoid concurrent using of the Next method
return nil, nil
}
func (s *AnonSlave) fetchSnapshot() (err error) {
pp, err := s.newPacket(&FetchSnapshot{})
if err != nil {
return
}
if err = s.send(pp); err != nil {
return err
}
s.c.releasePacket(pp)
if pp, err = s.receive(); err != nil {
return err
}
defer pp.Release()
p := &Packet{}
if err := p.UnmarshalBinary(pp.body); err != nil {
return err
}
if p.Cmd != OKCommand {
s.p = p
if p.Result == nil {
return ErrBadResult
}
return p.Result.Error
}
v := new(VClock)
_, err = v.UnmarshalMsg(pp.body)
if err != nil {
return err
}
s.VClock = v.VClock
return nil
}
// subscribe sends SUBSCRIBE request and waits for VCLOCK response.
func (s *AnonSlave) subscribe(lsns ...uint64) error {
vc := NewVectorClock(lsns...)
pp, err := s.newPacket(&Subscribe{
UUID: s.UUID,
ReplicaSetUUID: s.ReplicaSet.UUID,
VClock: vc,
Anon: true,
})
if err != nil {
return err
}
if err = s.send(pp); err != nil {
return err
}
s.c.releasePacket(pp)
if pp, err = s.receive(); err != nil {
return err
}
defer s.c.releasePacket(pp)
p := &pp.packet
err = p.UnmarshalBinary(pp.body)
if err != nil {
return err
}
sub := new(SubscribeResponse)
_, err = sub.UnmarshalMsg(pp.body)
if err != nil {
return err
}
// validate the response replica set UUID only if it is not empty
if s.ReplicaSet.UUID != "" && sub.ReplicaSetUUID != "" && s.ReplicaSet.UUID != sub.ReplicaSetUUID {
return NewUnexpectedReplicaSetUUIDError(s.ReplicaSet.UUID, sub.ReplicaSetUUID)
}
if sub.ReplicaSetUUID != "" {
s.ReplicaSet.UUID = sub.ReplicaSetUUID
}
s.VClock = sub.VClock
return nil
}
// nextSnap iterates responses on JOIN request.
// At the end it returns io.EOF error and nil packet.
// While iterating all
func (s *AnonSlave) nextSnap() (p *Packet, err error) {
pp, err := s.receive()
if err != nil {
return nil, err
}
defer s.c.releasePacket(pp)
p = &Packet{}
err = p.UnmarshalBinary(pp.body)
if err != nil {
return nil, err
}
// we have to parse snapshot logs to find replica set instances, UUID
switch p.Cmd {
case InsertCommand:
q := p.Request.(*Insert)
if q.Space == SpaceSchema {
key := q.Tuple[0].(string)
if key == SchemaKeyClusterUUID {
if s.ReplicaSet.UUID != "" && s.ReplicaSet.UUID != q.Tuple[1].(string) {
return nil, NewUnexpectedReplicaSetUUIDError(s.ReplicaSet.UUID, q.Tuple[1].(string))
}
s.ReplicaSet.UUID = q.Tuple[1].(string)
}
}
case OKCommand:
v := new(VClock)
_, err = v.UnmarshalMsg(pp.body)
if err != nil {
return nil, err
}
// ignore this VClock for anon replica
return nil, io.EOF
}
return p, nil
}