Skip to content

Commit b7fb168

Browse files
committed
HBASE-27579 CatalogJanitor can cause data loss due to errors during cleanMergeRegion (apache#4986)
Signed-off-by: Andrew Purtell <apurtell@apache.org> Signed-off-by: Viraj Jasani <vjasani@apache.org> Signed-off-by: Duo Zhang <zhangduo@apache.org>
1 parent c6bdba8 commit b7fb168

2 files changed

Lines changed: 146 additions & 56 deletions

File tree

hbase-server/src/main/java/org/apache/hadoop/hbase/master/janitor/CatalogJanitor.java

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.apache.hadoop.hbase.MetaTableAccessor;
3535
import org.apache.hadoop.hbase.ScheduledChore;
3636
import org.apache.hadoop.hbase.TableName;
37-
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
3837
import org.apache.hadoop.hbase.client.Connection;
3938
import org.apache.hadoop.hbase.client.ConnectionFactory;
4039
import org.apache.hadoop.hbase.client.Get;
@@ -183,12 +182,12 @@ public int scan() throws IOException {
183182
for (Map.Entry<RegionInfo, Result> e : mergedRegions.entrySet()) {
184183
if (this.services.isInMaintenanceMode()) {
185184
// Stop cleaning if the master is in maintenance mode
186-
LOG.debug("In maintenence mode, not cleaning");
185+
LOG.debug("In maintenance mode, not cleaning");
187186
break;
188187
}
189188

190189
List<RegionInfo> parents = MetaTableAccessor.getMergeRegions(e.getValue().rawCells());
191-
if (parents != null && cleanMergeRegion(e.getKey(), parents)) {
190+
if (parents != null && cleanMergeRegion(this.services, e.getKey(), parents)) {
192191
gcs++;
193192
}
194193
}
@@ -201,7 +200,7 @@ public int scan() throws IOException {
201200
if (this.services.isInMaintenanceMode()) {
202201
// Stop cleaning if the master is in maintenance mode
203202
if (LOG.isDebugEnabled()) {
204-
LOG.debug("In maintenence mode, not cleaning");
203+
LOG.debug("In maintenance mode, not cleaning");
205204
}
206205
break;
207206
}
@@ -248,30 +247,24 @@ public CatalogJanitorReport getLastReport() {
248247
* @return true if we delete references in merged region on hbase:meta and archive the files on
249248
* the file system
250249
*/
251-
private boolean cleanMergeRegion(final RegionInfo mergedRegion, List<RegionInfo> parents)
252-
throws IOException {
250+
static boolean cleanMergeRegion(MasterServices services, final RegionInfo mergedRegion,
251+
List<RegionInfo> parents) throws IOException {
253252
if (LOG.isDebugEnabled()) {
254253
LOG.debug("Cleaning merged region {}", mergedRegion);
255254
}
256-
FileSystem fs = this.services.getMasterFileSystem().getFileSystem();
257-
Path rootdir = this.services.getMasterFileSystem().getRootDir();
258-
Path tabledir = CommonFSUtils.getTableDir(rootdir, mergedRegion.getTable());
259-
TableDescriptor htd = getDescriptor(mergedRegion.getTable());
260-
HRegionFileSystem regionFs = null;
261-
try {
262-
regionFs = HRegionFileSystem.openRegionFromFileSystem(this.services.getConfiguration(), fs,
263-
tabledir, mergedRegion, true);
264-
} catch (IOException e) {
265-
LOG.warn("Merged region does not exist: " + mergedRegion.getEncodedName());
266-
}
267-
if (regionFs == null || !regionFs.hasReferences(htd)) {
255+
256+
Pair<Boolean, Boolean> result =
257+
checkRegionReferences(services, mergedRegion.getTable(), mergedRegion);
258+
259+
if (hasNoReferences(result)) {
268260
if (LOG.isDebugEnabled()) {
269261
LOG.debug(
270262
"Deleting parents ({}) from fs; merged child {} no longer holds references", parents
271263
.stream().map(r -> RegionInfo.getShortNameToLog(r)).collect(Collectors.joining(", ")),
272264
mergedRegion);
273265
}
274-
ProcedureExecutor<MasterProcedureEnv> pe = this.services.getMasterProcedureExecutor();
266+
267+
ProcedureExecutor<MasterProcedureEnv> pe = services.getMasterProcedureExecutor();
275268
GCMultipleMergedRegionsProcedure mergeRegionProcedure =
276269
new GCMultipleMergedRegionsProcedure(pe.getEnvironment(), mergedRegion, parents);
277270
pe.submitProcedure(mergeRegionProcedure);
@@ -280,7 +273,15 @@ private boolean cleanMergeRegion(final RegionInfo mergedRegion, List<RegionInfo>
280273
mergedRegion);
281274
}
282275
return true;
276+
} else {
277+
if (LOG.isDebugEnabled()) {
278+
LOG.debug(
279+
"Deferring cleanup up of {} parents of merged region {}, because references "
280+
+ "still exist in merged region or we encountered an exception in checking",
281+
parents.size(), mergedRegion.getEncodedName());
282+
}
283283
}
284+
284285
return false;
285286
}
286287

@@ -332,8 +333,10 @@ static boolean cleanParent(MasterServices services, RegionInfo parent, Result ro
332333
}
333334
// Run checks on each daughter split.
334335
PairOfSameType<RegionInfo> daughters = MetaTableAccessor.getDaughterRegions(rowContent);
335-
Pair<Boolean, Boolean> a = checkDaughterInFs(services, parent, daughters.getFirst());
336-
Pair<Boolean, Boolean> b = checkDaughterInFs(services, parent, daughters.getSecond());
336+
Pair<Boolean, Boolean> a =
337+
checkRegionReferences(services, parent.getTable(), daughters.getFirst());
338+
Pair<Boolean, Boolean> b =
339+
checkRegionReferences(services, parent.getTable(), daughters.getSecond());
337340
if (hasNoReferences(a) && hasNoReferences(b)) {
338341
String daughterA =
339342
daughters.getFirst() != null ? daughters.getFirst().getShortNameToLog() : "null";
@@ -386,59 +389,45 @@ private static boolean hasNoReferences(final Pair<Boolean, Boolean> p) {
386389
}
387390

388391
/**
389-
* Checks if a daughter region -- either splitA or splitB -- still holds references to parent.
390-
* @param parent Parent region
391-
* @param daughter Daughter region
392-
* @return A pair where the first boolean says whether or not the daughter region directory exists
393-
* in the filesystem and then the second boolean says whether the daughter has references
394-
* to the parent.
392+
* Checks if a region still holds references to parent.
393+
* @param tableName The table for the region
394+
* @param region The region to check
395+
* @return A pair where the first boolean says whether the region directory exists in the
396+
* filesystem and then the second boolean says whether the region has references to a
397+
* parent.
395398
*/
396-
private static Pair<Boolean, Boolean> checkDaughterInFs(MasterServices services,
397-
final RegionInfo parent, final RegionInfo daughter) throws IOException {
398-
if (daughter == null) {
399+
private static Pair<Boolean, Boolean> checkRegionReferences(MasterServices services,
400+
TableName tableName, RegionInfo region) throws IOException {
401+
if (region == null) {
399402
return new Pair<>(Boolean.FALSE, Boolean.FALSE);
400403
}
401404

402405
FileSystem fs = services.getMasterFileSystem().getFileSystem();
403406
Path rootdir = services.getMasterFileSystem().getRootDir();
404-
Path tabledir = CommonFSUtils.getTableDir(rootdir, daughter.getTable());
405-
406-
Path daughterRegionDir = new Path(tabledir, daughter.getEncodedName());
407-
408-
HRegionFileSystem regionFs;
407+
Path tabledir = CommonFSUtils.getTableDir(rootdir, tableName);
408+
Path regionDir = new Path(tabledir, region.getEncodedName());
409409

410410
try {
411-
if (!CommonFSUtils.isExists(fs, daughterRegionDir)) {
411+
if (!CommonFSUtils.isExists(fs, regionDir)) {
412412
return new Pair<>(Boolean.FALSE, Boolean.FALSE);
413413
}
414414
} catch (IOException ioe) {
415-
LOG.error("Error trying to determine if daughter region exists, "
416-
+ "assuming exists and has references", ioe);
415+
LOG.error("Error trying to determine if region exists, assuming exists and has references",
416+
ioe);
417417
return new Pair<>(Boolean.TRUE, Boolean.TRUE);
418418
}
419419

420-
boolean references = false;
421-
TableDescriptor parentDescriptor = services.getTableDescriptors().get(parent.getTable());
420+
TableDescriptor tableDescriptor = services.getTableDescriptors().get(tableName);
422421
try {
423-
regionFs = HRegionFileSystem.openRegionFromFileSystem(services.getConfiguration(), fs,
424-
tabledir, daughter, true);
425-
426-
for (ColumnFamilyDescriptor family : parentDescriptor.getColumnFamilies()) {
427-
references = regionFs.hasReferences(family.getNameAsString());
428-
if (references) {
429-
break;
430-
}
431-
}
422+
HRegionFileSystem regionFs = HRegionFileSystem
423+
.openRegionFromFileSystem(services.getConfiguration(), fs, tabledir, region, true);
424+
boolean references = regionFs.hasReferences(tableDescriptor);
425+
return new Pair<>(Boolean.TRUE, references);
432426
} catch (IOException e) {
433-
LOG.error("Error trying to determine referenced files from : " + daughter.getEncodedName()
434-
+ ", to: " + parent.getEncodedName() + " assuming has references", e);
427+
LOG.error("Error trying to determine if region {} has references, assuming it does",
428+
region.getEncodedName(), e);
435429
return new Pair<>(Boolean.TRUE, Boolean.TRUE);
436430
}
437-
return new Pair<>(Boolean.TRUE, references);
438-
}
439-
440-
private TableDescriptor getDescriptor(final TableName tableName) throws IOException {
441-
return this.services.getTableDescriptors().get(tableName);
442431
}
443432

444433
private void updateAssignmentManagerMetrics() {

hbase-server/src/test/java/org/apache/hadoop/hbase/master/janitor/TestCatalogJanitor.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertTrue;
24+
import static org.mockito.Mockito.any;
25+
import static org.mockito.Mockito.doAnswer;
2426
import static org.mockito.Mockito.doReturn;
27+
import static org.mockito.Mockito.doThrow;
2528
import static org.mockito.Mockito.spy;
29+
import static org.mockito.Mockito.when;
2630

2731
import java.io.IOException;
2832
import java.util.ArrayList;
@@ -34,6 +38,7 @@
3438
import java.util.SortedSet;
3539
import java.util.TreeMap;
3640
import java.util.concurrent.ConcurrentSkipListMap;
41+
import java.util.concurrent.atomic.AtomicBoolean;
3742
import org.apache.hadoop.fs.FSDataOutputStream;
3843
import org.apache.hadoop.fs.FileStatus;
3944
import org.apache.hadoop.fs.FileSystem;
@@ -46,10 +51,12 @@
4651
import org.apache.hadoop.hbase.MetaMockingUtil;
4752
import org.apache.hadoop.hbase.ServerName;
4853
import org.apache.hadoop.hbase.TableName;
54+
import org.apache.hadoop.hbase.client.RegionInfo;
4955
import org.apache.hadoop.hbase.client.Result;
5056
import org.apache.hadoop.hbase.client.TableDescriptor;
5157
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
5258
import org.apache.hadoop.hbase.io.Reference;
59+
import org.apache.hadoop.hbase.master.MasterFileSystem;
5360
import org.apache.hadoop.hbase.master.MasterServices;
5461
import org.apache.hadoop.hbase.master.assignment.MockMasterServices;
5562
import org.apache.hadoop.hbase.master.janitor.CatalogJanitor.SplitParentFirstComparator;
@@ -114,6 +121,100 @@ public void teardown() {
114121
this.masterServices.stop("DONE");
115122
}
116123

124+
@Test
125+
public void testCleanMerge() throws IOException {
126+
TableDescriptor td = createTableDescriptorForCurrentMethod();
127+
// Create regions.
128+
HRegionInfo merged =
129+
new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("eee"));
130+
HRegionInfo parenta =
131+
new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("ccc"));
132+
HRegionInfo parentb =
133+
new HRegionInfo(td.getTableName(), Bytes.toBytes("ccc"), Bytes.toBytes("eee"));
134+
135+
List<RegionInfo> parents = new ArrayList<>();
136+
parents.add(parenta);
137+
parents.add(parentb);
138+
139+
Path rootdir = this.masterServices.getMasterFileSystem().getRootDir();
140+
Path tabledir = CommonFSUtils.getTableDir(rootdir, td.getTableName());
141+
Path storedir =
142+
HRegionFileSystem.getStoreHomedir(tabledir, merged, td.getColumnFamilies()[0].getName());
143+
144+
Path parentaRef = createMergeReferenceFile(storedir, merged, parenta);
145+
Path parentbRef = createMergeReferenceFile(storedir, merged, parentb);
146+
147+
// references exist, should not clean
148+
assertFalse(CatalogJanitor.cleanMergeRegion(masterServices, merged, parents));
149+
150+
masterServices.getMasterFileSystem().getFileSystem().delete(parentaRef, false);
151+
152+
// one reference still exists, should not clean
153+
assertFalse(CatalogJanitor.cleanMergeRegion(masterServices, merged, parents));
154+
155+
masterServices.getMasterFileSystem().getFileSystem().delete(parentbRef, false);
156+
157+
// all references removed, should clean
158+
assertTrue(CatalogJanitor.cleanMergeRegion(masterServices, merged, parents));
159+
}
160+
161+
@Test
162+
public void testDontCleanMergeIfFileSystemException() throws IOException {
163+
TableDescriptor td = createTableDescriptorForCurrentMethod();
164+
// Create regions.
165+
HRegionInfo merged =
166+
new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("eee"));
167+
HRegionInfo parenta =
168+
new HRegionInfo(td.getTableName(), Bytes.toBytes("aaa"), Bytes.toBytes("ccc"));
169+
HRegionInfo parentb =
170+
new HRegionInfo(td.getTableName(), Bytes.toBytes("ccc"), Bytes.toBytes("eee"));
171+
172+
List<RegionInfo> parents = new ArrayList<>();
173+
parents.add(parenta);
174+
parents.add(parentb);
175+
176+
Path rootdir = this.masterServices.getMasterFileSystem().getRootDir();
177+
Path tabledir = CommonFSUtils.getTableDir(rootdir, td.getTableName());
178+
Path storedir =
179+
HRegionFileSystem.getStoreHomedir(tabledir, merged, td.getColumnFamilies()[0].getName());
180+
createMergeReferenceFile(storedir, merged, parenta);
181+
182+
MasterServices mockedMasterServices = spy(masterServices);
183+
MasterFileSystem mockedMasterFileSystem = spy(masterServices.getMasterFileSystem());
184+
FileSystem mockedFileSystem = spy(masterServices.getMasterFileSystem().getFileSystem());
185+
186+
when(mockedMasterServices.getMasterFileSystem()).thenReturn(mockedMasterFileSystem);
187+
when(mockedMasterFileSystem.getFileSystem()).thenReturn(mockedFileSystem);
188+
189+
// throw on the first exists check
190+
doThrow(new IOException("Some exception")).when(mockedFileSystem).exists(any());
191+
192+
assertFalse(CatalogJanitor.cleanMergeRegion(mockedMasterServices, merged, parents));
193+
194+
// throw on the second exists check (within HRegionfileSystem)
195+
AtomicBoolean returned = new AtomicBoolean(false);
196+
doAnswer(invocationOnMock -> {
197+
if (!returned.get()) {
198+
returned.set(true);
199+
return masterServices.getMasterFileSystem().getFileSystem()
200+
.exists(invocationOnMock.getArgument(0));
201+
}
202+
throw new IOException("Some exception");
203+
}).when(mockedFileSystem).exists(any());
204+
205+
assertFalse(CatalogJanitor.cleanMergeRegion(mockedMasterServices, merged, parents));
206+
}
207+
208+
private Path createMergeReferenceFile(Path storeDir, HRegionInfo mergedRegion,
209+
HRegionInfo parentRegion) throws IOException {
210+
Reference ref = Reference.createTopReference(mergedRegion.getStartKey());
211+
long now = EnvironmentEdgeManager.currentTime();
212+
// Reference name has this format: StoreFile#REF_NAME_PARSER
213+
Path p = new Path(storeDir, Long.toString(now) + "." + parentRegion.getEncodedName());
214+
FileSystem fs = this.masterServices.getMasterFileSystem().getFileSystem();
215+
return ref.write(fs, p);
216+
}
217+
117218
/**
118219
* Test clearing a split parent.
119220
*/

0 commit comments

Comments
 (0)