-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeer_transport.go
390 lines (321 loc) · 7.1 KB
/
peer_transport.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package prophet
import (
"errors"
"fmt"
"io"
"sync"
"github.com/fagongzi/goetty"
"github.com/fagongzi/util/task"
)
var (
errConnect = errors.New("not connected")
)
const (
hb byte = 0
hbACK byte = 1
remove byte = 2
)
type hbMsg struct {
res Resource
}
type hbACKMsg struct {
res Resource
peer Peer
}
type removeMsg struct {
id uint64
}
type shardCodec struct {
factory func() Resource
}
func (c *shardCodec) Decode(in *goetty.ByteBuf) (bool, interface{}, error) {
t := readByte(in)
switch t {
case hb:
msg := &hbMsg{
res: c.factory(),
}
data := readBytes(readInt(in), in)
err := msg.res.Unmarshal(data)
if err != nil {
return false, nil, err
}
return true, msg, nil
case hbACK:
msg := &hbACKMsg{
res: c.factory(),
}
msg.peer.ID = readUInt64(in)
msg.peer.ContainerID = readUInt64(in)
data := readBytes(readInt(in), in)
err := msg.res.Unmarshal(data)
if err != nil {
return false, nil, err
}
return true, msg, nil
case remove:
msg := &removeMsg{}
msg.id = readUInt64(in)
return true, msg, nil
}
return false, nil, fmt.Errorf("%d not support", t)
}
func (c *shardCodec) Encode(data interface{}, out *goetty.ByteBuf) error {
if msg, ok := data.(*hbMsg); ok {
data, err := msg.res.Marshal()
if err != nil {
return err
}
out.WriteByte(hb)
out.WriteInt(len(data))
out.Write(data)
} else if msg, ok := data.(*hbACKMsg); ok {
data, err := msg.res.Marshal()
if err != nil {
return err
}
out.WriteByte(hbACK)
out.WriteUInt64(msg.peer.ID)
out.WriteUInt64(msg.peer.ContainerID)
out.WriteInt(len(data))
out.Write(data)
} else if msg, ok := data.(*removeMsg); ok {
out.WriteByte(remove)
out.WriteUInt64(msg.id)
} else {
log.Fatalf("not support msg %T %+v",
data,
data)
}
return nil
}
func readUInt64(buf *goetty.ByteBuf) uint64 {
value, _ := buf.ReadUInt64()
return value
}
func readInt(buf *goetty.ByteBuf) int {
value, _ := buf.ReadInt()
return value
}
func readByte(buf *goetty.ByteBuf) byte {
value, _ := buf.ReadByte()
return value
}
func readBytes(n int, buf *goetty.ByteBuf) []byte {
_, value, _ := buf.ReadBytes(n)
return value
}
type sendMsg struct {
to uint64
data interface{}
}
// ReplicaTransport transport from peers
type ReplicaTransport interface {
Start()
Stop()
Send(uint64, interface{})
}
// NewReplicaTransport returns a replica transport to send and received message
func NewReplicaTransport(addr string, store ResourceStore, factory func() Resource) ReplicaTransport {
biz := &shardCodec{factory: factory}
encoder := goetty.NewIntLengthFieldBasedEncoder(biz)
decoder := goetty.NewIntLengthFieldBasedDecoder(biz)
t := &defaultReplicaTransport{
addr: addr,
decoder: decoder,
encoder: encoder,
server: goetty.NewServer(addr,
goetty.WithServerDecoder(decoder),
goetty.WithServerEncoder(encoder)),
conns: make(map[uint64]goetty.IOSessionPool),
addrs: &sync.Map{},
addrsRevert: &sync.Map{},
store: store,
msgs: task.New(1024),
}
return t
}
type defaultReplicaTransport struct {
sync.RWMutex
addr string
decoder goetty.Decoder
encoder goetty.Encoder
store ResourceStore
server *goetty.Server
addrs *sync.Map
addrsRevert *sync.Map
conns map[uint64]goetty.IOSessionPool
msgs *task.Queue
}
func (t *defaultReplicaTransport) Start() {
go t.readyToSend()
go func() {
err := t.server.Start(t.doConnection)
if err != nil {
log.Fatalf("replica transport start failed with %+v",
err)
}
}()
<-t.server.Started()
}
func (t *defaultReplicaTransport) Stop() {
t.msgs.Dispose()
t.server.Stop()
log.Infof("replica transport stopped")
}
func (t *defaultReplicaTransport) doConnection(session goetty.IOSession) error {
remoteIP := session.RemoteIP()
log.Infof("%s connected", remoteIP)
for {
msg, err := session.Read()
if err != nil {
if err == io.EOF {
log.Infof("closed by %s", remoteIP)
} else {
log.Warningf("read error from conn-%s, errors:\n%+v",
remoteIP,
err)
}
return err
}
ack := t.store.HandleReplicaMsg(msg)
if ack != nil {
session.WriteAndFlush(ack)
}
}
}
func (t *defaultReplicaTransport) Send(to uint64, msg interface{}) {
if to == t.store.Meta().ID() {
t.store.HandleReplicaMsg(msg)
return
}
t.msgs.Put(&sendMsg{
to: to,
data: msg,
})
}
func (t *defaultReplicaTransport) readyToSend() {
items := make([]interface{}, batch, batch)
for {
n, err := t.msgs.Get(batch, items)
if err != nil {
log.Infof("transfer sent worker stopped")
return
}
for i := int64(0); i < n; i++ {
msg := items[i].(*sendMsg)
t.doSend(msg)
}
}
}
func (t *defaultReplicaTransport) doSend(msg *sendMsg) error {
conn, err := t.getConn(msg.to)
if err != nil {
return err
}
err = t.doWrite(msg.data, conn)
t.putConn(msg.to, conn)
return err
}
func (t *defaultReplicaTransport) doWrite(msg interface{}, conn goetty.IOSession) error {
err := conn.WriteAndFlush(msg)
if err != nil {
conn.Close()
return err
}
return nil
}
func (t *defaultReplicaTransport) getConn(containerID uint64) (goetty.IOSession, error) {
conn, err := t.getConnLocked(containerID)
if err != nil {
return nil, err
}
if t.checkConnect(containerID, conn) {
return conn, nil
}
t.putConn(containerID, conn)
return nil, errConnect
}
func (t *defaultReplicaTransport) putConn(id uint64, conn goetty.IOSession) {
t.RLock()
pool := t.conns[id]
t.RUnlock()
if pool != nil {
pool.Put(conn)
} else {
conn.Close()
}
}
func (t *defaultReplicaTransport) getConnLocked(id uint64) (goetty.IOSession, error) {
var err error
t.RLock()
pool := t.conns[id]
t.RUnlock()
if pool == nil {
t.Lock()
pool = t.conns[id]
if pool == nil {
pool, err = goetty.NewIOSessionPool(1, 1, func() (goetty.IOSession, error) {
return t.createConn(id)
})
if err != nil {
return nil, err
}
t.conns[id] = pool
}
t.Unlock()
}
return pool.Get()
}
func (t *defaultReplicaTransport) checkConnect(id uint64, conn goetty.IOSession) bool {
if nil == conn {
return false
}
if conn.IsConnected() {
return true
}
ok, err := conn.Connect()
if err != nil {
log.Errorf("connect to container %d failure, errors:\n %+v",
id,
err)
return false
}
// read loop
go func() {
for {
msg, err := conn.Read()
if err != nil {
return
}
ack := t.store.HandleReplicaMsg(msg)
if ack != nil {
log.Fatalf("%+v %T unexpect ack %T", msg, data, ack)
}
}
}()
log.Infof("connected to container %d", id)
return ok
}
func (t *defaultReplicaTransport) createConn(id uint64) (goetty.IOSession, error) {
addr, err := t.getContainerAddr(id)
if err != nil {
return nil, err
}
return goetty.NewConnector(addr,
goetty.WithClientDecoder(t.decoder),
goetty.WithClientEncoder(t.encoder)), nil
}
func (t *defaultReplicaTransport) getContainerAddr(containerID uint64) (string, error) {
var err error
addr, ok := t.addrs.Load(containerID)
if !ok {
addr, err = t.store.GetContainerAddr(containerID)
if err != nil {
return "", err
}
t.addrs.Store(containerID, addr)
t.addrsRevert.Store(addr, containerID)
}
return addr.(string), nil
}