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;
21
22 import java.io.IOException;
23 import java.io.UnsupportedEncodingException;
24 import java.util.Random;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.client.Put;
29 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
30 import org.apache.hadoop.hbase.regionserver.HRegion;
31 import org.apache.hadoop.hbase.util.Bytes;
32
33
34 public abstract class AbstractMergeTestBase extends HBaseClusterTestCase {
35 static final Log LOG =
36 LogFactory.getLog(AbstractMergeTestBase.class.getName());
37 static final byte [] COLUMN_NAME = Bytes.toBytes("contents");
38 protected final Random rand = new Random();
39 protected HTableDescriptor desc;
40 protected ImmutableBytesWritable value;
41 protected boolean startMiniHBase;
42
43 public AbstractMergeTestBase() {
44 this(true);
45 }
46
47
48
49
50 public AbstractMergeTestBase(boolean startMiniHBase) {
51 super();
52
53 this.startMiniHBase = startMiniHBase;
54
55
56
57 String partialValue = String.valueOf(System.currentTimeMillis());
58 StringBuilder val = new StringBuilder();
59 while(val.length() < 1024) {
60 val.append(partialValue);
61 }
62
63 try {
64 value = new ImmutableBytesWritable(
65 val.toString().getBytes(HConstants.UTF8_ENCODING));
66 } catch (UnsupportedEncodingException e) {
67 fail();
68 }
69 desc = new HTableDescriptor(Bytes.toBytes("test"));
70 desc.addFamily(new HColumnDescriptor(COLUMN_NAME));
71 }
72
73 @Override
74 protected void hBaseClusterSetup() throws Exception {
75 if (startMiniHBase) {
76 super.hBaseClusterSetup();
77 }
78 }
79
80 @Override
81 public void preHBaseClusterSetup() throws Exception {
82 conf.setLong("hbase.hregion.max.filesize", 64L * 1024L * 1024L);
83
84
85
86
87
88
89
90
91 byte [] row_70001 = Bytes.toBytes("row_70001");
92 byte [] row_80001 = Bytes.toBytes("row_80001");
93
94
95
96
97
98
99
100
101 HRegion[] regions = {
102 createAregion(null, row_70001, 1, 70000),
103 createAregion(row_70001, row_80001, 70001, 10000),
104 createAregion(row_80001, null, 80001, 11000)
105 };
106
107
108
109
110 createRootAndMetaRegions();
111
112 for(int i = 0; i < regions.length; i++) {
113 HRegion.addRegionToMETA(meta, regions[i]);
114 }
115
116 closeRootAndMeta();
117 }
118
119 private HRegion createAregion(byte [] startKey, byte [] endKey, int firstRow,
120 int nrows) throws IOException {
121
122 HRegion region = createNewHRegion(desc, startKey, endKey);
123
124 System.out.println("created region " +
125 Bytes.toString(region.getRegionName()));
126
127 HRegionIncommon r = new HRegionIncommon(region);
128 for(int i = firstRow; i < firstRow + nrows; i++) {
129 Put put = new Put(Bytes.toBytes("row_"
130 + String.format("%1$05d", i)));
131 put.add(COLUMN_NAME, null, value.get());
132 region.put(put);
133 if(i % 10000 == 0) {
134 System.out.println("Flushing write #" + i);
135 r.flushcache();
136 }
137 }
138 region.close();
139 region.getLog().closeAndDelete();
140 region.getRegionInfo().setOffline(true);
141 return region;
142 }
143 }