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.HBaseConfiguration;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.Server;
30  import org.apache.hadoop.hbase.ServerName;
31  import org.apache.hadoop.hbase.catalog.CatalogTracker;
32  import org.apache.hadoop.hbase.replication.regionserver.Replication;
33  import org.apache.hadoop.hbase.replication.regionserver.ReplicationSourceManager;
34  import org.apache.hadoop.hbase.util.FSUtils;
35  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
36  import org.apache.hadoop.util.Tool;
37  import org.apache.hadoop.util.ToolRunner;
38  
39  /**
40   * In a scenario of Replication based Disaster/Recovery, when hbase
41   * Master-Cluster crashes, this tool is used to sync-up the delta from Master to
42   * Slave using the info from Zookeeper. The tool will run on Master-Cluser, and
43   * assume ZK, Filesystem and NetWork still available after hbase crashes
44   *
45   * hbase org.apache.hadoop.hbase.replication.regionserver.ReplicationSyncUp
46   */
47  
48  public class ReplicationSyncUp extends Configured implements Tool {
49  
50    static final Log LOG = LogFactory.getLog(ReplicationSyncUp.class.getName());
51  
52    private static Configuration conf;
53  
54    private static final long SLEEP_TIME = 10000;
55  
56    // although the tool is designed to be run on command line
57    // this api is provided for executing the tool through another app
58    public static void setConfigure(Configuration config) {
59      conf = config;
60    }
61  
62    /**
63     * Main program
64     * @param args
65     * @throws Exception
66     */
67    public static void main(String[] args) throws Exception {
68      if (conf == null) conf = HBaseConfiguration.create();
69      int ret = ToolRunner.run(conf, new ReplicationSyncUp(), args);
70      System.exit(ret);
71    }
72  
73    @Override
74    public int run(String[] args) throws Exception {
75      Replication replication;
76      ReplicationSourceManager manager;
77      FileSystem fs;
78      Path oldLogDir, logDir, rootDir;
79      ZooKeeperWatcher zkw;
80  
81      Abortable abortable = new Abortable() {
82        @Override
83        public void abort(String why, Throwable e) {
84        }
85  
86        @Override
87        public boolean isAborted() {
88          return false;
89        }
90      };
91  
92      zkw =
93          new ZooKeeperWatcher(conf, "syncupReplication" + System.currentTimeMillis(), abortable,
94              true);
95  
96      rootDir = FSUtils.getRootDir(conf);
97      fs = FileSystem.get(conf);
98      oldLogDir = new Path(rootDir, HConstants.HREGION_OLDLOGDIR_NAME);
99      logDir = new Path(rootDir, HConstants.HREGION_LOGDIR_NAME);
100 
101     System.out.println("Start Replication Server start");
102     replication = new Replication(new DummyServer(zkw), fs, logDir, oldLogDir);
103     manager = replication.getReplicationManager();
104     manager.init();
105 
106     try {
107       int numberOfOldSource = 1; // default wait once
108       while (numberOfOldSource > 0) {
109         Thread.sleep(SLEEP_TIME);
110         numberOfOldSource = manager.getOldSources().size();
111       }
112     } catch (InterruptedException e) {
113       System.err.println("didn't wait long enough:" + e);
114       return (-1);
115     }
116 
117     manager.join();
118     zkw.close();
119 
120     return (0);
121   }
122 
123   static class DummyServer implements Server {
124     String hostname;
125     ZooKeeperWatcher zkw;
126 
127     DummyServer(ZooKeeperWatcher zkw) {
128       // an unique name in case the first run fails
129       hostname = System.currentTimeMillis() + ".SyncUpTool.replication.org";
130       this.zkw = zkw;
131     }
132 
133     DummyServer(String hostname) {
134       this.hostname = hostname;
135     }
136 
137     @Override
138     public Configuration getConfiguration() {
139       return conf;
140     }
141 
142     @Override
143     public ZooKeeperWatcher getZooKeeper() {
144       return zkw;
145     }
146 
147     @Override
148     public CatalogTracker getCatalogTracker() {
149       return null;
150     }
151 
152     @Override
153     public ServerName getServerName() {
154       return new ServerName(hostname, 1234, 1L);
155     }
156 
157     @Override
158     public void abort(String why, Throwable e) {
159     }
160 
161     @Override
162     public boolean isAborted() {
163       return false;
164     }
165 
166     @Override
167     public void stop(String why) {
168     }
169 
170     @Override
171     public boolean isStopped() {
172       return false;
173     }
174   }
175 }