View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.replication.regionserver;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.conf.Configured;
24  import org.apache.hadoop.fs.FileSystem;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.Abortable;
27  import org.apache.hadoop.hbase.ChoreService;
28  import org.apache.hadoop.hbase.CoordinatedStateManager;
29  import org.apache.hadoop.hbase.HBaseConfiguration;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.Server;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.client.ClusterConnection;
34  import org.apache.hadoop.hbase.util.FSUtils;
35  import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
36  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
37  import org.apache.hadoop.util.Tool;
38  import org.apache.hadoop.util.ToolRunner;
39  
40  /**
41   * In a scenario of Replication based Disaster/Recovery, when hbase
42   * Master-Cluster crashes, this tool is used to sync-up the delta from Master to
43   * Slave using the info from Zookeeper. The tool will run on Master-Cluser, and
44   * assume ZK, Filesystem and NetWork still available after hbase crashes
45   *
46   * hbase org.apache.hadoop.hbase.replication.regionserver.ReplicationSyncUp
47   */
48  
49  public class ReplicationSyncUp extends Configured implements Tool {
50  
51    private static final Log LOG = LogFactory.getLog(ReplicationSyncUp.class.getName());
52  
53    private static Configuration conf;
54  
55    private static final long SLEEP_TIME = 10000;
56  
57    // although the tool is designed to be run on command line
58    // this api is provided for executing the tool through another app
59    public static void setConfigure(Configuration config) {
60      conf = config;
61    }
62  
63    /**
64     * Main program
65     * @param args
66     * @throws Exception
67     */
68    public static void main(String[] args) throws Exception {
69      if (conf == null) conf = HBaseConfiguration.create();
70      int ret = ToolRunner.run(conf, new ReplicationSyncUp(), args);
71      System.exit(ret);
72    }
73  
74    @Override
75    public int run(String[] args) throws Exception {
76      Replication replication;
77      ReplicationSourceManager manager;
78      FileSystem fs;
79      Path oldLogDir, logDir, rootDir;
80      ZooKeeperWatcher zkw;
81  
82      Abortable abortable = new Abortable() {
83        @Override
84        public void abort(String why, Throwable e) {
85        }
86  
87        @Override
88        public boolean isAborted() {
89          return false;
90        }
91      };
92  
93      zkw =
94          new ZooKeeperWatcher(conf, "syncupReplication" + System.currentTimeMillis(), abortable,
95              true);
96  
97      rootDir = FSUtils.getRootDir(conf);
98      fs = FileSystem.get(conf);
99      oldLogDir = new Path(rootDir, HConstants.HREGION_OLDLOGDIR_NAME);
100     logDir = new Path(rootDir, HConstants.HREGION_LOGDIR_NAME);
101 
102     System.out.println("Start Replication Server start");
103     replication = new Replication(new DummyServer(zkw), fs, logDir, oldLogDir);
104     manager = replication.getReplicationManager();
105     manager.init();
106 
107     try {
108       int numberOfOldSource = 1; // default wait once
109       while (numberOfOldSource > 0) {
110         Thread.sleep(SLEEP_TIME);
111         numberOfOldSource = manager.getOldSources().size();
112       }
113     } catch (InterruptedException e) {
114       System.err.println("didn't wait long enough:" + e);
115       return (-1);
116     }
117 
118     manager.join();
119     zkw.close();
120 
121     return (0);
122   }
123 
124   static class DummyServer implements Server {
125     String hostname;
126     ZooKeeperWatcher zkw;
127 
128     DummyServer(ZooKeeperWatcher zkw) {
129       // an unique name in case the first run fails
130       hostname = System.currentTimeMillis() + ".SyncUpTool.replication.org";
131       this.zkw = zkw;
132     }
133 
134     DummyServer(String hostname) {
135       this.hostname = hostname;
136     }
137 
138     @Override
139     public Configuration getConfiguration() {
140       return conf;
141     }
142 
143     @Override
144     public ZooKeeperWatcher getZooKeeper() {
145       return zkw;
146     }
147 
148     @Override
149     public CoordinatedStateManager getCoordinatedStateManager() {
150       return null;
151     }
152 
153     @Override
154     public MetaTableLocator getMetaTableLocator() {
155       return null;
156     }
157 
158     @Override
159     public ServerName getServerName() {
160       return ServerName.valueOf(hostname, 1234, 1L);
161     }
162 
163     @Override
164     public void abort(String why, Throwable e) {
165     }
166 
167     @Override
168     public boolean isAborted() {
169       return false;
170     }
171 
172     @Override
173     public void stop(String why) {
174     }
175 
176     @Override
177     public boolean isStopped() {
178       return false;
179     }
180 
181     @Override
182     public ClusterConnection getConnection() {
183       return null;
184     }
185 
186     @Override
187     public ChoreService getChoreService() {
188       return null;
189     }
190   }
191 }