1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
124 pair = MetaReader.getRegion(ct, Bytes.toBytes("nonexistent-region"));
125 assertNull(pair);
126
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
135 @Test public void testScanMetaForTable() throws IOException {
136 final String name = "testScanMetaForTable";
137 LOG.info("Started " + name);
138
139
140
141
142
143
144
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
152 byte[] greaterName = Bytes.toBytes("testScanMetaForTablf");
153 UTIL.createTable(greaterName, HConstants.CATALOG_FAMILY);
154
155
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 }