-
Notifications
You must be signed in to change notification settings - Fork 24
/
statistics.go
155 lines (136 loc) · 4.88 KB
/
statistics.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
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package sink
import (
"context"
"sync/atomic"
"time"
"github.com/pingcap/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/tikv/migration/cdc/pkg/util"
"go.uber.org/zap"
)
const (
printStatusInterval = 30 * time.Second
flushMetricsInterval = 5 * time.Second
)
// NewStatistics creates a statistics
func NewStatistics(ctx context.Context, name string, opts map[string]string) *Statistics {
statistics := &Statistics{name: name, lastPrintStatusTime: time.Now()}
if cid, ok := opts[OptChangefeedID]; ok {
statistics.changefeedID = cid
}
if cid, ok := opts[OptCaptureAddr]; ok {
statistics.captureAddr = cid
}
statistics.metricExecTxnHis = execTxnHistogram.WithLabelValues(statistics.captureAddr, statistics.changefeedID, name)
statistics.metricExecBatchHis = execBatchHistogram.WithLabelValues(statistics.captureAddr, statistics.changefeedID, name)
statistics.metricExecErrCnt = executionErrorCounter.WithLabelValues(statistics.captureAddr, statistics.changefeedID, name)
statistics.metricInvalidKeyCnt = executionErrorCounter.WithLabelValues(statistics.captureAddr, statistics.changefeedID, name+"_invalid_key")
// Flush metrics in background for better accuracy and efficiency.
captureAddr, changefeedID := statistics.captureAddr, statistics.changefeedID
ticker := time.NewTicker(flushMetricsInterval)
go func() {
defer ticker.Stop()
metricTotalRows := totalRowsCountGauge.WithLabelValues(captureAddr, changefeedID)
metricTotalFlushedRows := totalFlushedRowsCountGauge.WithLabelValues(captureAddr, changefeedID)
defer func() {
totalRowsCountGauge.DeleteLabelValues(captureAddr, changefeedID)
totalFlushedRowsCountGauge.DeleteLabelValues(captureAddr, changefeedID)
}()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
metricTotalRows.Set(float64(atomic.LoadUint64(&statistics.totalRows)))
metricTotalFlushedRows.Set(float64(atomic.LoadUint64(&statistics.totalFlushedRows)))
}
}
}()
return statistics
}
// Statistics maintains some status and metrics of the Sink
type Statistics struct {
name string
captureAddr string
changefeedID string
totalRows uint64
totalFlushedRows uint64
totalDDLCount uint64
lastPrintStatusTotalRows uint64
lastPrintStatusTime time.Time
metricExecTxnHis prometheus.Observer
metricExecBatchHis prometheus.Observer
metricExecErrCnt prometheus.Counter
metricInvalidKeyCnt prometheus.Counter
}
// AddEntriesCount records total number of rows needs to flush
func (b *Statistics) AddEntriesCount(count int) {
atomic.AddUint64(&b.totalRows, uint64(count))
}
// SubRowsCount records total number of rows needs to flush
func (b *Statistics) SubRowsCount(count int) {
atomic.AddUint64(&b.totalRows, ^uint64(count-1))
}
// TotalRowsCount returns total number of rows
func (b *Statistics) TotalRowsCount() uint64 {
return atomic.LoadUint64(&b.totalRows)
}
// AddDDLCount records total number of ddl needs to flush
func (b *Statistics) AddDDLCount() {
atomic.AddUint64(&b.totalDDLCount, 1)
}
// AddInvalidKeyCount records total number of invalid keys
func (b *Statistics) AddInvalidKeyCount() {
b.metricInvalidKeyCnt.Inc()
}
// RecordBatchExecution records the cost time of batch execution and batch size
func (b *Statistics) RecordBatchExecution(executor func() (int, error)) error {
startTime := time.Now()
batchSize, err := executor()
if err != nil {
b.metricExecErrCnt.Inc()
return err
}
castTime := time.Since(startTime).Seconds()
b.metricExecTxnHis.Observe(castTime)
b.metricExecBatchHis.Observe(float64(batchSize))
atomic.AddUint64(&b.totalFlushedRows, uint64(batchSize))
return nil
}
// PrintStatus prints the status of the Sink
func (b *Statistics) PrintStatus(ctx context.Context) {
since := time.Since(b.lastPrintStatusTime)
if since < printStatusInterval {
return
}
totalRows := atomic.LoadUint64(&b.totalRows)
count := totalRows - b.lastPrintStatusTotalRows
seconds := since.Seconds()
var qps uint64
if seconds > 0 {
qps = count / uint64(seconds)
}
b.lastPrintStatusTime = time.Now()
b.lastPrintStatusTotalRows = totalRows
totalDDLCount := atomic.LoadUint64(&b.totalDDLCount)
atomic.StoreUint64(&b.totalDDLCount, 0)
log.Info("sink replication status",
zap.String("name", b.name),
zap.String("changefeed", b.changefeedID),
util.ZapFieldCapture(ctx),
zap.Uint64("count", count),
zap.Uint64("qps", qps),
zap.Uint64("ddl", totalDDLCount))
}