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.catalog;
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.IOException;
25  import java.util.List;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.Abortable;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.HRegionInfo;
35  import org.apache.hadoop.hbase.HServerAddress;
36  import org.apache.hadoop.hbase.client.HBaseAdmin;
37  import org.apache.hadoop.hbase.client.HConnection;
38  import org.apache.hadoop.hbase.client.HConnectionManager;
39  import org.apache.hadoop.hbase.client.HTable;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.Pair;
42  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
43  import org.junit.AfterClass;
44  import org.junit.Before;
45  import org.junit.BeforeClass;
46  import org.junit.Test;
47  
48  /**
49   * Test {@link MetaReader}, {@link MetaEditor}, and {@link RootLocationEditor}.
50   */
51  public class TestMetaReaderEditor {
52    private static final Log LOG = LogFactory.getLog(TestMetaReaderEditor.class);
53    private static final  HBaseTestingUtility UTIL = new HBaseTestingUtility();
54    private ZooKeeperWatcher zkw;
55    private CatalogTracker ct;
56    private final static Abortable ABORTABLE = new Abortable() {
57      private final AtomicBoolean abort = new AtomicBoolean(false);
58  
59      @Override
60      public void abort(String why, Throwable e) {
61        LOG.info(why, e);
62        abort.set(true);
63      }
64    };
65  
66    @BeforeClass public static void beforeClass() throws Exception {
67      UTIL.startMiniCluster();
68    }
69  
70    @Before public void setup() throws IOException, InterruptedException {
71      Configuration c = new Configuration(UTIL.getConfiguration());
72      zkw = new ZooKeeperWatcher(c, "TestMetaReaderEditor", ABORTABLE);
73      HConnection connection = HConnectionManager.getConnection(c);
74      ct = new CatalogTracker(zkw, connection, ABORTABLE);
75      ct.start();
76    }
77  
78    @AfterClass public static void afterClass() throws IOException {
79      UTIL.shutdownMiniCluster();
80    }
81  
82    @Test public void testGetRegionsCatalogTables()
83    throws IOException, InterruptedException {
84      List<HRegionInfo> regions =
85        MetaReader.getTableRegions(ct, HConstants.META_TABLE_NAME);
86      assertTrue(regions.size() >= 1);
87      assertTrue(MetaReader.getTableRegionsAndLocations(ct,
88        Bytes.toString(HConstants.META_TABLE_NAME)).size() >= 1);
89      assertTrue(MetaReader.getTableRegionsAndLocations(ct,
90        Bytes.toString(HConstants.ROOT_TABLE_NAME)).size() == 1);
91    }
92  
93    @Test public void testTableExists() throws IOException {
94      final String name = "testTableExists";
95      final byte [] nameBytes = Bytes.toBytes(name);
96      assertFalse(MetaReader.tableExists(ct, name));
97      UTIL.createTable(nameBytes, HConstants.CATALOG_FAMILY);
98      assertTrue(MetaReader.tableExists(ct, name));
99      HBaseAdmin admin = UTIL.getHBaseAdmin();
100     admin.disableTable(name);
101     admin.deleteTable(name);
102     assertFalse(MetaReader.tableExists(ct, name));
103     assertTrue(MetaReader.tableExists(ct,
104       Bytes.toString(HConstants.META_TABLE_NAME)));
105     assertTrue(MetaReader.tableExists(ct,
106       Bytes.toString(HConstants.ROOT_TABLE_NAME)));
107   }
108 
109   @Test public void testGetRegion() throws IOException, InterruptedException {
110     final String name = "testGetRegion";
111     LOG.info("Started " + name);
112     final byte [] nameBytes = Bytes.toBytes(name);
113     HTable t = UTIL.createTable(nameBytes, HConstants.CATALOG_FAMILY);
114     int regionCount = UTIL.createMultiRegions(t, HConstants.CATALOG_FAMILY);
115 
116     // Test it works getting a region from user table.
117     List<HRegionInfo> regions = MetaReader.getTableRegions(ct, nameBytes);
118     assertEquals(regionCount, regions.size());
119     Pair<HRegionInfo, HServerAddress> pair =
120       MetaReader.getRegion(ct, regions.get(0).getRegionName());
121     assertEquals(regions.get(0).getEncodedName(),
122       pair.getFirst().getEncodedName());
123     // Test get on non-existent region.
124     pair = MetaReader.getRegion(ct, Bytes.toBytes("nonexistent-region"));
125     assertNull(pair);
126     // Test it works getting a region from meta/root.
127     pair =
128       MetaReader.getRegion(ct, HRegionInfo.FIRST_META_REGIONINFO.getRegionName());
129     assertEquals(HRegionInfo.FIRST_META_REGIONINFO.getEncodedName(),
130       pair.getFirst().getEncodedName());
131     LOG.info("Finished " + name);
132   }
133 
134   // Test for the optimization made in HBASE-3650
135   @Test public void testScanMetaForTable() throws IOException {
136     final String name = "testScanMetaForTable";
137     LOG.info("Started " + name);
138 
139     /** Create 5 tables
140      - testScanMetaForTable
141      - testScanMetaForTable0
142      - testScanMetaForTable1
143      - testScanMetaForTable2
144      - testScanMetaForTablf
145     **/
146 
147     UTIL.createTable(Bytes.toBytes(name), HConstants.CATALOG_FAMILY);
148     for (int i = 3; i < 3; i ++) {
149       UTIL.createTable(Bytes.toBytes(name+i), HConstants.CATALOG_FAMILY);
150     }
151     // name that is +1 greater than the first one (e+1=f)
152     byte[] greaterName = Bytes.toBytes("testScanMetaForTablf");
153     UTIL.createTable(greaterName, HConstants.CATALOG_FAMILY);
154 
155     // Now make sure we only get the regions from 1 of the tables at a time
156 
157     assertEquals(1, MetaReader.getTableRegions(ct, Bytes.toBytes(name)).size());
158     for (int i = 3; i < 3; i ++) {
159       assertEquals(1, MetaReader.getTableRegions(ct, Bytes.toBytes(name+i)).size());
160     }
161     assertEquals(1, MetaReader.getTableRegions(ct, greaterName).size());
162   }
163 }