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;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.HBaseConfiguration;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HColumnDescriptor;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.client.HBaseAdmin;
30  import org.apache.hadoop.hbase.client.HTable;
31  import org.apache.hadoop.hbase.client.replication.ReplicationAdmin;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
34  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
35  import org.junit.AfterClass;
36  import org.junit.BeforeClass;
37  
38  /**
39   * This class is only a base for other integration-level replication tests.
40   * Do not add tests here.
41   * TestReplicationSmallTests is where tests that don't require bring machines up/down should go
42   * All other tests should have their own classes and extend this one
43   */
44  public class TestReplicationBase {
45  
46    private static final Log LOG = LogFactory.getLog(TestReplicationBase.class);
47  
48    protected static Configuration conf1 = HBaseConfiguration.create();
49    protected static Configuration conf2;
50    protected static Configuration CONF_WITH_LOCALFS;
51  
52    protected static ZooKeeperWatcher zkw1;
53    protected static ZooKeeperWatcher zkw2;
54  
55    protected static ReplicationAdmin admin;
56  
57    protected static HTable htable1;
58    protected static HTable htable2;
59  
60    protected static HBaseTestingUtility utility1;
61    protected static HBaseTestingUtility utility2;
62    protected static final int NB_ROWS_IN_BATCH = 100;
63    protected static final int NB_ROWS_IN_BIG_BATCH =
64        NB_ROWS_IN_BATCH * 10;
65    protected static final long SLEEP_TIME = 1000;
66    protected static final int NB_RETRIES = 15;
67    protected static final int NB_RETRIES_FOR_BIG_BATCH = 30;
68  
69    protected static final byte[] tableName = Bytes.toBytes("test");
70    protected static final byte[] famName = Bytes.toBytes("f");
71    protected static final byte[] row = Bytes.toBytes("row");
72    protected static final byte[] noRepfamName = Bytes.toBytes("norep");
73  
74    /**
75     * @throws java.lang.Exception
76     */
77    @BeforeClass
78    public static void setUpBeforeClass() throws Exception {
79      conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
80      // smaller log roll size to trigger more events
81      conf1.setFloat("hbase.regionserver.logroll.multiplier", 0.0003f);
82      conf1.setInt("replication.source.size.capacity", 1024);
83      conf1.setLong("replication.source.sleepforretries", 100);
84      conf1.setInt("hbase.regionserver.maxlogs", 10);
85      conf1.setLong("hbase.master.logcleaner.ttl", 10);
86      conf1.setInt("zookeeper.recovery.retry", 1);
87      conf1.setInt("zookeeper.recovery.retry.intervalmill", 10);
88      conf1.setBoolean(HConstants.REPLICATION_ENABLE_KEY, true);
89      conf1.setBoolean("dfs.support.append", true);
90      conf1.setLong(HConstants.THREAD_WAKE_FREQUENCY, 100);
91      conf1.setInt("replication.stats.thread.period.seconds", 5);
92  
93      utility1 = new HBaseTestingUtility(conf1);
94      utility1.startMiniZKCluster();
95      MiniZooKeeperCluster miniZK = utility1.getZkCluster();
96      // Have to reget conf1 in case zk cluster location different
97      // than default
98      conf1 = utility1.getConfiguration();  
99      zkw1 = new ZooKeeperWatcher(conf1, "cluster1", null, true);
100     admin = new ReplicationAdmin(conf1);
101     LOG.info("Setup first Zk");
102 
103     // Base conf2 on conf1 so it gets the right zk cluster.
104     conf2 = HBaseConfiguration.create(conf1);
105     conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
106     conf2.setInt("hbase.client.retries.number", 6);
107     conf2.setBoolean(HConstants.REPLICATION_ENABLE_KEY, true);
108     conf2.setBoolean("dfs.support.append", true);
109 
110     utility2 = new HBaseTestingUtility(conf2);
111     utility2.setZkCluster(miniZK);
112     zkw2 = new ZooKeeperWatcher(conf2, "cluster2", null, true);
113 
114     admin.addPeer("2", utility2.getClusterKey());
115     setIsReplication(true);
116 
117     LOG.info("Setup second Zk");
118     CONF_WITH_LOCALFS = HBaseConfiguration.create(conf1);
119     utility1.startMiniCluster(2);
120     utility2.startMiniCluster(2);
121 
122     HTableDescriptor table = new HTableDescriptor(tableName);
123     HColumnDescriptor fam = new HColumnDescriptor(famName);
124     fam.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
125     table.addFamily(fam);
126     fam = new HColumnDescriptor(noRepfamName);
127     table.addFamily(fam);
128     HBaseAdmin admin1 = new HBaseAdmin(conf1);
129     HBaseAdmin admin2 = new HBaseAdmin(conf2);
130     admin1.createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
131     admin2.createTable(table);
132     htable1 = new HTable(conf1, tableName);
133     htable1.setWriteBufferSize(1024);
134     htable2 = new HTable(conf2, tableName);
135   }
136 
137   protected static void setIsReplication(boolean rep) throws Exception {
138     LOG.info("Set rep " + rep);
139     admin.setReplicating(rep);
140     Thread.sleep(SLEEP_TIME);
141   }
142 
143   /**
144    * @throws java.lang.Exception
145    */
146   @AfterClass
147   public static void tearDownAfterClass() throws Exception {
148     utility2.shutdownMiniCluster();
149     utility1.shutdownMiniCluster();
150   }
151 
152 
153 }
154