Skip to content

Commit 39c42c7

Browse files
committed
HBASE-26639 The implementation of TestMergesSplitsAddToTracker is problematic (apache#4010)
Signed-off-by: Wellington Ramos Chevreuil <[email protected]>
1 parent d25fa46 commit 39c42c7

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
*/
1818
package org.apache.hadoop.hbase.regionserver;
1919

20-
import static org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory.
21-
TRACKER_IMPL;
20+
import static org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory.TRACKER_IMPL;
2221
import static org.junit.Assert.assertTrue;
2322
import static org.junit.Assert.fail;
2423

2524
import java.io.IOException;
2625
import java.util.ArrayList;
27-
import java.util.HashMap;
2826
import java.util.List;
2927
import java.util.UUID;
3028
import java.util.concurrent.TimeUnit;
@@ -36,10 +34,14 @@
3634
import org.apache.hadoop.hbase.HBaseClassTestRule;
3735
import org.apache.hadoop.hbase.HBaseTestingUtility;
3836
import org.apache.hadoop.hbase.TableName;
37+
import org.apache.hadoop.hbase.TableNameTestRule;
38+
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
3939
import org.apache.hadoop.hbase.client.Put;
4040
import org.apache.hadoop.hbase.client.RegionInfo;
4141
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
4242
import org.apache.hadoop.hbase.client.Table;
43+
import org.apache.hadoop.hbase.client.TableDescriptor;
44+
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
4345
import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
4446
import org.apache.hadoop.hbase.regionserver.storefiletracker.TestStoreFileTracker;
4547
import org.apache.hadoop.hbase.testclassification.LargeTests;
@@ -54,7 +56,6 @@
5456
import org.junit.Rule;
5557
import org.junit.Test;
5658
import org.junit.experimental.categories.Category;
57-
import org.junit.rules.TestName;
5859

5960

6061
@Category({RegionServerTests.class, LargeTests.class})
@@ -66,14 +67,15 @@ public class TestMergesSplitsAddToTracker {
6667

6768
private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
6869

69-
public static final byte[] FAMILY_NAME = Bytes.toBytes("info");
70+
private static final String FAMILY_NAME_STR = "info";
71+
72+
private static final byte[] FAMILY_NAME = Bytes.toBytes(FAMILY_NAME_STR);
7073

7174
@Rule
72-
public TestName name = new TestName();
75+
public TableNameTestRule name = new TableNameTestRule();
7376

7477
@BeforeClass
7578
public static void setupClass() throws Exception {
76-
TEST_UTIL.getConfiguration().set(TRACKER_IMPL, TestStoreFileTracker.class.getName());
7779
TEST_UTIL.startMiniCluster();
7880
}
7981

@@ -84,13 +86,24 @@ public static void afterClass() throws Exception {
8486

8587
@Before
8688
public void setup(){
87-
TestStoreFileTracker.trackedFiles = new HashMap<>();
89+
TestStoreFileTracker.clear();
90+
}
91+
92+
private TableName createTable(byte[] splitKey) throws IOException {
93+
TableDescriptor td = TableDescriptorBuilder.newBuilder(name.getTableName())
94+
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_NAME))
95+
.setValue(TRACKER_IMPL, TestStoreFileTracker.class.getName()).build();
96+
if (splitKey != null) {
97+
TEST_UTIL.getAdmin().createTable(td, new byte[][] { splitKey });
98+
} else {
99+
TEST_UTIL.getAdmin().createTable(td);
100+
}
101+
return td.getTableName();
88102
}
89103

90104
@Test
91105
public void testCommitDaughterRegion() throws Exception {
92-
TableName table = TableName.valueOf(name.getMethodName());
93-
TEST_UTIL.createTable(table, FAMILY_NAME);
106+
TableName table = createTable(null);
94107
//first put some data in order to have a store file created
95108
putThreeRowsAndFlush(table);
96109
HRegion region = TEST_UTIL.getHBaseCluster().getRegions(table).get(0);
@@ -124,8 +137,7 @@ public void testCommitDaughterRegion() throws Exception {
124137

125138
@Test
126139
public void testCommitMergedRegion() throws Exception {
127-
TableName table = TableName.valueOf(name.getMethodName());
128-
TEST_UTIL.createTable(table, FAMILY_NAME);
140+
TableName table = createTable(null);
129141
//splitting the table first
130142
split(table, Bytes.toBytes("002"));
131143
//Add data and flush to create files in the two different regions
@@ -162,8 +174,7 @@ public void testCommitMergedRegion() throws Exception {
162174

163175
@Test
164176
public void testSplitLoadsFromTracker() throws Exception {
165-
TableName table = TableName.valueOf(name.getMethodName());
166-
TEST_UTIL.createTable(table, FAMILY_NAME);
177+
TableName table = createTable(null);
167178
//Add data and flush to create files in the two different regions
168179
putThreeRowsAndFlush(table);
169180
HRegion region = TEST_UTIL.getHBaseCluster().getRegions(table).get(0);
@@ -187,9 +198,7 @@ private void split(TableName table, byte[] splitKey) throws IOException {
187198

188199
@Test
189200
public void testMergeLoadsFromTracker() throws Exception {
190-
TableName table = TableName.valueOf(name.getMethodName());
191-
TEST_UTIL.createTable(table, new byte[][]{FAMILY_NAME},
192-
new byte[][]{Bytes.toBytes("002")});
201+
TableName table = createTable(Bytes.toBytes("002"));
193202
//Add data and flush to create files in the two different regions
194203
putThreeRowsAndFlush(table);
195204
List<HRegion> regions = TEST_UTIL.getHBaseCluster().getRegions(table);
@@ -237,10 +246,8 @@ private void validateDaughterRegionsFiles(HRegion region, String orignalFileName
237246
}
238247

239248
private void verifyFilesAreTracked(Path regionDir, FileSystem fs) throws Exception {
240-
String storeId = regionDir.getName() + "-info";
241-
for(FileStatus f : fs.listStatus(new Path(regionDir, Bytes.toString(FAMILY_NAME)))){
242-
assertTrue(TestStoreFileTracker.trackedFiles.get(storeId).stream().filter(s ->
243-
s.getPath().equals(f.getPath())).findFirst().isPresent());
249+
for (FileStatus f : fs.listStatus(new Path(regionDir, FAMILY_NAME_STR))) {
250+
assertTrue(TestStoreFileTracker.tracked(regionDir.getName(), FAMILY_NAME_STR, f.getPath()));
244251
}
245252
}
246253

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import java.io.IOException;
2121
import java.util.ArrayList;
2222
import java.util.Collection;
23-
import java.util.HashMap;
2423
import java.util.List;
25-
import java.util.Map;
26-
24+
import java.util.concurrent.BlockingQueue;
25+
import java.util.concurrent.ConcurrentHashMap;
26+
import java.util.concurrent.ConcurrentMap;
27+
import java.util.concurrent.LinkedBlockingQueue;
2728
import org.apache.hadoop.conf.Configuration;
29+
import org.apache.hadoop.fs.Path;
2830
import org.apache.hadoop.hbase.regionserver.StoreContext;
2931
import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
3032
import org.slf4j.Logger;
@@ -33,15 +35,16 @@
3335
public class TestStoreFileTracker extends DefaultStoreFileTracker {
3436

3537
private static final Logger LOG = LoggerFactory.getLogger(TestStoreFileTracker.class);
36-
public static Map<String, List<StoreFileInfo>> trackedFiles = new HashMap<>();
38+
private static ConcurrentMap<String, BlockingQueue<StoreFileInfo>> trackedFiles =
39+
new ConcurrentHashMap<>();
3740
private String storeId;
3841

3942
public TestStoreFileTracker(Configuration conf, boolean isPrimaryReplica, StoreContext ctx) {
4043
super(conf, isPrimaryReplica, ctx);
4144
if (ctx != null && ctx.getRegionFileSystem() != null) {
4245
this.storeId = ctx.getRegionInfo().getEncodedName() + "-" + ctx.getFamily().getNameAsString();
4346
LOG.info("created storeId: {}", storeId);
44-
trackedFiles.computeIfAbsent(storeId, v -> new ArrayList<>());
47+
trackedFiles.computeIfAbsent(storeId, v -> new LinkedBlockingQueue<>());
4548
} else {
4649
LOG.info("ctx.getRegionFileSystem() returned null. Leaving storeId null.");
4750
}
@@ -51,11 +54,19 @@ public TestStoreFileTracker(Configuration conf, boolean isPrimaryReplica, StoreC
5154
protected void doAddNewStoreFiles(Collection<StoreFileInfo> newFiles) throws IOException {
5255
LOG.info("adding to storeId: {}", storeId);
5356
trackedFiles.get(storeId).addAll(newFiles);
54-
trackedFiles.putIfAbsent(storeId, (List<StoreFileInfo>)newFiles);
5557
}
5658

5759
@Override
5860
public List<StoreFileInfo> load() throws IOException {
59-
return trackedFiles.get(storeId);
61+
return new ArrayList<>(trackedFiles.get(storeId));
62+
}
63+
64+
public static boolean tracked(String encodedRegionName, String family, Path file) {
65+
BlockingQueue<StoreFileInfo> files = trackedFiles.get(encodedRegionName + "-" + family);
66+
return files != null && files.stream().anyMatch(s -> s.getPath().equals(file));
67+
}
68+
69+
public static void clear() {
70+
trackedFiles.clear();
6071
}
6172
}

0 commit comments

Comments
 (0)