/
bulk.go
148 lines (128 loc) 路 4.32 KB
/
bulk.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
/*
*
* Copyright 2021 waterdrop authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package mongo
import (
"context"
"time"
"github.com/UnderTreeTech/waterdrop/pkg/breaker"
"github.com/UnderTreeTech/waterdrop/pkg/stats/metric"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/opentracing/opentracing-go"
"github.com/qiniu/qmgo"
)
// Bulk is context for batching operations to be sent to database in a single
// bulk write.
//
// Bulk is not safe for concurrent use.
//
// Notes:
//
// Individual operations inside a bulk do not trigger middlewares or hooks
// at present.
//
// Different from original mgo, the qmgo implementation of Bulk does not emulate
// bulk operations individually on old versions of MongoDB servers that do not
// natively support bulk operations.
//
// Only operations supported by the official driver are exposed, that is why
// InsertMany is missing from the methods.
type Bulk struct {
bulk *qmgo.Bulk
config *Config
span opentracing.Span
ctx context.Context
brk *breaker.BreakerGroup
}
// SetOrdered marks the bulk as ordered or unordered.
//
// If ordered, writes does not continue after one individual write fails.
// Default is ordered.
func (b *Bulk) SetOrdered(ordered bool) *Bulk {
b.bulk = b.bulk.SetOrdered(ordered)
return b
}
// InsertOne queues an InsertOne operation for bulk execution.
func (b *Bulk) InsertOne(doc interface{}) *Bulk {
b.bulk = b.bulk.InsertOne(doc)
return b
}
// Remove queues a Remove operation for bulk execution.
func (b *Bulk) Remove(filter interface{}) *Bulk {
b.bulk = b.bulk.Remove(filter)
return b
}
// RemoveId queues a RemoveId operation for bulk execution.
func (b *Bulk) RemoveId(id interface{}) *Bulk {
b.bulk = b.bulk.RemoveId(id)
return b
}
// RemoveAll queues a RemoveAll operation for bulk execution.
func (b *Bulk) RemoveAll(filter interface{}) *Bulk {
b.bulk = b.bulk.RemoveAll(filter)
return b
}
// Upsert queues an Upsert operation for bulk execution.
// The replacement should be document without operator
func (b *Bulk) Upsert(filter interface{}, replacement interface{}) *Bulk {
b.bulk = b.bulk.Upsert(filter, replacement)
return b
}
// UpsertId queues an UpsertId operation for bulk execution.
// The replacement should be document without operator
func (b *Bulk) UpsertId(id interface{}, replacement interface{}) *Bulk {
b.bulk = b.bulk.UpsertId(id, replacement)
return b
}
// UpdateOne queues an UpdateOne operation for bulk execution.
// The update should contain operator
func (b *Bulk) UpdateOne(filter interface{}, update interface{}) *Bulk {
b.bulk = b.bulk.UpdateOne(filter, update)
return b
}
// UpdateId queues an UpdateId operation for bulk execution.
// The update should contain operator
func (b *Bulk) UpdateId(id interface{}, update interface{}) *Bulk {
b.bulk = b.bulk.UpdateId(id, update)
return b
}
// UpdateAll queues an UpdateAll operation for bulk execution.
// The update should contain operator
func (b *Bulk) UpdateAll(filter interface{}, update interface{}) *Bulk {
b.bulk = b.bulk.UpdateAll(filter, update)
return b
}
// Run executes the collected operations in a single bulk operation.
//
// A successful call resets the Bulk. If an error is returned, the internal
// queue of operations is unchanged, containing both successful and failed
// operations.
func (b *Bulk) Run() (result *qmgo.BulkResult, err error) {
err = b.brk.Do(b.config.Addr, func() error {
now := time.Now()
defer b.span.Finish()
result, err = b.bulk.Run(b.ctx)
if ok, elapse := slowLog(now, b.config.SlowQueryDuration); ok {
ext.Error.Set(b.span, true)
b.span.LogFields(log.String("event", "slow_query"), log.Int64("elapse", int64(elapse)))
}
metric.MongoClientReqDuration.Observe(time.Since(now).Seconds(), b.config.DBName, b.config.Addr, "bulk")
return err
}, accept)
return
}