-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
275 lines (245 loc) · 6.98 KB
/
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
package transport
import (
"context"
"errors"
"fmt"
"log"
"os"
"runtime"
"strings"
"time"
rpcx "github.com/smallnest/rpcx/client"
"github.com/soyoslab/soy_log_collector/pkg/rpc"
c "github.com/soyoslab/soy_log_generator/pkg/compressor"
s "github.com/soyoslab/soy_log_generator/pkg/scheduler"
)
// SubmitFunc is a type for submission the packet to rpcx
type SubmitFunc func(*rpc.LogMessage, rpcx.XClient) error
// BufferingMetadata contains the cold data's buffering information
type BufferingMetadata struct {
packet rpc.LogMessage
start time.Time
threshold uint64
timeout time.Duration
}
// Port contains the rpcx client and the buffering information
type Port struct {
xclient rpcx.XClient
meta BufferingMetadata
}
// Close closes the rpcx client
func (p *Port) Close() {
if p.xclient != nil {
p.xclient.Close()
}
}
// Transport contains the rpcx and communcation information
type Transport struct {
scheduler *s.Scheduler
hot Port
cold Port
addr string
compressor c.Compressor
err error
submit SubmitFunc
fileMap map[string]uint8
packetMap []string
namespace string
}
// getAddr returns the address of the rpcx server
func getAddr(ip string, port string) string {
return fmt.Sprintf("%s:%s", ip, port)
}
// getXClient returns the rpcx client instance
// NewPeer2PeerDiscovery function always returns nil to err
// For this reason, second return parameter doesn't have any meaning
func getXClient(addr string, funcName string) (rpcx.XClient, error) {
discovery, _ := rpcx.NewPeer2PeerDiscovery("tcp@"+addr, "")
xclient := rpcx.NewXClient(funcName, rpcx.Failtry, rpcx.RandomSelect, discovery, rpcx.DefaultOption)
return xclient, nil
}
// exceptionHandler does mapping the error code to the Transport structure's error member
func exceptionHandler(transport *Transport, err error) error {
if transport != nil && err != nil {
transport.err = err
transport.Close()
}
return err
}
// InitTransport returns the instance of the Transport structure
func InitTransport(configFileName string, customFilterFunc s.CustomFilterFunc) (*Transport, error) {
var (
err error
scheduler *s.Scheduler
config s.Config
files []s.File
packet *rpc.LogMessage
reply *rpc.Reply
xclient rpcx.XClient
hostname string
)
t := new(Transport)
submitOps := s.SubmitOperations{}
submitOps.Hot = t.hotSubmitFunc
submitOps.Cold = t.coldSubmitFunc
scheduler, err = s.InitScheduler(configFileName, submitOps, customFilterFunc)
if err != nil {
goto out
}
t.err = nil
t.scheduler = scheduler
hostname, _ = os.Hostname()
t.namespace = fmt.Sprintf("%s:%s", t.scheduler.GetConfig().Namespace, hostname)
t.cold.meta.threshold = scheduler.GetConfig().ColdSendThreshold
t.cold.meta.timeout = time.Duration(scheduler.GetConfig().ColdTimeout) * time.Millisecond
t.cold.meta.start = time.Now()
t.compressor = &c.GzipComp{}
t.submit = Submit
t.fileMap = make(map[string]uint8)
files = t.scheduler.GetConfig().Files
t.packetMap = make([]string, len(files))
for idx, file := range files {
t.fileMap[file.Filename] = uint8(idx)
t.packetMap[idx] = file.Filename
}
config = t.scheduler.GetConfig()
t.addr = getAddr(config.TargetIP, config.TargetPort)
t.hot.xclient, _ = getXClient(t.addr, "HotPort")
t.cold.xclient, _ = getXClient(t.addr, "ColdPort")
xclient, _ = getXClient(t.addr, "Init")
packet = &rpc.LogMessage{}
packet.Namespace = t.namespace
packet.Files.MapTable = t.packetMap
reply = &rpc.Reply{}
err = xclient.Call(context.Background(), "Push", packet, reply)
if err != nil {
goto out
}
out:
return t, exceptionHandler(t, err)
}
// Run executes the scheduler
func (t *Transport) Run() error {
var err error
if t.scheduler == nil {
err = errors.New("scheduler must be allocated")
goto out
}
err = t.scheduler.Run()
out:
if err != nil {
t.err = err
}
return t.err
}
func getInfo(message s.Message) rpc.LogInfo {
info := rpc.LogInfo{}
info.Length = message.Info.Length
info.Timestamp = message.Info.Timestamp
return info
}
// getPacket converts the custom Message structures to the rpc.LogMessage format
func getPacket(messages []s.Message, fileMap map[string]uint8, packetMap []string) (rpc.LogMessage, error) {
packet := rpc.LogMessage{}
packet.Files.MapTable = packetMap
size := uint64(0)
for _, message := range messages {
info := getInfo(message)
packet.Info = append(packet.Info, info)
packet.Buffer = append(packet.Buffer, message.Data...)
if len(message.Info.Filename) == 0 {
return rpc.LogMessage{}, fmt.Errorf("filename must be specified")
}
idx := fileMap[message.Info.Filename]
packet.Files.Indexes = append(packet.Files.Indexes, idx)
size += message.Info.Length
}
if size != uint64(len(packet.Buffer)) {
return rpc.LogMessage{}, fmt.Errorf("buffer and info size mismatch (buffer: %d, info: %d)", len(packet.Buffer), size)
}
return packet, nil
}
// Submit submits the packet to server by using rpcx
func Submit(packet *rpc.LogMessage, xclient rpcx.XClient) error {
reply := &rpc.Reply{}
err := xclient.Call(context.Background(), "Push", packet, reply)
return err
}
// hotSubmitFunc submits the hot messages
func (t *Transport) hotSubmitFunc(messages []s.Message) error {
var (
packet rpc.LogMessage
err error
)
packet, err = getPacket(messages, t.fileMap, t.packetMap)
if err != nil {
goto exception
}
if len(packet.Info) == 0 {
return nil
}
packet.Namespace = t.namespace
packet.Files.MapTable = nil
for {
err = t.submit(&packet, t.hot.xclient)
if err == nil {
break
} else if !strings.Contains(err.Error(), "is full") {
goto exception
}
log.Println("hot port error detected:", err)
runtime.Gosched()
}
return nil
exception:
return exceptionHandler(t, err)
}
// coldSubmitFunc submits the cold messages
func (t *Transport) coldSubmitFunc(messages []s.Message) error {
var (
meta *BufferingMetadata
err error
packet rpc.LogMessage
)
packet, err = getPacket(messages, t.fileMap, nil)
if err != nil {
goto exception
}
if len(packet.Info) == 0 {
return nil
}
meta = &t.cold.meta
meta.packet.Info = append(meta.packet.Info, packet.Info...)
meta.packet.Buffer = append(meta.packet.Buffer, packet.Buffer...)
meta.packet.Files.Indexes = append(meta.packet.Files.Indexes, packet.Files.Indexes...)
if uint64(len(meta.packet.Buffer)) >= meta.threshold || time.Since(meta.start) >= meta.timeout {
meta.packet.Buffer, err = t.compressor.Compress(meta.packet.Buffer)
if err != nil {
goto exception
}
meta.packet.Namespace = t.namespace
meta.packet.Files.MapTable = nil
for {
err = t.submit(&meta.packet, t.cold.xclient)
if err == nil {
break
} else if !strings.Contains(err.Error(), "is full") {
goto exception
}
log.Println("cold port error detected:", err)
runtime.Gosched()
}
meta.packet = rpc.LogMessage{}
}
return nil
exception:
return exceptionHandler(t, err)
}
// Close closes the transport data structure
func (t *Transport) Close() {
if t.scheduler != nil {
t.scheduler.Close()
}
t.cold.Close()
t.hot.Close()
}