001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.replication.regionserver;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.conf.Configured;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.Abortable;
026import org.apache.hadoop.hbase.ChoreService;
027import org.apache.hadoop.hbase.CoordinatedStateManager;
028import org.apache.hadoop.hbase.HBaseConfiguration;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.Server;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.client.ClusterConnection;
033import org.apache.hadoop.hbase.client.Connection;
034import org.apache.hadoop.hbase.util.FSUtils;
035import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
036import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
037import org.apache.hadoop.util.Tool;
038import org.apache.hadoop.util.ToolRunner;
039import org.apache.yetus.audience.InterfaceAudience;
040
041/**
042 * In a scenario of Replication based Disaster/Recovery, when hbase Master-Cluster crashes, this
043 * tool is used to sync-up the delta from Master to Slave using the info from ZooKeeper. The tool
044 * will run on Master-Cluser, and assume ZK, Filesystem and NetWork still available after hbase
045 * crashes
046 *
047 * <pre>
048 * hbase org.apache.hadoop.hbase.replication.regionserver.ReplicationSyncUp
049 * </pre>
050 */
051@InterfaceAudience.Private
052public class ReplicationSyncUp extends Configured implements Tool {
053
054  private static final long SLEEP_TIME = 10000;
055
056  /**
057   * Main program
058   */
059  public static void main(String[] args) throws Exception {
060    int ret = ToolRunner.run(HBaseConfiguration.create(), new ReplicationSyncUp(), args);
061    System.exit(ret);
062  }
063
064  @Override
065  public int run(String[] args) throws Exception {
066    Abortable abortable = new Abortable() {
067      @Override
068      public void abort(String why, Throwable e) {
069      }
070
071      @Override
072      public boolean isAborted() {
073        return false;
074      }
075    };
076    Configuration conf = getConf();
077    try (ZKWatcher zkw =
078      new ZKWatcher(conf, "syncupReplication" + System.currentTimeMillis(), abortable, true)) {
079      Path walRootDir = FSUtils.getWALRootDir(conf);
080      FileSystem fs = FSUtils.getWALFileSystem(conf);
081      Path oldLogDir = new Path(walRootDir, HConstants.HREGION_OLDLOGDIR_NAME);
082      Path logDir = new Path(walRootDir, HConstants.HREGION_LOGDIR_NAME);
083
084      System.out.println("Start Replication Server start");
085      Replication replication = new Replication();
086      replication.initialize(new DummyServer(zkw), fs, logDir, oldLogDir, null);
087      ReplicationSourceManager manager = replication.getReplicationManager();
088      manager.init().get();
089      while (manager.activeFailoverTaskCount() > 0) {
090        Thread.sleep(SLEEP_TIME);
091      }
092      while (manager.getOldSources().size() > 0) {
093        Thread.sleep(SLEEP_TIME);
094      }
095      manager.join();
096    } catch (InterruptedException e) {
097      System.err.println("didn't wait long enough:" + e);
098      return -1;
099    }
100    return 0;
101  }
102
103  class DummyServer implements Server {
104    String hostname;
105    ZKWatcher zkw;
106
107    DummyServer(ZKWatcher zkw) {
108      // a unique name in case the first run fails
109      hostname = System.currentTimeMillis() + ".SyncUpTool.replication.org";
110      this.zkw = zkw;
111    }
112
113    DummyServer(String hostname) {
114      this.hostname = hostname;
115    }
116
117    @Override
118    public Configuration getConfiguration() {
119      return getConf();
120    }
121
122    @Override
123    public ZKWatcher getZooKeeper() {
124      return zkw;
125    }
126
127    @Override
128    public CoordinatedStateManager getCoordinatedStateManager() {
129      return null;
130    }
131
132    @Override
133    public MetaTableLocator getMetaTableLocator() {
134      return null;
135    }
136
137    @Override
138    public ServerName getServerName() {
139      return ServerName.valueOf(hostname, 1234, 1L);
140    }
141
142    @Override
143    public void abort(String why, Throwable e) {
144    }
145
146    @Override
147    public boolean isAborted() {
148      return false;
149    }
150
151    @Override
152    public void stop(String why) {
153    }
154
155    @Override
156    public boolean isStopped() {
157      return false;
158    }
159
160    @Override
161    public ClusterConnection getConnection() {
162      return null;
163    }
164
165    @Override
166    public ChoreService getChoreService() {
167      return null;
168    }
169
170    @Override
171    public ClusterConnection getClusterConnection() {
172      return null;
173    }
174
175    @Override
176    public FileSystem getFileSystem() {
177      return null;
178    }
179
180    @Override
181    public boolean isStopping() {
182      return false;
183    }
184
185    @Override
186    public Connection createConnection(Configuration conf) throws IOException {
187      return null;
188    }
189  }
190}