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.util;
21
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.*;
33 import org.apache.hadoop.hbase.catalog.CatalogTracker;
34 import org.apache.hadoop.hbase.catalog.MetaReader;
35 import org.apache.hadoop.hbase.client.HBaseAdmin;
36 import org.apache.hadoop.hbase.client.Put;
37 import org.apache.hadoop.hbase.regionserver.HRegion;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41
42
43
44 @Category(MediumTests.class)
45 public class TestMergeTable {
46 private static final Log LOG = LogFactory.getLog(TestMergeTable.class);
47 private final HBaseTestingUtility UTIL = new HBaseTestingUtility();
48 private static final byte [] COLUMN_NAME = Bytes.toBytes("contents");
49 private static final byte [] VALUE;
50 static {
51
52 String partialValue = String.valueOf(System.currentTimeMillis());
53 StringBuilder val = new StringBuilder();
54 while (val.length() < 1024) {
55 val.append(partialValue);
56 }
57 VALUE = Bytes.toBytes(val.toString());
58 }
59
60
61
62
63
64
65
66
67 @Test (timeout=300000) public void testMergeTable() throws Exception {
68
69 HTableDescriptor desc = new HTableDescriptor(Bytes.toBytes("test"));
70 desc.addFamily(new HColumnDescriptor(COLUMN_NAME));
71
72
73 UTIL.getConfiguration().setLong(HConstants.HREGION_MAX_FILESIZE, 64L * 1024L * 1024L);
74
75 UTIL.getConfiguration().setInt("hbase.regionserver.regionSplitLimit", 0);
76
77 UTIL.startMiniDFSCluster(1);
78
79 Path rootdir = UTIL.createRootDir();
80 FileSystem fs = FileSystem.get(UTIL.getConfiguration());
81 if (fs.exists(rootdir)) {
82 if (fs.delete(rootdir, true)) {
83 LOG.info("Cleaned up existing " + rootdir);
84 }
85 }
86
87
88
89
90
91
92
93 byte [] row_70001 = Bytes.toBytes("row_70001");
94 byte [] row_80001 = Bytes.toBytes("row_80001");
95
96
97
98 FSTableDescriptors.createTableDescriptor(fs, rootdir, desc);
99 HRegion [] regions = {
100 createRegion(desc, null, row_70001, 1, 70000, rootdir),
101 createRegion(desc, row_70001, row_80001, 70001, 10000, rootdir),
102 createRegion(desc, row_80001, null, 80001, 11000, rootdir)
103 };
104
105
106
107 setupROOTAndMeta(rootdir, regions);
108 try {
109 LOG.info("Starting mini zk cluster");
110 UTIL.startMiniZKCluster();
111 LOG.info("Starting mini hbase cluster");
112 UTIL.startMiniHBaseCluster(1, 1);
113 Configuration c = new Configuration(UTIL.getConfiguration());
114 CatalogTracker ct = new CatalogTracker(c);
115 ct.start();
116 List<HRegionInfo> originalTableRegions =
117 MetaReader.getTableRegions(ct, desc.getName());
118 LOG.info("originalTableRegions size=" + originalTableRegions.size() +
119 "; " + originalTableRegions);
120 HBaseAdmin admin = new HBaseAdmin(new Configuration(c));
121 admin.disableTable(desc.getName());
122 HMerge.merge(c, FileSystem.get(c), desc.getName());
123 List<HRegionInfo> postMergeTableRegions =
124 MetaReader.getTableRegions(ct, desc.getName());
125 LOG.info("postMergeTableRegions size=" + postMergeTableRegions.size() +
126 "; " + postMergeTableRegions);
127 assertTrue("originalTableRegions=" + originalTableRegions.size() +
128 ", postMergeTableRegions=" + postMergeTableRegions.size(),
129 postMergeTableRegions.size() < originalTableRegions.size());
130 LOG.info("Done with merge");
131 } finally {
132 UTIL.shutdownMiniCluster();
133 LOG.info("After cluster shutdown");
134 }
135 }
136
137 private HRegion createRegion(final HTableDescriptor desc,
138 byte [] startKey, byte [] endKey, int firstRow, int nrows, Path rootdir)
139 throws IOException {
140 HRegionInfo hri = new HRegionInfo(desc.getName(), startKey, endKey);
141 HRegion region = HRegion.createHRegion(hri, rootdir, UTIL.getConfiguration(), desc);
142 LOG.info("Created region " + region.getRegionNameAsString());
143 for(int i = firstRow; i < firstRow + nrows; i++) {
144 Put put = new Put(Bytes.toBytes("row_" + String.format("%1$05d", i)));
145 put.setWriteToWAL(false);
146 put.add(COLUMN_NAME, null, VALUE);
147 region.put(put);
148 if (i % 10000 == 0) {
149 LOG.info("Flushing write #" + i);
150 region.flushcache();
151 }
152 }
153 region.close();
154 region.getLog().closeAndDelete();
155 return region;
156 }
157
158 protected void setupROOTAndMeta(Path rootdir, final HRegion [] regions)
159 throws IOException {
160 HRegion root =
161 HRegion.createHRegion(HRegionInfo.ROOT_REGIONINFO, rootdir,
162 UTIL.getConfiguration(), HTableDescriptor.ROOT_TABLEDESC);
163 HRegion meta =
164 HRegion.createHRegion(HRegionInfo.FIRST_META_REGIONINFO, rootdir,
165 UTIL.getConfiguration(), HTableDescriptor.META_TABLEDESC);
166 HRegion.addRegionToMETA(root, meta);
167 for (HRegion r: regions) {
168 HRegion.addRegionToMETA(meta, r);
169 }
170 meta.close();
171 meta.getLog().closeAndDelete();
172 root.close();
173 root.getLog().closeAndDelete();
174 }
175
176 @org.junit.Rule
177 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
178 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
179 }
180