Skip to content

Commit 12002e6

Browse files
committed
HBASE-26724 Backport the UT changes in HBASE-24510 to branch-2.x (#4081)
Signed-off-by: Xin Sun <[email protected]>
1 parent 4e15101 commit 12002e6

File tree

11 files changed

+525
-442
lines changed

11 files changed

+525
-442
lines changed

hbase-server/src/test/java/org/apache/hadoop/hbase/HTestConst.java

Lines changed: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616
*/
1717
package org.apache.hadoop.hbase;
1818

19+
import java.io.IOException;
1920
import java.util.Arrays;
2021
import java.util.HashSet;
2122
import java.util.Set;
2223
import java.util.Collections;
23-
24+
import org.apache.hadoop.hbase.client.Durability;
25+
import org.apache.hadoop.hbase.client.Put;
26+
import org.apache.hadoop.hbase.client.Table;
27+
import org.apache.hadoop.hbase.regionserver.Region;
28+
import org.apache.hadoop.hbase.regionserver.RegionAsTable;
2429
import org.apache.hadoop.hbase.util.Bytes;
2530

2631
/**
27-
* Similar to {@link HConstants} but for tests. Also provides some simple
28-
* static utility functions to generate test data.
32+
* Similar to {@link HConstants} but for tests. Also provides some simple static utility functions
33+
* to generate test data.
2934
*/
3035
public class HTestConst {
3136

@@ -34,15 +39,13 @@ private HTestConst() {
3439

3540
public static final String DEFAULT_TABLE_STR = "MyTestTable";
3641
public static final byte[] DEFAULT_TABLE_BYTES = Bytes.toBytes(DEFAULT_TABLE_STR);
37-
public static final TableName DEFAULT_TABLE =
38-
TableName.valueOf(DEFAULT_TABLE_BYTES);
42+
public static final TableName DEFAULT_TABLE = TableName.valueOf(DEFAULT_TABLE_BYTES);
3943

4044
public static final String DEFAULT_CF_STR = "MyDefaultCF";
4145
public static final byte[] DEFAULT_CF_BYTES = Bytes.toBytes(DEFAULT_CF_STR);
4246

4347
public static final Set<String> DEFAULT_CF_STR_SET =
44-
Collections.unmodifiableSet(new HashSet<>(
45-
Arrays.asList(new String[] { DEFAULT_CF_STR })));
48+
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(new String[] { DEFAULT_CF_STR })));
4649

4750
public static final String DEFAULT_ROW_STR = "MyTestRow";
4851
public static final byte[] DEFAULT_ROW_BYTES = Bytes.toBytes(DEFAULT_ROW_STR);
@@ -53,17 +56,129 @@ private HTestConst() {
5356
public static String DEFAULT_VALUE_STR = "MyTestValue";
5457
public static byte[] DEFAULT_VALUE_BYTES = Bytes.toBytes(DEFAULT_VALUE_STR);
5558

59+
private static final char FIRST_CHAR = 'a';
60+
private static final char LAST_CHAR = 'z';
61+
private static final byte[] START_KEY_BYTES = { FIRST_CHAR, FIRST_CHAR, FIRST_CHAR };
62+
5663
/**
57-
* Generate the given number of unique byte sequences by appending numeric
58-
* suffixes (ASCII representations of decimal numbers).
64+
* Generate the given number of unique byte sequences by appending numeric suffixes (ASCII
65+
* representations of decimal numbers).
5966
*/
6067
public static byte[][] makeNAscii(byte[] base, int n) {
61-
byte [][] ret = new byte[n][];
68+
byte[][] ret = new byte[n][];
6269
for (int i = 0; i < n; i++) {
6370
byte[] tail = Bytes.toBytes(Integer.toString(i));
6471
ret[i] = Bytes.add(base, tail);
6572
}
6673
return ret;
6774
}
6875

76+
/**
77+
* Add content to region <code>r</code> on the passed column <code>column</code>. Adds data of the
78+
* from 'aaa', 'aab', etc where key and value are the same.
79+
* @return count of what we added.
80+
*/
81+
public static long addContent(final Region r, final byte[] columnFamily, final byte[] column)
82+
throws IOException {
83+
byte[] startKey = r.getRegionInfo().getStartKey();
84+
byte[] endKey = r.getRegionInfo().getEndKey();
85+
byte[] startKeyBytes = startKey;
86+
if (startKeyBytes == null || startKeyBytes.length == 0) {
87+
startKeyBytes = START_KEY_BYTES;
88+
}
89+
return addContent(new RegionAsTable(r), Bytes.toString(columnFamily), Bytes.toString(column),
90+
startKeyBytes, endKey, -1);
91+
}
92+
93+
public static long addContent(final Region r, final byte[] columnFamily) throws IOException {
94+
return addContent(r, columnFamily, null);
95+
}
96+
97+
/**
98+
* Add content to region <code>r</code> on the passed column <code>column</code>. Adds data of the
99+
* from 'aaa', 'aab', etc where key and value are the same.
100+
* @return count of what we added.
101+
*/
102+
public static long addContent(Table updater, String columnFamily) throws IOException {
103+
return addContent(updater, columnFamily, START_KEY_BYTES, null);
104+
}
105+
106+
public static long addContent(Table updater, String family, String column) throws IOException {
107+
return addContent(updater, family, column, START_KEY_BYTES, null);
108+
}
109+
110+
/**
111+
* Add content to region <code>r</code> on the passed column <code>column</code>. Adds data of the
112+
* from 'aaa', 'aab', etc where key and value are the same.
113+
* @return count of what we added.
114+
*/
115+
public static long addContent(Table updater, String columnFamily, byte[] startKeyBytes,
116+
byte[] endKey) throws IOException {
117+
return addContent(updater, columnFamily, null, startKeyBytes, endKey, -1);
118+
}
119+
120+
public static long addContent(Table updater, String family, String column, byte[] startKeyBytes,
121+
byte[] endKey) throws IOException {
122+
return addContent(updater, family, column, startKeyBytes, endKey, -1);
123+
}
124+
125+
/**
126+
* Add content to region <code>r</code> on the passed column <code>column</code>. Adds data of the
127+
* from 'aaa', 'aab', etc where key and value are the same.
128+
* @return count of what we added.
129+
*/
130+
public static long addContent(Table updater, String columnFamily, String column,
131+
byte[] startKeyBytes, byte[] endKey, long ts) throws IOException {
132+
long count = 0;
133+
// Add rows of three characters. The first character starts with the
134+
// 'a' character and runs up to 'z'. Per first character, we run the
135+
// second character over same range. And same for the third so rows
136+
// (and values) look like this: 'aaa', 'aab', 'aac', etc.
137+
char secondCharStart = (char) startKeyBytes[1];
138+
char thirdCharStart = (char) startKeyBytes[2];
139+
EXIT: for (char c = (char) startKeyBytes[0]; c <= LAST_CHAR; c++) {
140+
for (char d = secondCharStart; d <= LAST_CHAR; d++) {
141+
for (char e = thirdCharStart; e <= LAST_CHAR; e++) {
142+
byte[] t = new byte[] { (byte) c, (byte) d, (byte) e };
143+
if (endKey != null && endKey.length > 0 && Bytes.compareTo(endKey, t) <= 0) {
144+
break EXIT;
145+
}
146+
Put put;
147+
if (ts != -1) {
148+
put = new Put(t, ts);
149+
} else {
150+
put = new Put(t);
151+
}
152+
StringBuilder sb = new StringBuilder();
153+
if (column != null && column.contains(":")) {
154+
sb.append(column);
155+
} else {
156+
if (columnFamily != null) {
157+
sb.append(columnFamily);
158+
if (!columnFamily.endsWith(":")) {
159+
sb.append(":");
160+
}
161+
if (column != null) {
162+
sb.append(column);
163+
}
164+
}
165+
}
166+
byte[][] split = CellUtil.parseColumn(Bytes.toBytes(sb.toString()));
167+
if (split.length == 1) {
168+
byte[] qualifier = new byte[0];
169+
put.addColumn(split[0], qualifier, t);
170+
} else {
171+
put.addColumn(split[0], split[1], t);
172+
}
173+
put.setDurability(Durability.SKIP_WAL);
174+
updater.put(put);
175+
count++;
176+
}
177+
// Set start character back to FIRST_CHAR after we've done first loop.
178+
thirdCharStart = FIRST_CHAR;
179+
}
180+
secondCharStart = FIRST_CHAR;
181+
}
182+
return count;
183+
}
69184
}

hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
*/
1818
package org.apache.hadoop.hbase.client;
1919

20-
import static org.apache.hadoop.hbase.HBaseTestCase.assertByteEquals;
2120
import static org.hamcrest.MatcherAssert.assertThat;
2221
import static org.hamcrest.Matchers.greaterThan;
2322
import static org.hamcrest.Matchers.is;
2423
import static org.hamcrest.Matchers.lessThan;
24+
import static org.junit.Assert.assertArrayEquals;
2525

2626
import java.io.IOException;
2727
import java.nio.ByteBuffer;
@@ -168,7 +168,7 @@ public void testBasicGetValue() throws Exception {
168168
for (int i = 0; i < 100; ++i) {
169169
final byte[] qf = Bytes.toBytes(i);
170170

171-
assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), r.getValue(family, qf));
171+
assertArrayEquals(Bytes.add(value, Bytes.toBytes(i)), r.getValue(family, qf));
172172
assertTrue(r.containsColumn(family, qf));
173173
}
174174
}
@@ -187,7 +187,7 @@ public void testMultiVersionGetValue() throws Exception {
187187
for (int i = 0; i < 100; ++i) {
188188
final byte[] qf = Bytes.toBytes(i);
189189

190-
assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), r.getValue(family, qf));
190+
assertArrayEquals(Bytes.add(value, Bytes.toBytes(i)), r.getValue(family, qf));
191191
assertTrue(r.containsColumn(family, qf));
192192
}
193193
}

hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestCoprocessorInterface.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
import org.apache.hadoop.hbase.Coprocessor;
3939
import org.apache.hadoop.hbase.CoprocessorEnvironment;
4040
import org.apache.hadoop.hbase.HBaseClassTestRule;
41-
import org.apache.hadoop.hbase.HBaseTestCase;
4241
import org.apache.hadoop.hbase.HBaseTestingUtility;
4342
import org.apache.hadoop.hbase.HColumnDescriptor;
4443
import org.apache.hadoop.hbase.HConstants;
4544
import org.apache.hadoop.hbase.HTableDescriptor;
45+
import org.apache.hadoop.hbase.HTestConst;
4646
import org.apache.hadoop.hbase.TableName;
4747
import org.apache.hadoop.hbase.client.Get;
4848
import org.apache.hadoop.hbase.client.RegionInfo;
@@ -292,7 +292,7 @@ public void testSharedData() throws IOException {
292292
HRegion region = initHRegion(tableName, name.getMethodName(), hc, new Class<?>[]{}, families);
293293

294294
for (int i = 0; i < 3; i++) {
295-
HBaseTestCase.addContent(region, fam3);
295+
HTestConst.addContent(region, fam3);
296296
region.flush(true);
297297
}
298298

@@ -354,7 +354,7 @@ public void testCoprocessorInterface() throws IOException {
354354
HRegion region = initHRegion(tableName, name.getMethodName(), hc,
355355
new Class<?>[]{CoprocessorImpl.class}, families);
356356
for (int i = 0; i < 3; i++) {
357-
HBaseTestCase.addContent(region, fam3);
357+
HTestConst.addContent(region, fam3);
358358
region.flush(true);
359359
}
360360

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestBlocksScanned.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
*/
1818
package org.apache.hadoop.hbase.regionserver;
1919

20-
import static org.apache.hadoop.hbase.HBaseTestCase.addContent;
20+
import static org.apache.hadoop.hbase.HTestConst.addContent;
2121
import static org.junit.Assert.assertEquals;
2222

2323
import java.util.ArrayList;
2424
import java.util.List;
25-
2625
import org.apache.hadoop.conf.Configuration;
2726
import org.apache.hadoop.fs.Path;
2827
import org.apache.hadoop.hbase.Cell;

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompaction.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static org.junit.Assert.assertThrows;
2929
import static org.junit.Assert.assertTrue;
3030
import static org.junit.Assert.fail;
31-
import static org.mockito.Matchers.any;
31+
import static org.mockito.ArgumentMatchers.any;
3232
import static org.mockito.Mockito.doAnswer;
3333
import static org.mockito.Mockito.mock;
3434
import static org.mockito.Mockito.spy;
@@ -50,11 +50,11 @@
5050
import org.apache.hadoop.hbase.ChoreService;
5151
import org.apache.hadoop.hbase.HBaseClassTestRule;
5252
import org.apache.hadoop.hbase.HBaseConfiguration;
53-
import org.apache.hadoop.hbase.HBaseTestCase;
5453
import org.apache.hadoop.hbase.HBaseTestingUtility;
5554
import org.apache.hadoop.hbase.HColumnDescriptor;
5655
import org.apache.hadoop.hbase.HConstants;
5756
import org.apache.hadoop.hbase.HTableDescriptor;
57+
import org.apache.hadoop.hbase.HTestConst;
5858
import org.apache.hadoop.hbase.Waiter;
5959
import org.apache.hadoop.hbase.client.Delete;
6060
import org.apache.hadoop.hbase.client.Durability;
@@ -175,7 +175,7 @@ public void testInterruptCompactionBySize() throws Exception {
175175
for (int j = 0; j < jmax; j++) {
176176
p.addColumn(COLUMN_FAMILY, Bytes.toBytes(j), pad);
177177
}
178-
HBaseTestCase.addContent(loader, Bytes.toString(COLUMN_FAMILY));
178+
HTestConst.addContent(loader, Bytes.toString(COLUMN_FAMILY));
179179
loader.put(p);
180180
r.flush(true);
181181
}
@@ -251,7 +251,7 @@ public void testInterruptCompactionByTime() throws Exception {
251251
for (int j = 0; j < jmax; j++) {
252252
p.addColumn(COLUMN_FAMILY, Bytes.toBytes(j), pad);
253253
}
254-
HBaseTestCase.addContent(loader, Bytes.toString(COLUMN_FAMILY));
254+
HTestConst.addContent(loader, Bytes.toString(COLUMN_FAMILY));
255255
loader.put(p);
256256
r.flush(true);
257257
}
@@ -330,7 +330,7 @@ private void createStoreFile(final HRegion region) throws IOException {
330330

331331
private void createStoreFile(final HRegion region, String family) throws IOException {
332332
Table loader = new RegionAsTable(region);
333-
HBaseTestCase.addContent(loader, family);
333+
HTestConst.addContent(loader, family);
334334
region.flush(true);
335335
}
336336

@@ -494,7 +494,7 @@ public void testStopStartCompaction() throws IOException {
494494
for (int j = 0; j < jmax; j++) {
495495
p.addColumn(COLUMN_FAMILY, Bytes.toBytes(j), pad);
496496
}
497-
HBaseTestCase.addContent(loader, Bytes.toString(COLUMN_FAMILY));
497+
HTestConst.addContent(loader, Bytes.toString(COLUMN_FAMILY));
498498
loader.put(p);
499499
r.flush(true);
500500
}

0 commit comments

Comments
 (0)