-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
resultstreamer.go
149 lines (129 loc) · 3.5 KB
/
resultstreamer.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
/*
Copyright 2019 The Vitess 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 vstreamer
import (
"context"
"fmt"
"time"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/dbconfigs"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp"
)
// resultStreamer streams the results of the requested query
// along with the GTID of the snapshot. This is used by vdiff
// to synchronize the target to that GTID before comparing
// the results.
type resultStreamer struct {
ctx context.Context
cancel func()
cp dbconfigs.Connector
query string
tableName sqlparser.IdentifierCS
send func(*binlogdatapb.VStreamResultsResponse) error
vse *Engine
pktsize PacketSizer
}
func newResultStreamer(ctx context.Context, cp dbconfigs.Connector, query string, send func(*binlogdatapb.VStreamResultsResponse) error, vse *Engine) *resultStreamer {
ctx, cancel := context.WithCancel(ctx)
return &resultStreamer{
ctx: ctx,
cancel: cancel,
cp: cp,
query: query,
send: send,
vse: vse,
pktsize: DefaultPacketSizer(),
}
}
func (rs *resultStreamer) Cancel() {
rs.cancel()
}
func (rs *resultStreamer) Stream() error {
_, fromTable, err := analyzeSelect(rs.query)
if err != nil {
return err
}
rs.tableName = fromTable
conn, err := snapshotConnect(rs.ctx, rs.cp)
if err != nil {
return err
}
defer conn.Close()
gtid, rotatedLog, err := conn.streamWithSnapshot(rs.ctx, rs.tableName.String(), rs.query)
if rotatedLog {
rs.vse.vstreamerFlushedBinlogs.Add(1)
}
if err != nil {
return err
}
// first call the callback with the fields
flds, err := conn.Fields()
if err != nil {
return err
}
err = rs.send(&binlogdatapb.VStreamResultsResponse{
Fields: flds,
Gtid: gtid,
})
if err != nil {
return fmt.Errorf("stream send error: %v", err)
}
response := &binlogdatapb.VStreamResultsResponse{}
byteCount := 0
for {
select {
case <-rs.ctx.Done():
return fmt.Errorf("stream ended: %v", rs.ctx.Err())
default:
}
// check throttler.
if !rs.vse.throttlerClient.ThrottleCheckOKOrWaitAppName(rs.ctx, throttlerapp.ResultStreamerName) {
continue
}
row, err := conn.FetchNext(nil)
if err != nil {
return err
}
if row == nil {
break
}
response.Rows = append(response.Rows, sqltypes.RowToProto3(row))
for _, s := range row {
byteCount += s.Len()
}
if rs.pktsize.ShouldSend(byteCount) {
rs.vse.resultStreamerNumRows.Add(int64(len(response.Rows)))
rs.vse.resultStreamerNumPackets.Add(int64(1))
startSend := time.Now()
err = rs.send(response)
if err != nil {
return err
}
rs.pktsize.Record(byteCount, time.Since(startSend))
// empty the rows so we start over, but we keep the
// same capacity
response.Rows = response.Rows[:0]
byteCount = 0
}
}
if len(response.Rows) > 0 {
rs.vse.resultStreamerNumRows.Add(int64(len(response.Rows)))
err = rs.send(response)
if err != nil {
return err
}
}
return nil
}