forked from tiglabs/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_replicate.go
253 lines (226 loc) · 5.53 KB
/
transport_replicate.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
// Copyright 2018 The tiglabs raft 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.
package raft
import (
"encoding/binary"
"fmt"
"io"
"net"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/tiglabs/raft/logger"
"github.com/tiglabs/raft/proto"
"github.com/tiglabs/raft/util"
)
type replicateTransport struct {
config *TransportConfig
raftServer *RaftServer
listener net.Listener
curSnapshot int32
mu sync.RWMutex
senders map[uint64]*transportSender
stopc chan struct{}
}
func newReplicateTransport(raftServer *RaftServer, config *TransportConfig) (*replicateTransport, error) {
var (
listener net.Listener
err error
)
if listener, err = net.Listen("tcp", config.ReplicateAddr); err != nil {
return nil, err
}
t := &replicateTransport{
config: config,
raftServer: raftServer,
listener: listener,
senders: make(map[uint64]*transportSender),
stopc: make(chan struct{}),
}
return t, nil
}
func (t *replicateTransport) stop() {
t.mu.Lock()
defer t.mu.Unlock()
select {
case <-t.stopc:
return
default:
close(t.stopc)
t.listener.Close()
for _, s := range t.senders {
s.stop()
}
}
}
func (t *replicateTransport) send(m *proto.Message) {
s := t.getSender(m.To)
s.send(m)
}
func (t *replicateTransport) getSender(nodeId uint64) *transportSender {
t.mu.RLock()
sender, ok := t.senders[nodeId]
t.mu.RUnlock()
if ok {
return sender
}
t.mu.Lock()
defer t.mu.Unlock()
if sender, ok = t.senders[nodeId]; !ok {
sender = newTransportSender(nodeId, uint64(t.config.MaxReplConcurrency), t.config.SendBufferSize, Replicate, t.config.Resolver)
t.senders[nodeId] = sender
}
return sender
}
func (t *replicateTransport) sendSnapshot(m *proto.Message, rs *snapshotStatus) {
var (
conn *util.ConnTimeout
err error
)
defer func() {
atomic.AddInt32(&t.curSnapshot, -1)
rs.respond(err)
if conn != nil {
conn.Close()
}
if err != nil {
logger.Error("[Transport] %v send snapshot to %v failed error is: %v.", m.ID, m.To, err)
} else if logger.IsEnableWarn() {
logger.Warn("[Transport] %v send snapshot to %v successful.", m.ID, m.To)
}
}()
if atomic.AddInt32(&t.curSnapshot, 1) > int32(t.config.MaxSnapConcurrency) {
err = fmt.Errorf("snapshot concurrency exceed the limit %v.", t.config.MaxSnapConcurrency)
return
}
if conn = getConn(m.To, Replicate, t.config.Resolver, 10*time.Minute, 1*time.Minute); conn == nil {
err = fmt.Errorf("can't get connection to %v.", m.To)
return
}
// send snapshot header message
bufWr := util.NewBufferWriter(conn, 1*MB)
if err = m.Encode(bufWr); err != nil {
return
}
if err = bufWr.Flush(); err != nil {
return
}
// send snapshot data
var (
data []byte
loopCount = 0
sizeBuf = make([]byte, 4)
)
for err == nil {
loopCount = loopCount + 1
if loopCount > 16 {
loopCount = 0
runtime.Gosched()
}
select {
case <-rs.stopCh:
err = fmt.Errorf("raft has shutdown.")
default:
data, err = m.Snapshot.Next()
if len(data) > 0 {
// write block size
binary.BigEndian.PutUint32(sizeBuf, uint32(len(data)))
if _, err = bufWr.Write(sizeBuf); err == nil {
_, err = bufWr.Write(data)
}
}
}
}
// write end flag and flush
if err != nil && err != io.EOF {
return
}
binary.BigEndian.PutUint32(sizeBuf, 0)
if _, err = bufWr.Write(sizeBuf); err != nil {
return
}
if err = bufWr.Flush(); err != nil {
return
}
// wait response
err = nil
resp := make([]byte, 1)
io.ReadFull(conn, resp)
if resp[0] != 1 {
err = fmt.Errorf("follower response failed.")
}
}
func (t *replicateTransport) start() {
util.RunWorkerUtilStop(func() {
for {
select {
case <-t.stopc:
return
default:
conn, err := t.listener.Accept()
if err != nil {
continue
}
t.handleConn(util.NewConnTimeout(conn))
}
}
}, t.stopc)
}
func (t *replicateTransport) handleConn(conn *util.ConnTimeout) {
util.RunWorker(func() {
defer conn.Close()
loopCount := 0
bufRd := util.NewBufferReader(conn, 16*KB)
for {
loopCount = loopCount + 1
if loopCount > 16 {
loopCount = 0
runtime.Gosched()
}
select {
case <-t.stopc:
return
default:
if msg, err := reciveMessage(bufRd); err != nil {
return
} else {
//logger.Debug(fmt.Sprintf("Recive %v from (%v)", msg.ToString(), conn.RemoteAddr()))
if msg.Type == proto.ReqMsgSnapShot {
if err := t.handleSnapshot(msg, conn, bufRd); err != nil {
return
}
} else {
t.raftServer.reciveMessage(msg)
}
}
}
}
})
}
var snap_ack = []byte{1}
func (t *replicateTransport) handleSnapshot(m *proto.Message, conn *util.ConnTimeout, bufRd *util.BufferReader) error {
conn.SetReadTimeout(time.Minute)
conn.SetWriteTimeout(15 * time.Second)
bufRd.Grow(1 * MB)
req := newSnapshotRequest(m, bufRd)
t.raftServer.reciveSnapshot(req)
// wait snapshot result
if err := req.response(); err != nil {
logger.Error("[Transport] handle snapshot request from %v error: %v.", m.From, err)
return err
}
_, err := conn.Write(snap_ack)
return err
}