/
tunnel.go
225 lines (201 loc) · 5.61 KB
/
tunnel.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
package tunnel
import (
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
"mongoshake/oplog"
"github.com/gugemichael/nimo4go"
LOG "github.com/vinllen/log4go"
"mongoshake/common"
)
const InitialStageChecking = false
const (
MsgNormal = 0x00000000
MsgRetransmission = 0x00000001
MsgProbe = 0x00000010
MsgResident = 0x00000100
MsgPersistent = 0x00001000
MsgStorageBackend = 0x00010000
)
const (
ReplyOK = 0
ReplyError int64 = -1
ReplyNetworkOpFail int64 = -2
ReplyNetworkTimeout int64 = -3
ReplyRetransmission int64 = -4
ReplyServerFault int64 = -5
ReplyChecksumInvalid int64 = -6
ReplyCompressorNotSupported int64 = -7
ReplyDecompressInvalid = -8
)
// WMessage wrapped TMessage
type WMessage struct {
*TMessage // whole raw log
ParsedLogs []*oplog.PartialLog // parsed log
}
type TMessage struct {
Checksum uint32
Tag uint32
Shard uint32
Compress uint32
RawLogs [][]byte
}
func (msg *TMessage) Crc32() uint32 {
var value uint32
for _, log := range msg.RawLogs {
value ^= crc32.ChecksumIEEE(log)
}
return value
}
func (msg *TMessage) ToBytes(order binary.ByteOrder) []byte {
buffer := bytes.Buffer{}
binary.Write(&buffer, order, msg.Checksum)
binary.Write(&buffer, order, msg.Tag)
binary.Write(&buffer, order, msg.Shard)
binary.Write(&buffer, order, msg.Compress)
binary.Write(&buffer, order, uint32(len(msg.RawLogs)))
for _, log := range msg.RawLogs {
binary.Write(&buffer, order, uint32(len(log)))
buffer.Write(log)
}
return buffer.Bytes()
}
func (msg *TMessage) FromBytes(buf []byte, order binary.ByteOrder) {
buffer := bytes.NewBuffer(buf)
binary.Read(buffer, order, &msg.Checksum)
binary.Read(buffer, order, &msg.Tag)
binary.Read(buffer, order, &msg.Shard)
binary.Read(buffer, order, &msg.Compress)
var n uint32
binary.Read(buffer, order, &n)
nimo.AssertTrue((buffer.Len() != 0 && msg.Tag&MsgProbe == 0) ||
(buffer.Len() == 0 && msg.Tag&MsgProbe != 0),
"message decode left bytes are empty")
var start = uint32(len(buf) - buffer.Len())
for n != 0 {
tmp := bytes.NewBuffer(buf[start:])
var length uint32
binary.Read(tmp, order, &length)
start += 4
// total "n" number should be exactly correct. crash with
// out of range with slice if we got dirty records
nimo.AssertTrue(start+length <= uint32(len(buf)), "oplogs in msg offset is invalid")
bytes := buf[start : start+length]
start += length
msg.RawLogs = append(msg.RawLogs, bytes)
n--
}
}
func (msg *TMessage) String() string {
return fmt.Sprintf("[cksum:%d, tag:%d, shard:%d, compress:%d, logs_len:%d]",
msg.Checksum, msg.Tag, msg.Shard, msg.Compress, len(msg.RawLogs))
}
func (msg *TMessage) ApproximateSize() uint64 {
var size uint64 = 0
for _, log := range msg.RawLogs {
size += uint64(len(log))
}
return size
}
type Writer interface {
/**
* Indicate weather this tunnel cares about ACK feedback value.
* Like RPC_TUNNEL (ack required is true), it's asynchronous and
* needs peer receiver has completely consumed the log entries
* and we can drop the reserved log entries only if the log entry
* ACK is confirmed
*/
AckRequired() bool
/**
* prepare stage of the tunnel such as create the network connection or initialize
* something etc before the Send() invocation.
* return true on successful or false on failed
*/
Prepare() bool
/**
* write the real tunnel message to tunnel.
*
* return the right ACK offset value with positive number. if AckRequired is set
* this ACk offset is used to purge buffered oplogs. Otherwise upper layer use
* the max oplog ts as ACK offset and discard the returned value (ACK offset).
* error on returning a negative number
*/
Send(message *WMessage) int64
/**
* whether need parsed log or raw log
*/
ParsedLogsRequired() bool
/*
* tunnel name
*/
Name() string
}
type WriterFactory struct {
Name string
}
// create specific Tunnel with tunnel name and pass connection
// or usefully meta
func (factory *WriterFactory) Create(address []string, workerId uint32) Writer {
switch factory.Name {
case utils.VarTunnelKafka:
return &KafkaWriter{RemoteAddr: address[0]}
case utils.VarTunnelTcp:
return &TCPWriter{RemoteAddr: address[0]}
case utils.VarTunnelRpc:
return &RPCWriter{RemoteAddr: address[0]}
case utils.VarTunnelMock:
return &MockWriter{}
case utils.VarTunnelFile:
return &FileWriter{Local: address[0]}
case utils.VarTunnelDirect:
return &DirectWriter{RemoteAddrs: address, ReplayerId: workerId}
default:
LOG.Critical("Specific tunnel not found [%s]", factory.Name)
return nil
}
}
// create specific Tunnel with tunnel name and pass connection
// or usefully meta
func (factory *ReaderFactory) Create(address string) Reader {
switch factory.Name {
case utils.VarTunnelKafka:
return &KafkaReader{address: address}
case utils.VarTunnelTcp:
return &TCPReader{listenAddress: address}
case utils.VarTunnelRpc:
return &RPCReader{address: address}
case utils.VarTunnelMock:
return &MockReader{}
case utils.VarTunnelFile:
return &FileReader{File: address}
case utils.VarTunnelDirect:
LOG.Critical("direct mode not supported in reader")
return nil
default:
LOG.Critical("Specific tunnel not found [%s]", factory.Name)
return nil
}
}
type Reader interface {
/**
* Bridge of tunnel reader and aggregater(replayer)
*
*/
Link(aggregate []Replayer) error
}
type Replayer interface {
/**
* Replay oplog entry with batched Oplog
*
*/
Sync(message *TMessage, completion func()) int64
/**
* Ack offset value
*
*/
GetAcked() int64
}
type ReaderFactory struct {
Name string
}