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;
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.TableName;
30  import org.apache.hadoop.hbase.client.Admin;
31  import org.apache.hadoop.hbase.client.Connection;
32  import org.apache.hadoop.hbase.client.ConnectionFactory;
33  import org.apache.hadoop.hbase.client.Table;
34  import org.apache.hadoop.hbase.client.replication.ReplicationAdmin;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
37  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
38  import org.junit.AfterClass;
39  import org.junit.BeforeClass;
40  
41  /**
42   * This class is only a base for other integration-level replication tests.
43   * Do not add tests here.
44   * TestReplicationSmallTests is where tests that don't require bring machines up/down should go
45   * All other tests should have their own classes and extend this one
46   */
47  public class TestReplicationBase {
48  /*
49    {
50      ((Log4JLogger) ReplicationSource.LOG).getLogger().setLevel(Level.ALL);
51    }*/
52  
53    private static final Log LOG = LogFactory.getLog(TestReplicationBase.class);
54  
55    protected static Configuration conf1 = HBaseConfiguration.create();
56    protected static Configuration conf2;
57    protected static Configuration CONF_WITH_LOCALFS;
58  
59    protected static ZooKeeperWatcher zkw1;
60    protected static ZooKeeperWatcher zkw2;
61  
62    protected static ReplicationAdmin admin;
63  
64    protected static Table htable1;
65    protected static Table htable2;
66  
67    protected static HBaseTestingUtility utility1;
68    protected static HBaseTestingUtility utility2;
69    protected static final String PEER_ID = "2";
70    protected static final int NB_ROWS_IN_BATCH = 100;
71    protected static final int NB_ROWS_IN_BIG_BATCH =
72        NB_ROWS_IN_BATCH * 10;
73    protected static final long SLEEP_TIME = 500;
74    protected static final int NB_RETRIES = 10;
75  
76    protected static final TableName tableName = TableName.valueOf("test");
77    protected static final byte[] famName = Bytes.toBytes("f");
78    protected static final byte[] row = Bytes.toBytes("row");
79    protected static final byte[] noRepfamName = Bytes.toBytes("norep");
80    protected static final String PEER_ID2 = "2";
81  
82    /**
83     * @throws java.lang.Exception
84     */
85    @BeforeClass
86    public static void setUpBeforeClass() throws Exception {
87      conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
88      // We don't want too many edits per batch sent to the ReplicationEndpoint to trigger
89      // sufficient number of events. But we don't want to go too low because
90      // HBaseInterClusterReplicationEndpoint partitions entries into batches and we want
91      // more than one batch sent to the peer cluster for better testing.
92      conf1.setInt("replication.source.size.capacity", 102400);
93      conf1.setLong("replication.source.sleepforretries", 100);
94      conf1.setInt("hbase.regionserver.maxlogs", 10);
95      conf1.setLong("hbase.master.logcleaner.ttl", 10);
96      conf1.setInt("zookeeper.recovery.retry", 1);
97      conf1.setInt("zookeeper.recovery.retry.intervalmill", 10);
98      conf1.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
99      conf1.setBoolean("dfs.support.append", true);
100     conf1.setLong(HConstants.THREAD_WAKE_FREQUENCY, 100);
101     conf1.setInt("replication.stats.thread.period.seconds", 5);
102     conf1.setBoolean("hbase.tests.use.shortcircuit.reads", false);
103     conf1.setLong("replication.sleep.before.failover", 2000);
104     conf1.setInt("replication.source.maxretriesmultiplier", 10);
105     conf1.setFloat("replication.source.ratio", 1.0f);
106     conf1.setBoolean("replication.source.eof.autorecovery", true);
107 
108     utility1 = new HBaseTestingUtility(conf1);
109     utility1.startMiniZKCluster();
110     MiniZooKeeperCluster miniZK = utility1.getZkCluster();
111     // Have to reget conf1 in case zk cluster location different
112     // than default
113     conf1 = utility1.getConfiguration();  
114     zkw1 = new ZooKeeperWatcher(conf1, "cluster1", null, true);
115     admin = new ReplicationAdmin(conf1);
116     LOG.info("Setup first Zk");
117 
118     // Base conf2 on conf1 so it gets the right zk cluster.
119     conf2 = HBaseConfiguration.create(conf1);
120     conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
121     conf2.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
122     conf2.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
123     conf2.setBoolean("dfs.support.append", true);
124     conf2.setBoolean("hbase.tests.use.shortcircuit.reads", false);
125 
126     utility2 = new HBaseTestingUtility(conf2);
127     utility2.setZkCluster(miniZK);
128     zkw2 = new ZooKeeperWatcher(conf2, "cluster2", null, true);
129 
130     ReplicationPeerConfig rpc = new ReplicationPeerConfig();
131     rpc.setClusterKey(utility2.getClusterKey());
132     admin.addPeer(PEER_ID, rpc);
133 
134     LOG.info("Setup second Zk");
135     CONF_WITH_LOCALFS = HBaseConfiguration.create(conf1);
136     utility1.startMiniCluster(2);
137     // Have a bunch of slave servers, because inter-cluster shipping logic uses number of sinks
138     // as a component in deciding maximum number of parallel batches to send to the peer cluster.
139     utility2.startMiniCluster(4);
140 
141     HTableDescriptor table = new HTableDescriptor(tableName);
142     HColumnDescriptor fam = new HColumnDescriptor(famName);
143     fam.setMaxVersions(100);
144     fam.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
145     table.addFamily(fam);
146     fam = new HColumnDescriptor(noRepfamName);
147     table.addFamily(fam);
148     Connection connection1 = utility1.getConnection();
149     Connection connection2 = utility2.getConnection();
150     try (Admin admin1 = connection1.getAdmin(); Admin admin2 = connection2.getAdmin()) {
151       admin1.createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
152       admin2.createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
153     }
154     utility1.waitUntilAllRegionsAssigned(tableName);
155     utility2.waitUntilAllRegionsAssigned(tableName);
156     htable1 = connection1.getTable(tableName);
157     htable1.setWriteBufferSize(1024);
158     htable2 = connection2.getTable(tableName);
159   }
160 
161   /**
162    * @throws java.lang.Exception
163    */
164   @AfterClass
165   public static void tearDownAfterClass() throws Exception {
166     htable2.close();
167     htable1.close();
168     admin.close();
169     utility2.shutdownMiniCluster();
170     utility1.shutdownMiniCluster();
171   }
172 }