View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.db.protocol.mysql.packet.binlog.management;
19  
20  import com.google.common.base.Preconditions;
21  import lombok.Getter;
22  import lombok.ToString;
23  import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinlogEventType;
24  import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.AbstractMySQLBinlogEventPacket;
25  import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.MySQLBinlogEventHeader;
26  import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
27  
28  /**
29   * MySQL binlog format description event packet.
30   *
31   * @see <a href="https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Format__description__event.html">FORMAT_DESCRIPTION_EVENT</a>
32   * @see <a href="https://dev.mysql.com/worklog/task/?id=2540#tabs-2540-4">WL#2540: Replication event checksums</a>
33   */
34  @Getter
35  @ToString
36  public final class MySQLBinlogFormatDescriptionEventPacket extends AbstractMySQLBinlogEventPacket {
37      
38      private final int binlogVersion = 4;
39      
40      private final byte[] mysqlServerVersion;
41      
42      private final int createTimestamp;
43      
44      private final int eventHeaderLength = 19;
45      
46      public MySQLBinlogFormatDescriptionEventPacket(final MySQLBinlogEventHeader binlogEventHeader, final MySQLPacketPayload payload) {
47          super(binlogEventHeader);
48          Preconditions.checkArgument(binlogVersion == payload.readInt2(), "Binlog version of FORMAT_DESCRIPTION_EVENT should always 4");
49          mysqlServerVersion = payload.readStringFixByBytes(50);
50          createTimestamp = payload.readInt4();
51          Preconditions.checkArgument(eventHeaderLength == payload.readInt1(), "Length of the Binlog Event Header should always be 19.");
52          skipTypeHeaderLength(payload);
53          skipCheckSums(payload);
54      }
55      
56      /**
57       * Type header length is not fixed value, it depends on mysql server version.
58       * Because the binlog event type may add in future version.
59       * During test, the length in version 5.7.21, the length of FORMAT_DESCRIPTION_EVENT is 95 not 84.
60       *
61       * @param payload MySQL binlog packet payload
62       */
63      private void skipTypeHeaderLength(final MySQLPacketPayload payload) {
64          payload.skipReserved(MySQLBinlogEventType.FORMAT_DESCRIPTION_EVENT.getValue() - 1);
65          int eventLength = payload.readInt1();
66          int remainLength = eventLength - 2 - 50 - 4 - 1 - (MySQLBinlogEventType.FORMAT_DESCRIPTION_EVENT.getValue() - 1) - 1;
67          payload.skipReserved(remainLength);
68      }
69      
70      private void skipCheckSums(final MySQLPacketPayload payload) {
71          int checksumAlgorithmFlag = payload.readInt1();
72          if (1 == checksumAlgorithmFlag) {
73              payload.skipReserved(4);
74          }
75      }
76      
77      @Override
78      protected void writeEvent(final MySQLPacketPayload payload) {
79          // TODO
80      }
81  }