1
1
package drainer
2
2
3
3
import (
4
+ "fmt"
4
5
"io"
5
6
"strconv"
6
7
"sync"
@@ -21,6 +22,9 @@ import (
21
22
pb "github.com/pingcap/tipb/go-binlog"
22
23
)
23
24
25
+ // sleep 10 millisecond to wait matched binlog
26
+ var waitMatchedTime = 10 * time .Millisecond
27
+
24
28
type binlogEntity struct {
25
29
tp pb.BinlogType
26
30
startTS int64
@@ -157,10 +161,12 @@ func (p *Pump) publish(t *tikv.LockResolver) {
157
161
case entity = <- p .binlogChan :
158
162
}
159
163
164
+ begin := time .Now ()
160
165
switch entity .tp {
161
166
case pb .BinlogType_Prewrite :
162
167
// while we meet the prebinlog we must find it's mathced commit binlog
163
168
p .mustFindCommitBinlog (t , entity .startTS )
169
+ findMatchedBinlogHistogram .WithLabelValues (p .nodeID ).Observe (time .Since (begin ).Seconds ())
164
170
case pb .BinlogType_Commit , pb .BinlogType_Rollback :
165
171
// if the commitTs is larger than maxCommitTs,
166
172
// we would publish all binlogs:
@@ -175,6 +181,7 @@ func (p *Pump) publish(t *tikv.LockResolver) {
175
181
} else {
176
182
binlogs = make (map [int64 ]* binlogItem )
177
183
}
184
+ publishBinlogHistogram .WithLabelValues (p .nodeID ).Observe (time .Since (begin ).Seconds ())
178
185
}
179
186
}
180
187
@@ -195,7 +202,7 @@ func (p *Pump) mustFindCommitBinlog(t *tikv.LockResolver, startTS int64) {
195
202
196
203
b , ok := p .getPrewriteBinlogEntity (startTS )
197
204
if ok {
198
- time .Sleep (waitTime )
205
+ time .Sleep (waitMatchedTime )
199
206
// check again after sleep a moment
200
207
b , ok = p .getPrewriteBinlogEntity (startTS )
201
208
if ok {
@@ -296,7 +303,7 @@ func (p *Pump) putIntoHeap(items map[int64]*binlogItem) {
296
303
// if we meet a smaller binlog, we should ignore it. because we have published binlogs that before window low boundary
297
304
continue
298
305
}
299
- p .bh .push (p .ctx , item )
306
+ p .bh .push (p .ctx , item , true )
300
307
}
301
308
302
309
errorBinlogCount .Add (float64 (errorBinlogs ))
@@ -352,8 +359,9 @@ func (p *Pump) getDDLJob(id int64) (*model.Job, error) {
352
359
}
353
360
354
361
func (p * Pump ) collectBinlogs (windowLower , windowUpper int64 ) binlogItems {
362
+ begin := time .Now ()
355
363
var bs binlogItems
356
- item := p .bh .peek ()
364
+ item := p .bh .pop ()
357
365
for item != nil && item .binlog .CommitTs <= windowUpper {
358
366
// make sure to discard old binlogs whose commitTS is earlier or equal minTS
359
367
if item .binlog .CommitTs > windowLower {
@@ -363,10 +371,14 @@ func (p *Pump) collectBinlogs(windowLower, windowUpper int64) binlogItems {
363
371
if ComparePos (p .currentPos , item .pos ) == - 1 {
364
372
p .currentPos = item .pos
365
373
}
366
- _ = p .bh .pop ()
367
- item = p .bh .peek ()
374
+ item = p .bh .pop ()
375
+ }
376
+ if item != nil {
377
+ p .bh .push (p .ctx , item , false )
368
378
}
369
379
380
+ publishBinlogHistogram .WithLabelValues (fmt .Sprintf ("%s_collect_binlogs" , p .nodeID )).Observe (time .Since (begin ).Seconds ())
381
+
370
382
return bs
371
383
}
372
384
0 commit comments