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();
089      int numberOfOldSource = 1; // default wait once
090      while (numberOfOldSource > 0) {
091        Thread.sleep(SLEEP_TIME);
092        numberOfOldSource = manager.getOldSources().size();
093      }
094      manager.join();
095    } catch (InterruptedException e) {
096      System.err.println("didn't wait long enough:" + e);
097      return -1;
098    }
099    return 0;
100  }
101
102  class DummyServer implements Server {
103    String hostname;
104    ZKWatcher zkw;
105
106    DummyServer(ZKWatcher zkw) {
107      // an unique name in case the first run fails
108      hostname = System.currentTimeMillis() + ".SyncUpTool.replication.org";
109      this.zkw = zkw;
110    }
111
112    DummyServer(String hostname) {
113      this.hostname = hostname;
114    }
115
116    @Override
117    public Configuration getConfiguration() {
118      return getConf();
119    }
120
121    @Override
122    public ZKWatcher getZooKeeper() {
123      return zkw;
124    }
125
126    @Override
127    public CoordinatedStateManager getCoordinatedStateManager() {
128      return null;
129    }
130
131    @Override
132    public MetaTableLocator getMetaTableLocator() {
133      return null;
134    }
135
136    @Override
137    public ServerName getServerName() {
138      return ServerName.valueOf(hostname, 1234, 1L);
139    }
140
141    @Override
142    public void abort(String why, Throwable e) {
143    }
144
145    @Override
146    public boolean isAborted() {
147      return false;
148    }
149
150    @Override
151    public void stop(String why) {
152    }
153
154    @Override
155    public boolean isStopped() {
156      return false;
157    }
158
159    @Override
160    public ClusterConnection getConnection() {
161      return null;
162    }
163
164    @Override
165    public ChoreService getChoreService() {
166      return null;
167    }
168
169    @Override
170    public ClusterConnection getClusterConnection() {
171      return null;
172    }
173
174    @Override
175    public FileSystem getFileSystem() {
176      return null;
177    }
178
179    @Override
180    public boolean isStopping() {
181      return false;
182    }
183
184    @Override
185    public Connection createConnection(Configuration conf) throws IOException {
186      return null;
187    }
188  }
189}