/
bulkWriter.go
229 lines (207 loc) · 5.96 KB
/
bulkWriter.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
package mongodb
import (
"context"
"sync"
"time"
"github.com/hashicorp/go-multierror"
"github.com/plgd-dev/hub/v2/certificate-authority/store"
"github.com/plgd-dev/hub/v2/pkg/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type bulkWriter struct {
col *mongo.Collection
documentLimit uint16 // https://www.mongodb.com/docs/manual/reference/limits/#mongodb-limit-Write-Command-Batch-Limit-Size - must be <= 100000
throttleTime time.Duration
flushTimeout time.Duration
logger log.Logger
done chan struct{}
trigger chan bool
mutex sync.Mutex
models map[string]*store.SigningRecord
wg sync.WaitGroup
}
func newBulkWriter(col *mongo.Collection, documentLimit uint16, throttleTime time.Duration, flushTimeout time.Duration, logger log.Logger) *bulkWriter {
r := &bulkWriter{
col: col,
documentLimit: documentLimit,
throttleTime: throttleTime,
flushTimeout: flushTimeout,
done: make(chan struct{}),
trigger: make(chan bool, 1),
logger: logger,
}
r.wg.Add(1)
go func() {
defer r.wg.Done()
r.run()
}()
return r
}
func toSigningRecordFilter(signingRecord *store.SigningRecord) bson.M {
res := bson.M{"_id": signingRecord.GetId()}
return res
}
func getSigningRecordCreationDate(defaultTime time.Time, signingRecord *store.SigningRecord) int64 {
ret := defaultTime.UTC().UnixNano()
if signingRecord.GetCredential().GetDate() > 0 && signingRecord.GetCredential().GetDate() < ret {
ret = signingRecord.GetCredential().GetDate()
}
return ret
}
func setValueByDate(key, datePath string, dateOperator string, date int64, value interface{}) bson.M {
return bson.M{
"$set": bson.M{
key: bson.M{
"$ifNull": bson.A{
bson.M{
"$cond": bson.M{
"if": bson.M{
dateOperator: bson.A{"$" + datePath, date},
},
"then": value,
"else": "$" + key,
},
}, value,
},
},
},
}
}
func updateSigningRecord(signingRecord *store.SigningRecord) []bson.M {
creationDate := signingRecord.GetCreationDate()
if creationDate == 0 {
creationDate = getSigningRecordCreationDate(time.Now(), signingRecord)
}
ret := []bson.M{
{"$set": bson.M{
"_id": signingRecord.GetId(),
store.CommonNameKey: signingRecord.GetCommonName(),
store.OwnerKey: signingRecord.GetOwner(),
store.PublicKeyKey: signingRecord.GetPublicKey(),
}},
}
ret = append(ret, setValueByDate(store.CreationDateKey, store.CreationDateKey, "$gt", creationDate, creationDate))
if signingRecord.GetCredential() != nil {
ret = append(ret, setValueByDate(store.CredentialKey, store.CredentialKey+"."+store.DateKey, "$lt", signingRecord.GetCredential().GetDate(), signingRecord.GetCredential()))
}
return ret
}
func convertSigningRecordToWriteModel(signingRecord *store.SigningRecord) mongo.WriteModel {
return mongo.NewUpdateOneModel().SetFilter(toSigningRecordFilter(signingRecord)).SetUpdate(updateSigningRecord(signingRecord)).SetUpsert(true)
}
func mergeLatestUpdateSigningRecord(toUpdate *store.SigningRecord, latest *store.SigningRecord) *store.SigningRecord {
if toUpdate == nil {
return latest
}
if latest.GetCredential().GetDate() > toUpdate.GetCredential().GetDate() {
toUpdate.Credential = latest.GetCredential()
}
if latest.GetCreationDate() < toUpdate.GetCreationDate() {
toUpdate.CreationDate = latest.GetCreationDate()
if toUpdate.GetCommonName() == "" {
toUpdate.CommonName = latest.GetCommonName()
}
if toUpdate.GetOwner() == "" {
toUpdate.Owner = latest.GetOwner()
}
if toUpdate.GetPublicKey() == "" {
toUpdate.PublicKey = latest.GetPublicKey()
}
}
return toUpdate
}
func (b *bulkWriter) popSigningRecords() map[string]*store.SigningRecord {
b.mutex.Lock()
defer b.mutex.Unlock()
models := b.models
b.models = nil
return models
}
func (b *bulkWriter) Push(signingRecords ...*store.SigningRecord) {
b.mutex.Lock()
defer b.mutex.Unlock()
if b.models == nil {
b.models = make(map[string]*store.SigningRecord)
}
for _, signingRecord := range signingRecords {
b.models[signingRecord.GetId()] = mergeLatestUpdateSigningRecord(b.models[signingRecord.GetId()], signingRecord)
}
select {
case b.trigger <- true:
default:
}
}
func (b *bulkWriter) numSigningRecords() int {
b.mutex.Lock()
defer b.mutex.Unlock()
return len(b.models)
}
func (b *bulkWriter) run() {
ticker := time.NewTicker(b.throttleTime)
tickerRunning := true
defer ticker.Stop()
for {
select {
case <-ticker.C:
if b.tryBulkWrite() == 0 && tickerRunning {
ticker.Stop()
tickerRunning = false
}
case <-b.trigger:
if !tickerRunning {
ticker.Reset(b.throttleTime)
tickerRunning = true
}
if b.numSigningRecords() > int(b.documentLimit) {
b.tryBulkWrite()
}
case <-b.done:
return
}
}
}
func (b *bulkWriter) bulkWrite() (int, error) {
SigningRecords := b.popSigningRecords()
if len(SigningRecords) == 0 {
return 0, nil
}
ctx := context.Background()
if b.flushTimeout != 0 {
ctx1, cancel := context.WithTimeout(context.Background(), b.flushTimeout)
defer cancel()
ctx = ctx1
}
m := make([]mongo.WriteModel, 0, int(b.documentLimit)+1)
var errors *multierror.Error
for _, SigningRecord := range SigningRecords {
m = append(m, convertSigningRecordToWriteModel(SigningRecord))
if b.documentLimit == 0 || len(m)%int(b.documentLimit) == 0 {
_, err := b.col.BulkWrite(ctx, m, options.BulkWrite().SetOrdered(false))
if err != nil {
errors = multierror.Append(errors, err)
}
m = m[:0]
}
}
if len(m) > 0 {
_, err := b.col.BulkWrite(ctx, m, options.BulkWrite().SetOrdered(false))
if err != nil {
errors = multierror.Append(errors, err)
}
}
return len(SigningRecords), errors.ErrorOrNil()
}
func (b *bulkWriter) tryBulkWrite() int {
n, err := b.bulkWrite()
if err != nil {
b.logger.Errorf("failed to bulk update Signing records: %w", err)
}
return n
}
func (b *bulkWriter) Close() {
close(b.done)
b.wg.Wait()
b.tryBulkWrite()
}