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 
119     return (0);
120   }
121 
122   static class DummyServer implements Server {
123     String hostname;
124     ZooKeeperWatcher zkw;
125 
126     DummyServer(ZooKeeperWatcher zkw) {
127       // an unique name in case the first run fails
128       hostname = System.currentTimeMillis() + ".SyncUpTool.replication.org";
129       this.zkw = zkw;
130     }
131 
132     DummyServer(String hostname) {
133       this.hostname = hostname;
134     }
135 
136     @Override
137     public Configuration getConfiguration() {
138       return conf;
139     }
140 
141     @Override
142     public ZooKeeperWatcher getZooKeeper() {
143       return zkw;
144     }
145 
146     @Override
147     public CatalogTracker getCatalogTracker() {
148       return null;
149     }
150 
151     @Override
152     public ServerName getServerName() {
153       return new ServerName(hostname, 1234, 1L);
154     }
155 
156     @Override
157     public void abort(String why, Throwable e) {
158     }
159 
160     @Override
161     public boolean isAborted() {
162       return false;
163     }
164 
165     @Override
166     public void stop(String why) {
167     }
168 
169     @Override
170     public boolean isStopped() {
171       return false;
172     }
173   }
174 }