forked from Mintegral-official/juno
/
mongo_index.go
264 lines (241 loc) · 6.37 KB
/
mongo_index.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
package builder
import (
"context"
"encoding/json"
"github.com/MintegralTech/juno/helpers"
"github.com/MintegralTech/juno/index"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"sync"
"time"
)
type MongoIndexBuilder struct {
ops *MongoIndexManagerOps
innerIndex index.Index
totalNum int64
errorNum int64
client *mongo.Client
collection *mongo.Collection
findOpt *options.FindOptions
addCounter int64
deleteCounter int64
mergeTime time.Duration
lastBaseUpdated time.Time
lastIncUpdated time.Time
lock sync.RWMutex
}
func NewMongoIndexBuilder(ops *MongoIndexManagerOps) (*MongoIndexBuilder, error) {
if ops == nil {
return nil, helpers.MongoCfgError
}
mib := &MongoIndexBuilder{
ops: ops,
innerIndex: nil,
}
ctx, _ := context.WithTimeout(context.TODO(), time.Duration(ops.ConnectTimeout)*time.Microsecond)
opt := options.Client().ApplyURI(ops.URI)
opt.SetReadPreference(readpref.SecondaryPreferred())
direct := true
opt.Direct = &direct
client, err := mongo.Connect(ctx, opt)
if err != nil {
if mib.ops.Logger != nil {
mib.ops.Logger.Warnf("mongo connect failed: [%s]", err.Error())
}
return nil, helpers.ConnectError
}
mib.client = client
mib.findOpt = options.MergeFindOptions(ops.FindOpt)
d := time.Duration(ops.ReadTimeout) * time.Microsecond
mib.findOpt.MaxTime = &d
if err = client.Ping(ctx, readpref.Primary()); err != nil {
if mib.ops.Logger != nil {
mib.ops.Logger.Warnf("mongo ping failed: [%s]", err.Error())
}
return nil, helpers.PingError
}
mib.collection = client.Database(ops.DB).Collection(ops.Collection)
if mib.collection == nil {
if mib.ops.Logger != nil {
mib.ops.Logger.Warnf("mongo database[%s] collection[%s] not found", ops.DB, ops.Collection)
}
return nil, helpers.CollectionNotFound
}
return mib, nil
}
func (mib *MongoIndexBuilder) GetIndex() index.Index {
mib.lock.RLock()
defer mib.lock.RUnlock()
return mib.innerIndex
}
func (mib *MongoIndexBuilder) update(ctx context.Context, name string) error {
now := time.Now()
err := mib.base(name)
d := time.Now().Sub(now)
if err != nil {
mib.WarnStatus("base load failed: "+err.Error(), d)
return err
} else {
mib.InfoStatus("base load success", d)
}
go func() {
var (
base = time.After(time.Duration(mib.ops.BaseInterval) * time.Second)
inc = time.After(time.Duration(mib.ops.IncInterval) * time.Second)
)
for {
select {
case <-ctx.Done():
mib.InfoStatus("finish: ", 0)
return
case <-base:
now := time.Now()
err := mib.base(name)
d := time.Now().Sub(now)
base = time.After(time.Duration(mib.ops.BaseInterval) * time.Second)
if err != nil {
mib.WarnStatus("base load failed: "+err.Error(), d)
} else {
mib.InfoStatus("base load success", d)
}
case <-inc:
now := time.Now()
err := mib.inc(ctx)
d := time.Now().Sub(now)
inc = time.After(time.Duration(mib.ops.IncInterval)*time.Second + time.Nanosecond)
if err != nil {
mib.WarnStatus("inc failed: "+err.Error(), d)
} else {
mib.InfoStatus("inc success", d)
}
}
}
}()
return nil
}
func (mib *MongoIndexBuilder) base(name string) (err error) {
mib.totalNum, mib.errorNum = 0, 0
mib.addCounter = 0
if mib.ops.OnBeforeBase != nil {
mib.ops.BaseQuery = mib.ops.OnBeforeBase(mib.ops.UserData)
}
cur, err := mib.collection.Find(nil, mib.ops.BaseQuery, mib.ops.FindOpt)
if err != nil {
return err
}
defer func() {
_ = cur.Close(nil)
}()
var baseIndex = index.NewIndex(name)
for cur.Next(nil) {
if cur.Err() != nil {
mib.errorNum++
continue
}
r := mib.ops.BaseParser.Parse(cur.Current, mib.ops.UserData)
if r == nil {
mib.errorNum++
continue
}
mib.totalNum++
if e := baseIndex.Add(r.Value); e == nil {
mib.addCounter++
} else {
mib.ops.Logger.Warnf("load base error[%s]", e.Error())
mib.errorNum++
}
//mib.addCounter++
}
mib.lock.Lock()
mib.innerIndex = baseIndex
mib.lock.Unlock()
if mib.ops.OnFinishBase != nil {
mib.ops.OnFinishBase(mib)
}
mib.lastBaseUpdated = time.Now()
return err
}
func (mib *MongoIndexBuilder) inc(ctx context.Context) (err error) {
if mib.ops.OnBeforeInc != nil {
mib.ops.IncQuery = mib.ops.OnBeforeInc(mib.ops.UserData)
}
c, cancel := context.WithTimeout(ctx, time.Duration(mib.ops.ReadTimeout)*time.Microsecond)
defer cancel()
cur, err := mib.collection.Find(c, mib.ops.IncQuery, mib.ops.FindOpt)
if err != nil {
return err
}
defer func() {
_ = cur.Close(c)
}()
mib.deleteCounter = 0
mib.addCounter = 0
tmpIndex := index.NewIndex(mib.innerIndex.GetName())
for cur.Next(nil) {
mib.totalNum++
if cur.Err() != nil {
mib.errorNum++
continue
}
r := mib.ops.IncParser.Parse(cur.Current, mib.ops.UserData)
if r == nil {
mib.errorNum++
continue
}
if r.DataMod == DataAddOrUpdate {
if e := tmpIndex.Add(r.Value); e == nil {
mib.addCounter++
} else {
mib.ops.Logger.Warnf("load inc error[%s]", e.Error())
mib.errorNum++
}
} else {
mib.innerIndex.Del(r.Value)
mib.deleteCounter++
}
}
t := tmpIndex.(*index.IndexerV2)
now := time.Now()
if err := t.MergeIndex(mib.innerIndex.(*index.IndexerV2)); err != nil {
return err
}
mib.lock.Lock()
mib.innerIndex = t
mib.lock.Unlock()
mib.mergeTime = time.Now().Sub(now)
if mib.ops.OnFinishInc != nil {
mib.ops.OnFinishInc(mib)
}
mib.lastIncUpdated = time.Now()
return nil
}
func (mib *MongoIndexBuilder) Build(ctx context.Context, name string) error {
return mib.update(ctx, name)
}
func (mib *MongoIndexBuilder) InfoStatus(s string, d time.Duration) {
if mib.ops.Logger != nil {
mib.ops.Logger.Info(s, mib.Info(s, d))
}
}
func (mib *MongoIndexBuilder) WarnStatus(s string, d time.Duration) {
if mib.ops.Logger != nil {
mib.ops.Logger.Info(s, mib.Info(s, d))
}
}
func (mib *MongoIndexBuilder) Info(s string, d time.Duration) string {
data, _ := json.Marshal(mib.GetBuilderInfo())
return string(data)
}
func (mib *MongoIndexBuilder) GetBuilderInfo() *BuildInfo {
return &BuildInfo{
TotalNumber: mib.totalNum,
ErrorNumber: mib.errorNum,
AddNum: mib.addCounter,
DeleteNum: mib.deleteCounter,
MergeTime: mib.mergeTime,
LastBaseUpdated: mib.lastBaseUpdated,
LastIncUpdated: mib.lastIncUpdated,
IndexInfo: mib.GetIndex().GetIndexInfo(),
}
}