vitess.io/vitess@v0.16.2/go/mysql/binlog_event_mysql56.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 mysql
    18  
    19  import (
    20  	"encoding/binary"
    21  
    22  	"vitess.io/vitess/go/vt/proto/vtrpc"
    23  	"vitess.io/vitess/go/vt/vterrors"
    24  )
    25  
    26  // mysql56BinlogEvent wraps a raw packet buffer and provides methods to examine
    27  // it by implementing BinlogEvent. Some methods are pulled in from
    28  // binlogEvent.
    29  type mysql56BinlogEvent struct {
    30  	binlogEvent
    31  	semiSyncAckRequested bool
    32  }
    33  
    34  // NewMysql56BinlogEventWithSemiSyncInfo creates a BinlogEvent from given byte array
    35  func NewMysql56BinlogEventWithSemiSyncInfo(buf []byte, semiSyncAckRequested bool) BinlogEvent {
    36  	return mysql56BinlogEvent{binlogEvent: binlogEvent(buf), semiSyncAckRequested: semiSyncAckRequested}
    37  }
    38  
    39  // NewMysql56BinlogEvent creates a BinlogEvent from given byte array
    40  func NewMysql56BinlogEvent(buf []byte) BinlogEvent {
    41  	return mysql56BinlogEvent{binlogEvent: binlogEvent(buf)}
    42  }
    43  
    44  // IsSemiSyncAckRequested implements BinlogEvent.IsSemiSyncAckRequested().
    45  func (ev mysql56BinlogEvent) IsSemiSyncAckRequested() bool {
    46  	return ev.semiSyncAckRequested
    47  }
    48  
    49  // IsGTID implements BinlogEvent.IsGTID().
    50  func (ev mysql56BinlogEvent) IsGTID() bool {
    51  	return ev.Type() == eGTIDEvent
    52  }
    53  
    54  // GTID implements BinlogEvent.GTID().
    55  //
    56  // Expected format:
    57  //
    58  //	# bytes   field
    59  //	1         flags
    60  //	16        SID (server UUID)
    61  //	8         GNO (sequence number, signed int)
    62  func (ev mysql56BinlogEvent) GTID(f BinlogFormat) (GTID, bool, error) {
    63  	data := ev.Bytes()[f.HeaderLength:]
    64  	var sid SID
    65  	copy(sid[:], data[1:1+16])
    66  	gno := int64(binary.LittleEndian.Uint64(data[1+16 : 1+16+8]))
    67  	return Mysql56GTID{Server: sid, Sequence: gno}, false /* hasBegin */, nil
    68  }
    69  
    70  // PreviousGTIDs implements BinlogEvent.PreviousGTIDs().
    71  func (ev mysql56BinlogEvent) PreviousGTIDs(f BinlogFormat) (Position, error) {
    72  	data := ev.Bytes()[f.HeaderLength:]
    73  	set, err := NewMysql56GTIDSetFromSIDBlock(data)
    74  	if err != nil {
    75  		return Position{}, err
    76  	}
    77  	return Position{
    78  		GTIDSet: set,
    79  	}, nil
    80  }
    81  
    82  // StripChecksum implements BinlogEvent.StripChecksum().
    83  func (ev mysql56BinlogEvent) StripChecksum(f BinlogFormat) (BinlogEvent, []byte, error) {
    84  	switch f.ChecksumAlgorithm {
    85  	case BinlogChecksumAlgOff, BinlogChecksumAlgUndef:
    86  		// There is no checksum.
    87  		return ev, nil, nil
    88  	case BinlogChecksumAlgCRC32:
    89  		// Checksum is the last 4 bytes of the event buffer.
    90  		data := ev.Bytes()
    91  		length := len(data)
    92  		checksum := data[length-4:]
    93  		data = data[:length-4]
    94  		return mysql56BinlogEvent{binlogEvent: binlogEvent(data)}, checksum, nil
    95  	default:
    96  		// MySQL 5.6 does not guarantee that future checksum algorithms will be
    97  		// 4 bytes, so we can't support them a priori.
    98  		return ev, nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "unsupported checksum algorithm: %v", f.ChecksumAlgorithm)
    99  	}
   100  }