1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.client.Get;
24  import org.apache.hadoop.hbase.client.HTable;
25  import org.apache.hadoop.hbase.client.Put;
26  import org.apache.hadoop.hbase.client.Result;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  
32  /**
33   * Class that tests the multi-cluster in 1 JVM case, useful
34   * only for "unit'ish tests".
35   */
36  public class TestMultiClusters {
37  
38    private static final byte[] TABLE_NAME = Bytes.toBytes("test");
39    private static final byte[] FAM_NAME = Bytes.toBytes("fam");
40    private static final byte[] ROW = Bytes.toBytes("row");
41    private static final byte[] QUAL_NAME = Bytes.toBytes("qual");
42    private static final byte[] VALUE = Bytes.toBytes("value");
43  
44    /**
45     * Basic sanity test that spins up 2 HDFS and HBase clusters that share the
46     * same ZK ensemble. We then create the same table in both and make sure that
47     * what we insert in one place doesn't end up in the other.
48     * @throws Exception
49     */
50    @Test (timeout=100000)
51    public void twoClusters() throws Exception{
52      Configuration conf1 = HBaseConfiguration.create();
53      // Different path for different clusters
54      conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
55      HBaseTestingUtility utility1 = new HBaseTestingUtility(conf1);
56      utility1.startMiniZKCluster();
57  
58      Configuration conf2 = HBaseConfiguration.create();
59      conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
60      HBaseTestingUtility utility2 = new HBaseTestingUtility(conf2);
61      // They share the same ensemble, but homed differently
62      utility2.setZkCluster(utility1.getZkCluster());
63  
64      utility1.startMiniCluster();
65      utility2.startMiniCluster();
66  
67      HTable table1 = utility1.createTable(TABLE_NAME, FAM_NAME);
68      HTable table2 = utility2.createTable(TABLE_NAME, FAM_NAME);
69  
70      Put put = new Put(ROW);
71      put.add(FAM_NAME, QUAL_NAME, VALUE);
72      table1.put(put);
73  
74      Get get = new Get(ROW);
75      get.addColumn(FAM_NAME, QUAL_NAME);
76      Result res = table1.get(get);
77      assertEquals(1, res.size());
78  
79      res = table2.get(get);
80      assertEquals(0, res.size());
81    }
82  
83  }