Merge pull request #30 from zhaoqun911/mysql-develop

[ROCKETMQ-293] Add high-availability support for rocketmq-mysql-replicator
diff --git a/rocketmq-mysql/LICENSE-BIN b/rocketmq-mysql/LICENSE-BIN
index 22b0aa4..c299852 100644
--- a/rocketmq-mysql/LICENSE-BIN
+++ b/rocketmq-mysql/LICENSE-BIN
@@ -299,3 +299,13 @@
 ------
 This product has a bundle mysql-binlog-connector-java, which is available under the ASL2 License.
 The source code of mysql-binlog-connector-java can be found at https://github.com/shyiko/mysql-binlog-connector-java.
+
+------
+This product has a bundle zookeeper, which is available under the ASL2 License.
+The source code of zookeeper can be found at https://github.com/apache/zookeeper.
+
+Apache ZooKeeper
+Copyright 2009-2014 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
\ No newline at end of file
diff --git a/rocketmq-mysql/pom.xml b/rocketmq-mysql/pom.xml
index 23e7468..6f3569a 100644
--- a/rocketmq-mysql/pom.xml
+++ b/rocketmq-mysql/pom.xml
@@ -112,6 +112,11 @@
             <version>1.9</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.curator</groupId>
+            <artifactId>curator-recipes</artifactId>
+            <version>2.8.0</version>
+        </dependency>
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.11</version>
@@ -210,6 +215,7 @@
                 <configuration>
                     <excludes>
                         <exclude>README.md</exclude>
+                        <exclude>NOTICE-BIN</exclude>
                     </excludes>
                 </configuration>
             </plugin>
diff --git a/rocketmq-mysql/src/main/assembly/scripts/start.sh b/rocketmq-mysql/src/main/assembly/scripts/start.sh
index e159f36..047458d 100644
--- a/rocketmq-mysql/src/main/assembly/scripts/start.sh
+++ b/rocketmq-mysql/src/main/assembly/scripts/start.sh
@@ -1,5 +1,20 @@
 #!/usr/bin/env bash
 
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
 binPath=$(cd "$(dirname "$0")"; pwd);
 cd $binPath
 cd ..
diff --git a/rocketmq-mysql/src/main/assembly/scripts/stop.sh b/rocketmq-mysql/src/main/assembly/scripts/stop.sh
index f0e3c0d..fdebf5c 100755
--- a/rocketmq-mysql/src/main/assembly/scripts/stop.sh
+++ b/rocketmq-mysql/src/main/assembly/scripts/stop.sh
@@ -1,5 +1,20 @@
 #!/bin/bash
 
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
 PROGRAM_NAME="org.apache.rocketmq.mysql.Replicator"
 PIDS=`ps -ef | grep $PROGRAM_NAME | grep -v "grep" | awk '{print $2}'`
 
diff --git a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/Config.java b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/Config.java
index 6c14cb4..0ddc055 100644
--- a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/Config.java
+++ b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/Config.java
@@ -33,6 +33,8 @@
     public String mqNamesrvAddr;
     public String mqTopic;
 
+    public String zkAddr;
+
     public String startType = "DEFAULT";
     public String binlogFilename;
     public Long nextPosition;
@@ -127,4 +129,8 @@
     public void setStartType(String startType) {
         this.startType = startType;
     }
+
+    public void setZkAddr(String zkAddr) {
+        this.zkAddr = zkAddr;
+    }
 }
\ No newline at end of file
diff --git a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/Replicator.java b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/Replicator.java
index ae3c984..e0705ca 100644
--- a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/Replicator.java
+++ b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/Replicator.java
@@ -19,6 +19,7 @@
 
 import org.apache.rocketmq.mysql.binlog.EventProcessor;
 import org.apache.rocketmq.mysql.binlog.Transaction;
+import org.apache.rocketmq.mysql.ha.MasterElectionLatch;
 import org.apache.rocketmq.mysql.position.BinlogPositionLogThread;
 import org.apache.rocketmq.mysql.productor.RocketMQProducer;
 import org.apache.rocketmq.mysql.position.BinlogPosition;
@@ -37,6 +38,10 @@
 
     private RocketMQProducer rocketMQProducer;
 
+    private BinlogPositionLogThread binlogPositionLogThread;
+
+    private MasterElectionLatch masterElectionLatch;
+
     private Object lock = new Object();
     private BinlogPosition nextBinlogPosition;
     private long nextQueueOffset;
@@ -57,11 +62,12 @@
             rocketMQProducer = new RocketMQProducer(config);
             rocketMQProducer.start();
 
-            BinlogPositionLogThread binlogPositionLogThread = new BinlogPositionLogThread(this);
-            binlogPositionLogThread.start();
-
-            eventProcessor = new EventProcessor(this);
-            eventProcessor.start();
+            if (config.zkAddr != null) {
+                masterElectionLatch = new MasterElectionLatch(this);
+                masterElectionLatch.elect();
+            } else {
+                startProcess();
+            }
 
         } catch (Exception e) {
             LOGGER.error("Start error.", e);
@@ -69,6 +75,23 @@
         }
     }
 
+    public void startProcess() {
+
+        binlogPositionLogThread = new BinlogPositionLogThread(this);
+        binlogPositionLogThread.start();
+
+        eventProcessor = new EventProcessor(this);
+        eventProcessor.start();
+    }
+
+    public void stopProcess() {
+
+        binlogPositionLogThread.interrupt();
+
+        eventProcessor.stopProcess();
+        eventProcessor.interrupt();
+    }
+
     public void commit(Transaction transaction, boolean isComplete) {
 
         String json = transaction.toJson();
diff --git a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/binlog/EventProcessor.java b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/binlog/EventProcessor.java
index a730403..28657cb 100644
--- a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/binlog/EventProcessor.java
+++ b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/binlog/EventProcessor.java
@@ -46,12 +46,14 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class EventProcessor {
+public class EventProcessor extends Thread {
     private static final Logger LOGGER = LoggerFactory.getLogger(EventProcessor.class);
 
     private Replicator replicator;
     private Config config;
 
+    private volatile boolean eof = false;
+
     private DataSource dataSource;
 
     private BinlogPositionManager binlogPositionManager;
@@ -74,7 +76,29 @@
         this.config = replicator.getConfig();
     }
 
-    public void start() throws Exception {
+    @Override
+    public void run() {
+
+        do {
+            try {
+                startProcess();
+
+                break;
+            } catch (Exception e) {
+                LOGGER.error("Start error.", e);
+            }
+
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+            }
+
+        } while (!eof);
+
+        LOGGER.info("Process thread stopped.");
+    }
+
+    public void startProcess() throws Exception {
 
         initDataSource();
 
@@ -102,14 +126,14 @@
 
         binaryLogClient.connect(3000);
 
-        LOGGER.info("Started.");
+        LOGGER.info("Process thread started.");
 
         doProcess();
     }
 
     private void doProcess() {
 
-        while (true) {
+        while (!eof) {
 
             try {
                 Event event = queue.poll(1000, TimeUnit.MILLISECONDS);
@@ -147,16 +171,18 @@
                         break;
 
                 }
+            } catch (InterruptedException ex) {
+                LOGGER.info("Process thread interrupted.");
+
             } catch (Exception e) {
                 LOGGER.error("Binlog process error.", e);
             }
-
         }
     }
 
     private void checkConnection() throws Exception {
 
-        if (!binaryLogClient.isConnected()) {
+        if (!binaryLogClient.isConnected() && !eof) {
             BinlogPosition binlogPosition = replicator.getNextBinlogPosition();
             if (binlogPosition != null) {
                 binaryLogClient.setBinlogFilename(binlogPosition.getBinlogFilename());
@@ -167,6 +193,19 @@
         }
     }
 
+    public void stopProcess() {
+
+        eof = true;
+
+        try {
+            if (binaryLogClient != null) {
+                binaryLogClient.disconnect();
+            }
+        } catch (Exception e) {
+            LOGGER.error("stop error", e);
+        }
+    }
+
     private void processTableMapEvent(Event event) {
         TableMapEventData data = event.getData();
         String dbName = data.getDatabase();
@@ -278,8 +317,4 @@
         dataSource = DruidDataSourceFactory.createDataSource(map);
     }
 
-    public Config getConfig() {
-        return config;
-    }
-
-}
+}
\ No newline at end of file
diff --git a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/ha/MasterElectionLatch.java b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/ha/MasterElectionLatch.java
new file mode 100644
index 0000000..8bb0df3
--- /dev/null
+++ b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/ha/MasterElectionLatch.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.rocketmq.mysql.ha;
+
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.framework.recipes.leader.LeaderLatch;
+import org.apache.curator.framework.recipes.leader.LeaderLatchListener;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.rocketmq.mysql.Replicator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MasterElectionLatch implements LeaderLatchListener {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MasterElectionLatch.class);
+
+    private Replicator replicator;
+
+    public MasterElectionLatch(Replicator replicator) {
+
+        this.replicator = replicator;
+    }
+
+    public void elect() throws Exception {
+
+        CuratorFramework client = CuratorFrameworkFactory.builder()
+            .connectString(replicator.getConfig().zkAddr)
+            .retryPolicy(new ExponentialBackoffRetry(1000, 3))
+            .sessionTimeoutMs(3000)
+            .connectionTimeoutMs(3000)
+            .namespace("rocketmq-mysql-replicator")
+            .build();
+        client.start();
+
+        LeaderLatch leaderLatch = new LeaderLatch(client, "/master", "replicator");
+        leaderLatch.addListener(this);
+        leaderLatch.start();
+    }
+
+    @Override
+    public void isLeader() {
+
+        LOGGER.info("ZK_EVENT:isLeader!");
+
+        replicator.startProcess();
+    }
+
+    @Override
+    public void notLeader() {
+
+        LOGGER.info("ZK_EVENT:notLeader!");
+
+        replicator.stopProcess();
+    }
+
+
+}
diff --git a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/position/BinlogPositionLogThread.java b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/position/BinlogPositionLogThread.java
index dedb08f..6c15195 100644
--- a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/position/BinlogPositionLogThread.java
+++ b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/position/BinlogPositionLogThread.java
@@ -38,7 +38,7 @@
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
-                logger.error("Offset thread interrupted.", e);
+                logger.info("Offset thread interrupted.");
             }
 
             replicator.logPosition();
diff --git a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/position/BinlogPositionManager.java b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/position/BinlogPositionManager.java
index fd6555c..27bb375 100644
--- a/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/position/BinlogPositionManager.java
+++ b/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/position/BinlogPositionManager.java
@@ -109,6 +109,7 @@
                 nextPosition = js.getLong("nextPosition");
             }
         }
+        consumer.shutdown();
 
     }
 
diff --git a/rocketmq-mysql/src/main/resources/rocketmq_mysql.conf b/rocketmq-mysql/src/main/resources/rocketmq_mysql.conf
index 4a7a35f..00878ce 100644
--- a/rocketmq-mysql/src/main/resources/rocketmq_mysql.conf
+++ b/rocketmq-mysql/src/main/resources/rocketmq_mysql.conf
@@ -22,6 +22,8 @@
 mqNamesrvAddr=
 mqTopic=
 
+#zkAddr=
+
 #startType=
 #binlogFilename=
 #nextPosition=