Skip to content

Commit a216807

Browse files
committed
HBASE-26224 Introduce a MigrationStoreFileTracker to support migrating from different store file tracker implementations
1 parent 488c21e commit a216807

File tree

6 files changed

+319
-6
lines changed

6 files changed

+319
-6
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/storefiletracker/DefaultStoreFileTracker.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.util.Collection;
22+
import java.util.Collections;
2223
import java.util.List;
2324
import org.apache.hadoop.conf.Configuration;
2425
import org.apache.hadoop.hbase.regionserver.StoreContext;
@@ -32,14 +33,15 @@
3233
@InterfaceAudience.Private
3334
class DefaultStoreFileTracker extends StoreFileTrackerBase {
3435

35-
public DefaultStoreFileTracker(Configuration conf, boolean isPrimaryReplica,
36-
StoreContext ctx) {
36+
public DefaultStoreFileTracker(Configuration conf, boolean isPrimaryReplica, StoreContext ctx) {
3737
super(conf, isPrimaryReplica, ctx);
3838
}
3939

4040
@Override
4141
public List<StoreFileInfo> load() throws IOException {
42-
return ctx.getRegionFileSystem().getStoreFiles(ctx.getFamily().getNameAsString());
42+
List<StoreFileInfo> files =
43+
ctx.getRegionFileSystem().getStoreFiles(ctx.getFamily().getNameAsString());
44+
return files != null ? files : Collections.emptyList();
4345
}
4446

4547
@Override
@@ -57,4 +59,9 @@ protected void doAddCompactionResults(Collection<StoreFileInfo> compactedFiles,
5759
Collection<StoreFileInfo> newFiles) throws IOException {
5860
// NOOP
5961
}
62+
63+
@Override
64+
void set(List<StoreFileInfo> files) {
65+
// NOOP
66+
}
6067
}

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/storefiletracker/FileBasedStoreFileTracker.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* storages.
4949
*/
5050
@InterfaceAudience.Private
51-
public class FileBasedStoreFileTracker extends StoreFileTrackerBase {
51+
class FileBasedStoreFileTracker extends StoreFileTrackerBase {
5252

5353
private final StoreFileListFile backedFile;
5454

@@ -139,4 +139,17 @@ protected void doAddCompactionResults(Collection<StoreFileInfo> compactedFiles,
139139
}
140140
}
141141
}
142+
143+
@Override
144+
void set(List<StoreFileInfo> files) throws IOException {
145+
synchronized (storefiles) {
146+
storefiles.clear();
147+
StoreFileList.Builder builder = StoreFileList.newBuilder();
148+
for (StoreFileInfo info : files) {
149+
storefiles.put(info.getPath().getName(), info);
150+
builder.addStoreFile(toStoreFileEntry(info));
151+
}
152+
backedFile.update(builder);
153+
}
154+
}
142155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.regionserver.storefiletracker;
19+
20+
import java.io.IOException;
21+
import java.util.Collection;
22+
import java.util.List;
23+
import org.apache.hadoop.conf.Configuration;
24+
import org.apache.hadoop.hbase.regionserver.StoreContext;
25+
import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
26+
import org.apache.yetus.audience.InterfaceAudience;
27+
28+
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
29+
30+
/**
31+
* A store file tracker used for migrating between store file tracker implementations.
32+
*/
33+
@InterfaceAudience.Private
34+
class MigrationStoreFileTracker extends StoreFileTrackerBase {
35+
36+
public static final String SRC_IMPL = "hbase.store.file-tracker.migration.src.impl";
37+
38+
public static final String DST_IMPL = "hbase.store.file-tracker.migration.dst.impl";
39+
40+
private final StoreFileTrackerBase src;
41+
42+
private final StoreFileTrackerBase dst;
43+
44+
public MigrationStoreFileTracker(Configuration conf, boolean isPrimaryReplica, StoreContext ctx) {
45+
super(conf, isPrimaryReplica, ctx);
46+
this.src = StoreFileTrackerFactory.create(conf, SRC_IMPL, isPrimaryReplica, ctx);
47+
this.dst = StoreFileTrackerFactory.create(conf, DST_IMPL, isPrimaryReplica, ctx);
48+
Preconditions.checkArgument(!src.getClass().equals(dst.getClass()),
49+
"src and dst is the same: %s", src.getClass());
50+
}
51+
52+
@Override
53+
public List<StoreFileInfo> load() throws IOException {
54+
List<StoreFileInfo> files = src.load();
55+
// we must call load to initialize the store file tracker, but we do not care the result, just
56+
// ignore.
57+
dst.load();
58+
dst.set(files);
59+
return files;
60+
}
61+
62+
@Override
63+
protected boolean requireWritingToTmpDirFirst() {
64+
// Returns true if either of the two StoreFileTracker returns true.
65+
// For example, if we want to migrate from a tracker implementation which can ignore the broken
66+
// files under data directory to a tracker implementation which can not, if we still allow
67+
// writing in tmp directory directly, we may have some broken files under the data directory and
68+
// then after we finally change the implementation which can not ignore the broken files, we
69+
// will be in trouble.
70+
return src.requireWritingToTmpDirFirst() || dst.requireWritingToTmpDirFirst();
71+
}
72+
73+
@Override
74+
protected void doAddNewStoreFiles(Collection<StoreFileInfo> newFiles) throws IOException {
75+
src.doAddNewStoreFiles(newFiles);
76+
dst.doAddNewStoreFiles(newFiles);
77+
}
78+
79+
@Override
80+
protected void doAddCompactionResults(Collection<StoreFileInfo> compactedFiles,
81+
Collection<StoreFileInfo> newFiles) throws IOException {
82+
src.doAddCompactionResults(compactedFiles, newFiles);
83+
dst.doAddCompactionResults(compactedFiles, newFiles);
84+
}
85+
86+
@Override
87+
void set(List<StoreFileInfo> files) {
88+
throw new UnsupportedOperationException(
89+
"Should not call this method on " + getClass().getSimpleName());
90+
}
91+
}

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/storefiletracker/StoreFileTrackerBase.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.util.Collection;
22+
import java.util.List;
2223
import org.apache.hadoop.conf.Configuration;
2324
import org.apache.hadoop.fs.Path;
2425
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
@@ -95,8 +96,7 @@ private HFileContext createFileContext(Compression.Algorithm compression,
9596
}
9697

9798
@Override
98-
public final StoreFileWriter createWriter(CreateStoreFileWriterParams params)
99-
throws IOException {
99+
public final StoreFileWriter createWriter(CreateStoreFileWriterParams params) throws IOException {
100100
if (!isPrimaryReplica) {
101101
throw new IllegalStateException("Should not call create writer on secondary replicas");
102102
}
@@ -170,4 +170,12 @@ public final StoreFileWriter createWriter(CreateStoreFileWriterParams params)
170170

171171
protected abstract void doAddCompactionResults(Collection<StoreFileInfo> compactedFiles,
172172
Collection<StoreFileInfo> newFiles) throws IOException;
173+
174+
/**
175+
* used to mirror the store file list after loading when migration.
176+
* <p/>
177+
* Do not add this method to the {@link StoreFileTracker} interface since we do not need this
178+
* method in upper layer.
179+
*/
180+
abstract void set(List<StoreFileInfo> files) throws IOException;
173181
}

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/storefiletracker/StoreFileTrackerFactory.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.apache.hadoop.hbase.util.ReflectionUtils;
2323
import org.apache.yetus.audience.InterfaceAudience;
2424

25+
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
26+
2527
/**
2628
* Factory method for creating store file tracker.
2729
*/
@@ -36,4 +38,17 @@ public static StoreFileTracker create(Configuration conf, boolean isPrimaryRepli
3638
conf.getClass(TRACK_IMPL, DefaultStoreFileTracker.class, StoreFileTracker.class);
3739
return ReflectionUtils.newInstance(tracker, conf, isPrimaryReplica, ctx);
3840
}
41+
42+
static StoreFileTrackerBase create(Configuration conf, String configName,
43+
boolean isPrimaryReplica, StoreContext ctx) {
44+
String className =
45+
Preconditions.checkNotNull(conf.get(configName), "config %s is not set", configName);
46+
Class<? extends StoreFileTrackerBase> tracker;
47+
try {
48+
tracker = Class.forName(className).asSubclass(StoreFileTrackerBase.class);
49+
} catch (ClassNotFoundException e) {
50+
throw new RuntimeException(e);
51+
}
52+
return ReflectionUtils.newInstance(tracker, conf, isPrimaryReplica, ctx);
53+
}
3954
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.regionserver.storefiletracker;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
import java.io.IOException;
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
import org.apache.hadoop.conf.Configuration;
27+
import org.apache.hadoop.fs.Path;
28+
import org.apache.hadoop.hbase.HBaseClassTestRule;
29+
import org.apache.hadoop.hbase.HBaseTestingUtil;
30+
import org.apache.hadoop.hbase.TableName;
31+
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
32+
import org.apache.hadoop.hbase.client.Get;
33+
import org.apache.hadoop.hbase.client.Put;
34+
import org.apache.hadoop.hbase.client.RegionInfo;
35+
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
36+
import org.apache.hadoop.hbase.client.Result;
37+
import org.apache.hadoop.hbase.client.TableDescriptor;
38+
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
39+
import org.apache.hadoop.hbase.regionserver.ChunkCreator;
40+
import org.apache.hadoop.hbase.regionserver.HRegion;
41+
import org.apache.hadoop.hbase.regionserver.MemStoreLAB;
42+
import org.apache.hadoop.hbase.testclassification.MediumTests;
43+
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
44+
import org.apache.hadoop.hbase.util.Bytes;
45+
import org.apache.hadoop.hbase.wal.WAL;
46+
import org.junit.After;
47+
import org.junit.Before;
48+
import org.junit.BeforeClass;
49+
import org.junit.ClassRule;
50+
import org.junit.Rule;
51+
import org.junit.Test;
52+
import org.junit.experimental.categories.Category;
53+
import org.junit.rules.TestName;
54+
import org.junit.runner.RunWith;
55+
import org.junit.runners.Parameterized;
56+
import org.junit.runners.Parameterized.Parameter;
57+
import org.junit.runners.Parameterized.Parameters;
58+
59+
import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
60+
61+
@RunWith(Parameterized.class)
62+
@Category({ RegionServerTests.class, MediumTests.class })
63+
public class TestMigrationStoreFileTracker {
64+
65+
@ClassRule
66+
public static final HBaseClassTestRule CLASS_RULE =
67+
HBaseClassTestRule.forClass(TestMigrationStoreFileTracker.class);
68+
69+
private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
70+
71+
private static final byte[] CF = Bytes.toBytes("cf");
72+
73+
private static final byte[] CQ = Bytes.toBytes("cq");
74+
75+
private static final TableDescriptor TD =
76+
TableDescriptorBuilder.newBuilder(TableName.valueOf("file_based_tracker"))
77+
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).build();
78+
79+
private static final RegionInfo RI = RegionInfoBuilder.newBuilder(TD.getTableName()).build();
80+
81+
@Rule
82+
public TestName name = new TestName();
83+
84+
@Parameter(0)
85+
public Class<? extends StoreFileTrackerBase> srcImplClass;
86+
87+
@Parameter(1)
88+
public Class<? extends StoreFileTrackerBase> dstImplClass;
89+
90+
private HRegion region;
91+
92+
private Path rootDir;
93+
94+
private WAL wal;
95+
96+
@Parameters(name = "{index}: src={0}, dst={1}")
97+
public static List<Object[]> params() {
98+
List<Class<? extends StoreFileTrackerBase>> impls =
99+
Arrays.asList(DefaultStoreFileTracker.class, FileBasedStoreFileTracker.class);
100+
List<Object[]> params = new ArrayList<>();
101+
for (Class<? extends StoreFileTrackerBase> src : impls) {
102+
for (Class<? extends StoreFileTrackerBase> dst : impls) {
103+
if (src.equals(dst)) {
104+
continue;
105+
}
106+
params.add(new Object[] { src, dst });
107+
}
108+
}
109+
return params;
110+
}
111+
112+
@BeforeClass
113+
public static void setUpBeforeClass() {
114+
ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null,
115+
MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
116+
}
117+
118+
@Before
119+
public void setUp() throws IOException {
120+
Configuration conf = UTIL.getConfiguration();
121+
conf.setClass(MigrationStoreFileTracker.SRC_IMPL, srcImplClass, StoreFileTrackerBase.class);
122+
conf.setClass(MigrationStoreFileTracker.DST_IMPL, dstImplClass, StoreFileTrackerBase.class);
123+
rootDir = UTIL.getDataTestDir(name.getMethodName().replaceAll("[=:\\[ ]", "_"));
124+
wal = HBaseTestingUtil.createWal(conf, rootDir, RI);
125+
}
126+
127+
@After
128+
public void tearDown() throws IOException {
129+
if (region != null) {
130+
region.close();
131+
}
132+
Closeables.close(wal, true);
133+
UTIL.cleanupTestDir();
134+
}
135+
136+
private HRegion createRegion(Class<? extends StoreFileTrackerBase> trackerImplClass)
137+
throws IOException {
138+
Configuration conf = new Configuration(UTIL.getConfiguration());
139+
conf.setClass(StoreFileTrackerFactory.TRACK_IMPL, trackerImplClass, StoreFileTracker.class);
140+
return HRegion.createHRegion(RI, rootDir, conf, TD, wal, true);
141+
}
142+
143+
private HRegion reopenRegion(Class<? extends StoreFileTrackerBase> trackerImplClass)
144+
throws IOException {
145+
region.close();
146+
Configuration conf = new Configuration(UTIL.getConfiguration());
147+
conf.setClass(StoreFileTrackerFactory.TRACK_IMPL, trackerImplClass, StoreFileTracker.class);
148+
return HRegion.openHRegion(rootDir, RI, TD, wal, conf);
149+
}
150+
151+
private void putData(int start, int end) throws IOException {
152+
for (int i = start; i < end; i++) {
153+
region.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
154+
if (i % 30 == 0) {
155+
region.flush(true);
156+
}
157+
}
158+
}
159+
160+
private void verifyData(int start, int end) throws IOException {
161+
for (int i = start; i < end; i++) {
162+
Result result = region.get(new Get(Bytes.toBytes(i)));
163+
assertEquals(i, Bytes.toInt(result.getValue(CF, CQ)));
164+
}
165+
}
166+
167+
@Test
168+
public void testMigration() throws IOException {
169+
region = createRegion(srcImplClass);
170+
putData(0, 100);
171+
verifyData(0, 100);
172+
region = reopenRegion(MigrationStoreFileTracker.class);
173+
verifyData(0, 100);
174+
region.compact(true);
175+
putData(100, 200);
176+
region = reopenRegion(dstImplClass);
177+
verifyData(0, 200);
178+
}
179+
}

0 commit comments

Comments
 (0)