forked from alibaba/MongoShake
/
direct_writer.go
47 lines (38 loc) · 1.14 KB
/
direct_writer.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
package tunnel
import (
"mongoshake/executor"
"github.com/gugemichael/nimo4go"
LOG "github.com/vinllen/log4go"
"mongoshake/common"
)
type DirectWriter struct {
RemoteAddrs []string
ReplayerId uint32 // equal to worker-id
batchExecutor *executor.BatchGroupExecutor
}
func (writer *DirectWriter) Prepare() bool {
nimo.AssertTrue(len(writer.RemoteAddrs) > 0, "RemoteAddrs must > 0")
first := writer.RemoteAddrs[0]
if _, err := utils.NewMongoConn(first, utils.ConnectModeSecondaryPreferred, true); err != nil {
LOG.Critical("target mongo server[%s] connect failed: %s", first, err.Error())
return false
}
urlChoose := writer.ReplayerId % uint32(len(writer.RemoteAddrs))
writer.batchExecutor = &executor.BatchGroupExecutor{
ReplayerId: writer.ReplayerId,
MongoUrl: writer.RemoteAddrs[urlChoose],
}
// writer.batchExecutor.RestAPI()
writer.batchExecutor.Start()
return true
}
func (writer *DirectWriter) Send(message *WMessage) int64 {
writer.batchExecutor.Sync(message.ParsedLogs, nil)
return 0
}
func (writer *DirectWriter) AckRequired() bool {
return false
}
func (writer *DirectWriter) ParsedLogsRequired() bool {
return true
}