View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.replication.regionserver;
20  
21  import static org.apache.hadoop.hbase.replication.ReplicationSourceDummy.fakeExceptionMessage;
22  
23  import java.io.IOException;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.ClusterId;
31  import org.apache.hadoop.hbase.HBaseConfiguration;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HColumnDescriptor;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.HRegionInfo;
36  import org.apache.hadoop.hbase.HTableDescriptor;
37  import org.apache.hadoop.hbase.TableName;
38  import org.apache.hadoop.hbase.replication.ReplicationSourceDummy;
39  import org.apache.hadoop.hbase.replication.ReplicationStateZKBase;
40  import org.apache.hadoop.hbase.replication.regionserver.helper.DummyServer;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.apache.hadoop.hbase.util.FSUtils;
43  import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
44  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
45  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
46  
47  import org.junit.After;
48  import org.junit.AfterClass;
49  import org.junit.Before;
50  import org.junit.BeforeClass;
51  import org.junit.Rule;
52  import org.junit.rules.TestName;
53  
54  public abstract class TestReplicationSourceManagerBase {
55  
56    private static final Log LOG =
57      LogFactory.getLog(TestReplicationSourceManagerBase.class);
58  
59    protected static Configuration conf;
60    protected static HBaseTestingUtility utility;
61    protected static Replication replication;
62    protected static ReplicationSourceManager manager;
63    protected static ZooKeeperWatcher zkw;
64    protected static HTableDescriptor htd;
65    protected static HRegionInfo hri;
66  
67    protected static final byte[] r1 = Bytes.toBytes("r1");
68    protected static final byte[] r2 = Bytes.toBytes("r2");
69    protected static final byte[] f1 = Bytes.toBytes("f1");
70    protected static final byte[] f2 = Bytes.toBytes("f2");
71    protected static final TableName test = TableName.valueOf("test");
72    protected static final String slaveId = "1";
73    protected static FileSystem fs;
74    protected static Path oldLogDir;
75    protected static Path logDir;
76    protected static DummyServer server;
77  
78    @BeforeClass
79    public static void setUpBeforeClass() throws Exception {
80  
81      conf = HBaseConfiguration.create();
82      conf.set("replication.replicationsource.implementation",
83        ReplicationSourceDummy.class.getCanonicalName());
84      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
85      conf.setLong("replication.sleep.before.failover", 2000);
86      conf.setInt("replication.source.maxretriesmultiplier", 10);
87      utility = new HBaseTestingUtility(conf);
88      utility.startMiniZKCluster();
89  
90      zkw = new ZooKeeperWatcher(conf, "test", null);
91      ZKUtil.createWithParents(zkw, "/hbase/replication");
92      ZKUtil.createWithParents(zkw, "/hbase/replication/peers/1");
93      ZKUtil.setData(zkw, "/hbase/replication/peers/1", Bytes.toBytes(
94        conf.get(HConstants.ZOOKEEPER_QUORUM) + ":" + conf.get(HConstants.ZOOKEEPER_CLIENT_PORT)
95          + ":/1"));
96      ZKUtil.createWithParents(zkw, "/hbase/replication/peers/1/peer-state");
97      ZKUtil.setData(zkw, "/hbase/replication/peers/1/peer-state",
98        ReplicationStateZKBase.ENABLED_ZNODE_BYTES);
99      ZKUtil.createWithParents(zkw, "/hbase/replication/state");
100     ZKUtil.setData(zkw, "/hbase/replication/state", ReplicationStateZKBase.ENABLED_ZNODE_BYTES);
101 
102     ZKClusterId.setClusterId(zkw, new ClusterId());
103     FSUtils.setRootDir(utility.getConfiguration(), utility.getDataTestDir());
104     fs = FileSystem.get(conf);
105     oldLogDir = new Path(utility.getDataTestDir(), HConstants.HREGION_OLDLOGDIR_NAME);
106     logDir = new Path(utility.getDataTestDir(), HConstants.HREGION_LOGDIR_NAME);
107     server = new DummyServer(conf, "example.hostname.com", zkw);
108     replication = new Replication(server, fs, logDir, oldLogDir);
109     manager = replication.getReplicationManager();
110 
111     manager.addSource(slaveId);
112 
113     htd = new HTableDescriptor(test);
114     HColumnDescriptor col = new HColumnDescriptor(f1);
115     col.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
116     htd.addFamily(col);
117     col = new HColumnDescriptor(f2);
118     col.setScope(HConstants.REPLICATION_SCOPE_LOCAL);
119     htd.addFamily(col);
120 
121     hri = new HRegionInfo(htd.getTableName(), r1, r2);
122   }
123 
124   @AfterClass public static void tearDownAfterClass() throws Exception {
125     try {
126       manager.join();
127     } catch (RuntimeException re) {
128       if (re.getMessage().equals(fakeExceptionMessage)) {
129         LOG.info("It is fine");
130       }
131     }
132     utility.shutdownMiniCluster();
133   }
134 
135   @Rule public TestName testName = new TestName();
136 
137   private void cleanLogDir() throws IOException {
138     fs.delete(logDir, true);
139     fs.delete(oldLogDir, true);
140   }
141 
142   @Before public void setUp() throws Exception {
143     LOG.info("Start " + testName.getMethodName());
144     cleanLogDir();
145   }
146 
147   @After public void tearDown() throws Exception {
148     LOG.info("End " + testName.getMethodName());
149     cleanLogDir();
150   }
151 }