vitess.io/vitess@v0.16.2/go/vt/binlog/keyrange_filter.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package binlog 18 19 import ( 20 "fmt" 21 22 "vitess.io/vitess/go/vt/key" 23 "vitess.io/vitess/go/vt/log" 24 25 binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" 26 querypb "vitess.io/vitess/go/vt/proto/query" 27 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 28 ) 29 30 // keyRangeFilterFunc returns a function that calls callback only if statements 31 // in the transaction match the specified keyrange. The resulting function can be 32 // passed into the Streamer: bls.Stream(file, pos, sendTransaction) -> 33 // bls.Stream(file, pos, keyRangeFilterFunc(keyrange, sendTransaction)) 34 func keyRangeFilterFunc(keyrange *topodatapb.KeyRange, callback func(*binlogdatapb.BinlogTransaction) error) sendTransactionFunc { 35 return func(eventToken *querypb.EventToken, statements []FullBinlogStatement) error { 36 matched := false 37 filtered := make([]*binlogdatapb.BinlogTransaction_Statement, 0, len(statements)) 38 for _, statement := range statements { 39 switch statement.Statement.Category { 40 case binlogdatapb.BinlogTransaction_Statement_BL_SET: 41 filtered = append(filtered, statement.Statement) 42 case binlogdatapb.BinlogTransaction_Statement_BL_DDL: 43 log.Warningf("Not forwarding DDL: %s", statement.Statement.Sql) 44 continue 45 case binlogdatapb.BinlogTransaction_Statement_BL_INSERT, 46 binlogdatapb.BinlogTransaction_Statement_BL_UPDATE, 47 binlogdatapb.BinlogTransaction_Statement_BL_DELETE: 48 if statement.KeyspaceID == nil { 49 updateStreamErrors.Add("KeyRangeStream", 1) 50 return fmt.Errorf("SBR mode unsupported for streaming: %s", statement.Statement.Sql) 51 } 52 if !key.KeyRangeContains(keyrange, statement.KeyspaceID) { 53 continue 54 } 55 filtered = append(filtered, statement.Statement) 56 matched = true 57 case binlogdatapb.BinlogTransaction_Statement_BL_UNRECOGNIZED: 58 updateStreamErrors.Add("KeyRangeStream", 1) 59 log.Errorf("Error parsing keyspace id: %s", statement.Statement.Sql) 60 continue 61 } 62 } 63 trans := &binlogdatapb.BinlogTransaction{ 64 EventToken: eventToken, 65 } 66 if matched { 67 trans.Statements = filtered 68 } 69 return callback(trans) 70 } 71 }