forked from Mintegral-official/mtggokit
/
bifrost_streamer.go
172 lines (157 loc) · 4.41 KB
/
bifrost_streamer.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
package streamer
import (
"context"
"errors"
"fmt"
"github.com/Mintegral-official/mtggokit/bifrost/container"
"github.com/smallnest/rpcx/client"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/share"
"time"
)
type BifrostStreamer struct {
cfg *BiFrostStreamerCfg
container container.Container
client client.XClient
incRecords []*IncRecord
curIdx int
progress int64
totalNum int64
errorNum int64
startTime int64
endTime int64
hasInit bool
}
func NewBiFrostStreamer(cfg *BiFrostStreamerCfg) *BifrostStreamer {
d := client.NewPeer2PeerDiscovery("tcp@"+cfg.URI, "")
option := client.DefaultOption
option.SerializeType = protocol.SerializeType(5)
share.Codecs[protocol.SerializeType(5)] = &GobCodec{}
c := client.NewXClient("BifrostService", client.Failtry, client.RandomSelect, d, option)
return &BifrostStreamer{
cfg: cfg,
client: c,
}
}
func (bs *BifrostStreamer) SetContainer(container container.Container) {
bs.container = container
}
func (bs *BifrostStreamer) GetContainer() container.Container {
return bs.container
}
func (bs *BifrostStreamer) GetSchedInfo() *SchedInfo {
return &SchedInfo{
TimeInterval: bs.cfg.Interval,
}
}
func (bs *BifrostStreamer) HasNext() bool {
return bs.curIdx < len(bs.incRecords)
}
func (bs *BifrostStreamer) Next() (container.DataMode, container.MapKey, interface{}, error) {
bs.totalNum++
if bs.curIdx < 0 || bs.curIdx >= len(bs.incRecords) {
bs.errorNum++
return container.DataModeAdd, nil, nil, errors.New(fmt.Sprintf("invalid index[%d],cap[%d]", bs.curIdx, len(bs.incRecords)))
}
r := bs.incRecords[bs.curIdx]
bs.curIdx++
return r.DataMode, r.MapKey, r.Value, nil
}
func (bs *BifrostStreamer) UpdateData(ctx context.Context) error {
if bs.cfg.IsSync {
bs.startTime = time.Now().UnixNano()
err := bs.loadBase(ctx)
bs.endTime = time.Now().UnixNano()
if err != nil {
bs.WarnStatus("Sync LoadBase error: " + err.Error())
return err
}
bs.InfoStatus("Sync LoadBase succ")
bs.hasInit = true
}
go func() {
if !bs.hasInit {
bs.startTime = time.Now().UnixNano()
err := bs.loadBase(ctx)
bs.endTime = time.Now().UnixNano()
if err != nil {
bs.WarnStatus("Async LoadBase error: " + err.Error())
}
bs.InfoStatus("Async LoadBase succ")
bs.hasInit = true
}
for {
inc := time.After(time.Duration(bs.cfg.Interval) * time.Second)
select {
case <-ctx.Done():
return
case <-inc:
bs.startTime = time.Now().UnixNano()
err := bs.loadInc(ctx)
bs.endTime = time.Now().UnixNano()
if err != nil {
bs.WarnStatus("LoadBase error: " + err.Error())
} else {
bs.InfoStatus("LoadBase succ")
}
}
}
}()
return nil
}
func (bs *BifrostStreamer) loadBase(ctx context.Context) error {
bs.totalNum = 0
bs.errorNum = 0
res := &BaseRes{}
err := bs.client.Call(ctx, "GetBase", &BaseReq{
Name: bs.cfg.Name,
Progress: bs.progress,
}, res)
if err != nil {
return err
}
if res.Status != Ok {
return errors.New("GetBase error, status[" + res.Status.toString() + "]")
}
if res.BaseInfo == nil {
return errors.New("GetBase error, baseInfo is nil")
}
bs.progress = res.BaseInfo.Progress
records := make([]*IncRecord, 0, len(res.BaseInfo.Data))
fmt.Println("xxxxxxxxxxxxxxxxxxxxxx: ", bs.progress, len(res.BaseInfo.Data))
for k, v := range res.BaseInfo.Data {
records = append(records, &IncRecord{
DataMode: container.DataModeAdd,
MapKey: k,
Value: v,
Progress: bs.progress,
})
}
bs.incRecords = records
return bs.container.LoadBase(bs)
}
func (bs *BifrostStreamer) loadInc(ctx context.Context) error {
res := &IncRes{}
err := bs.client.Call(ctx, "GetInc", &IncReq{
Name: bs.cfg.Name,
Batch: 1000,
}, res)
if err != nil {
return err
}
if res.Status != Ok {
return errors.New("GetInc error, status[" + res.Status.toString() + "]")
}
bs.incRecords = res.IncRecords
return bs.container.LoadInc(bs)
}
func (fs *BifrostStreamer) InfoStatus(s string) {
if fs.cfg.Logger != nil {
fs.cfg.Logger.Infof("streamer[%s] %s, totalNum[%d], errorNum[%d], userData[%s], timeUsed[%d]", fs.cfg.Name, s, fs.totalNum, fs.errorNum, fs.cfg.UserData, (fs.endTime-fs.startTime)/10e6)
}
}
func (fs *BifrostStreamer) WarnStatus(s string) {
if fs.cfg.Logger != nil {
fs.cfg.Logger.Warnf("streamer[%s] %s, totalNum[%d], errorNum[%d], userData[%s], timeUsed[%d]", fs.cfg.Name, s, fs.totalNum, fs.errorNum, fs.cfg.UserData, (fs.endTime-fs.startTime)/10e6)
}
}