forked from lni/dragonboat
/
snapshot.go
371 lines (355 loc) · 10.8 KB
/
snapshot.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
//
//
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// This file contains code derived from CockroachDB. The asyncSendSnapshot method,
// connectAndProcessSnapshot method and the processSnapshotQueue method is similar
// to the one used in CockroachDB.
package transport
import (
"errors"
"sync/atomic"
"github.com/lni/dragonboat/internal/settings"
"github.com/lni/dragonboat/internal/utils/fileutil"
"github.com/lni/dragonboat/internal/utils/logutil"
"github.com/lni/dragonboat/raftio"
pb "github.com/lni/dragonboat/raftpb"
)
var (
maxSnapshotCount = settings.Soft.MaxSnapshotCount
)
// ASyncSendSnapshot sends raft snapshot message to its target.
func (t *Transport) ASyncSendSnapshot(m pb.Message) bool {
if !t.asyncSendSnapshot(m) {
plog.Debugf("failed to send snapshot to %s",
logutil.DescribeNode(m.ClusterId, m.To))
t.sendSnapshotNotification(m.ClusterId, m.To, true)
return false
}
return true
}
func (t *Transport) asyncSendSnapshot(m pb.Message) bool {
toNodeID := m.To
clusterID := m.ClusterId
if m.Type != pb.InstallSnapshot {
panic("non-snapshot message received by ASyncSendSnapshot")
}
count := uint64(t.increaseSnapshotCount())
if count > maxSnapshotCount {
return false
}
chunks := splitSnapshotMessage(m)
if uint64(len(chunks)) > snapSendBufSize {
// FIXME:
// what users can do, what we can do to prevent from reaching here?
panic("len(chunks) > snapSendBufSize, oversized snapshot image")
}
addr, key, err := t.resolver.Resolve(clusterID, toNodeID)
if err != nil {
return false
}
// fail fast
if !t.GetCircuitBreaker(addr).Ready() {
return false
}
t.mu.Lock()
ch, ok := t.mu.chunks[key]
closeCh := t.mu.chunksClose[key]
if !ok {
ch = make(chan pb.SnapshotChunk, snapSendBufSize)
closeCh = make(chan struct{})
t.mu.chunks[key] = ch
t.mu.chunksClose[key] = closeCh
}
t.mu.Unlock()
if !ok {
shutdownQueue := func() {
t.mu.Lock()
t.snapshotQueueMu.Lock()
close(closeCh)
t.cleanup(ch)
t.snapshotQueueMu.Unlock()
delete(t.mu.chunks, key)
delete(t.mu.chunksClose, key)
t.mu.Unlock()
}
t.stopper.RunWorker(func() {
t.connectAndProcessSnapshot(clusterID, toNodeID, addr, ch)
plog.Infof("%s connectAndProcessSnapshot returned",
logutil.DescribeNode(clusterID, toNodeID))
shutdownQueue()
})
}
t.snapshotQueueMu.Lock()
defer t.snapshotQueueMu.Unlock()
availableSlot := cap(ch) - len(ch)
if availableSlot < len(chunks) {
plog.Infof("not enough slots, cap %d, ch len %d, want %d",
cap(ch), len(ch), len(chunks))
// fail fast
// notify raft that the snapshot failed.
return false
}
// now we can ensure everything in ch are continuous chunks from the same
// snapshot image.
for i := range chunks {
select {
case <-closeCh:
return false
default:
}
chunk := chunks[i]
select {
case ch <- chunk:
default:
panic("snapshot channel not suppose to be full")
}
}
return true
}
func (t *Transport) connectAndProcessSnapshot(clusterID uint64,
toNodeID uint64, remoteHost string, ch <-chan pb.SnapshotChunk) {
breaker := t.GetCircuitBreaker(remoteHost)
successes := breaker.Successes()
consecFailures := breaker.ConsecFailures()
if err := func() error {
conn, err := t.raftRPC.GetSnapshotConnection(t.ctx, remoteHost)
if err != nil {
plog.Warningf("failed to get snapshot client to %s",
logutil.DescribeNode(clusterID, toNodeID))
t.sendSnapshotNotification(clusterID, toNodeID, true)
return err
}
defer conn.Close()
breaker.Success()
if successes == 0 || consecFailures > 0 {
plog.Infof("snapshot stream to %s (%s) established",
logutil.DescribeNode(clusterID, toNodeID), remoteHost)
}
return t.processSnapshotQueue(ch, conn)
}(); err != nil {
plog.Warningf("connectAndProcessSnapshot failed: %v", err)
breaker.Fail()
}
}
func (t *Transport) sendSnapshotChunk(c pb.SnapshotChunk,
conn raftio.ISnapshotConnection) error {
v := t.preStreamChunkSend.Load()
if v != nil {
updated, shouldSend := v.(StreamChunkSendFunc)(c)
if !shouldSend {
return errChunkSendSkipped
}
return conn.SendSnapshotChunk(updated)
}
return conn.SendSnapshotChunk(c)
}
func (t *Transport) processSnapshotQueue(ch <-chan pb.SnapshotChunk,
conn raftio.ISnapshotConnection) error {
chunks := make([]pb.SnapshotChunk, 0)
var deploymentIDSet bool
var deploymentID uint64
idx := uint64(0)
for {
select {
case <-t.stopper.ShouldStop():
plog.Infof("processSnapshotQueue is going to exit")
return nil
case chunk := <-ch:
// everything in ch are continuous chunks
if len(chunks) == 0 && chunk.ChunkId != 0 {
panic("chunk alignment error")
}
chunks = append(chunks, chunk)
if uint64(len(chunks)) > snapSendBufSize {
panic("chunk count > snapSendBufSize")
}
// all chunks have been pulled from the ch
if chunk.ChunkId+1 == chunk.ChunkCount {
// drop the message if deployment id is not set.
if !deploymentIDSet {
if t.deploymentIDSet() {
deploymentIDSet = true
deploymentID = t.getDeploymentID()
plog.Infof("Deployment id is %d", deploymentID)
}
}
if !deploymentIDSet {
plog.Warningf("snapshot to %s dropped, deployment id not set",
logutil.DescribeNode(chunk.ClusterId, chunk.NodeId))
t.sendSnapshotNotification(chunk.ClusterId, chunk.NodeId, true)
continue
}
failed := false
for _, curChunk := range chunks {
chunkData := make([]byte, snapChunkSize)
idx++
data, err := loadSnapshotChunkData(curChunk, chunkData)
if err != nil {
// for whatever reason, we failed to get the current chunk data
// report this snapshot as failed. the connection is still good,
// everything in ch should continue
plog.Errorf("failed to read the snapshot chunk, %v", err)
failed = true
break
}
curChunk.Data = data
curChunk.DeploymentId = deploymentID
if err := t.sendSnapshotChunk(curChunk, conn); err != nil {
plog.Debugf("snapshot to %s failed",
logutil.DescribeNode(chunk.ClusterId, chunk.NodeId))
t.sendSnapshotNotification(chunk.ClusterId, chunk.NodeId, true)
return err
}
// when t.streamChunkSent is set, it is called after each successful
// chunk sent
if t.streamChunkSent != nil {
t.streamChunkSent(curChunk)
}
}
plog.Debugf("snapshot to %s processed, failed to send value %t",
logutil.DescribeNode(chunk.ClusterId, chunk.NodeId), failed)
t.sendSnapshotNotification(chunk.ClusterId, chunk.NodeId, failed)
chunks = make([]pb.SnapshotChunk, 0)
}
}
}
}
func (t *Transport) cleanup(ch <-chan pb.SnapshotChunk) {
for {
select {
case chunk := <-ch:
if chunk.ChunkId == 0 {
plog.Debugf("snapshot channel cleanup called, snapshot to %s failed",
logutil.DescribeNode(chunk.ClusterId, chunk.NodeId))
t.sendSnapshotNotification(chunk.ClusterId, chunk.NodeId, true)
}
default:
return
}
}
}
func (t *Transport) getSnapshotCount() int32 {
return atomic.LoadInt32(&t.snapshotCount)
}
func (t *Transport) increaseSnapshotCount() int32 {
return atomic.AddInt32(&t.snapshotCount, 1)
}
func (t *Transport) decreaseSnapshotCount() {
// the time-of-check-to-time-of-use issue won't cause real problem
// here as we are trying to enforce an upper limit only and that
// upper limit doesn't have to be 100% precise
v := atomic.AddInt32(&t.snapshotCount, -1)
if v < 0 {
atomic.StoreInt32(&t.snapshotCount, 0)
}
}
func splitBySnapshotFile(msg pb.Message,
filepath string, filesize uint64, startChunkID uint64,
sf *pb.SnapshotFile) []pb.SnapshotChunk {
if filesize == 0 {
panic("empty file included in snapshot")
}
results := make([]pb.SnapshotChunk, 0)
chunkCount := uint64((filesize-1)/snapChunkSize + 1)
plog.Infof("splitBySnapshotFile called, chunkCount %d, filesize %d",
chunkCount, filesize)
for i := uint64(0); i < chunkCount; i++ {
var csz uint64
if i == chunkCount-1 {
csz = filesize - (chunkCount-1)*snapChunkSize
} else {
csz = snapChunkSize
}
c := pb.SnapshotChunk{
BinVer: raftio.RPCBinVersion,
ClusterId: msg.ClusterId,
NodeId: msg.To,
From: msg.From,
FileChunkId: i,
FileChunkCount: chunkCount,
ChunkId: startChunkID + i,
ChunkSize: csz,
Index: msg.Snapshot.Index,
Term: msg.Snapshot.Term,
Membership: msg.Snapshot.Membership,
Filepath: filepath,
FileSize: filesize,
}
if sf != nil {
c.HasFileInfo = true
c.FileInfo = *sf
}
results = append(results, c)
}
return results
}
func splitSnapshotMessage(m pb.Message) []pb.SnapshotChunk {
if m.Type != pb.InstallSnapshot {
panic("not a snapshot message")
}
startChunkID := uint64(0)
results := splitBySnapshotFile(m,
m.Snapshot.Filepath, m.Snapshot.FileSize, startChunkID, nil)
startChunkID += uint64(len(results))
for _, snapshotFile := range m.Snapshot.Files {
chunks := splitBySnapshotFile(m,
snapshotFile.Filepath, snapshotFile.FileSize, startChunkID, snapshotFile)
results = append(results, chunks...)
startChunkID += uint64(len(chunks))
}
for idx := range results {
results[idx].ChunkCount = uint64(len(results))
}
return results
}
func loadSnapshotChunkData(chunk pb.SnapshotChunk,
data []byte) ([]byte, error) {
f, err := fileutil.OpenChunkFileForRead(chunk.Filepath)
if err != nil {
return nil, err
}
defer f.Close()
offset := chunk.FileChunkId * snapChunkSize
if _, err = f.SeekFromBeginning(int64(offset)); err != nil {
return nil, err
}
if chunk.ChunkSize != uint64(len(data)) {
data = make([]byte, chunk.ChunkSize)
}
n, err := f.Read(data)
if err != nil {
return nil, err
}
if uint64(n) != chunk.ChunkSize {
return nil, errors.New("failed to read the chunk from snapshot file")
}
return data, nil
}