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.data.pipeline.mysql.ingest;
19  
20  import com.google.common.base.Preconditions;
21  import org.apache.shardingsphere.data.pipeline.mysql.ingest.binlog.BinlogPosition;
22  import org.apache.shardingsphere.data.pipeline.core.ingest.position.DialectIngestPositionManager;
23  
24  import javax.sql.DataSource;
25  import java.sql.Connection;
26  import java.sql.PreparedStatement;
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  
30  /**
31   * Ingest position manager for MySQL.
32   */
33  public final class MySQLIngestPositionManager implements DialectIngestPositionManager {
34      
35      @Override
36      public BinlogPosition init(final DataSource dataSource, final String slotNameSuffix) throws SQLException {
37          try (Connection connection = dataSource.getConnection()) {
38              return getBinlogPosition(connection);
39          }
40      }
41      
42      @Override
43      public BinlogPosition init(final String data) {
44          String[] array = data.split("#");
45          Preconditions.checkArgument(2 == array.length, "Unknown binlog position: %s", data);
46          return new BinlogPosition(array[0], Long.parseLong(array[1]), 0L);
47      }
48      
49      private BinlogPosition getBinlogPosition(final Connection connection) throws SQLException {
50          String filename;
51          long position;
52          long serverId;
53          try (
54                  PreparedStatement preparedStatement = connection.prepareStatement("SHOW MASTER STATUS");
55                  ResultSet resultSet = preparedStatement.executeQuery()) {
56              resultSet.next();
57              filename = resultSet.getString(1);
58              position = resultSet.getLong(2);
59          }
60          try (
61                  PreparedStatement preparedStatement = connection.prepareStatement("SHOW VARIABLES LIKE 'server_id'");
62                  ResultSet resultSet = preparedStatement.executeQuery()) {
63              resultSet.next();
64              serverId = resultSet.getLong(2);
65          }
66          return new BinlogPosition(filename, position, serverId);
67      }
68      
69      @Override
70      public String getDatabaseType() {
71          return "MySQL";
72      }
73  }