From 95727a581d83beff77d80fe4c245d99d00012f40 Mon Sep 17 00:00:00 2001 From: Guanghao Zhang Date: Tue, 19 Feb 2019 20:50:37 +0800 Subject: [PATCH] HBASE-20724 Sometimes some compacted storefiles are still opened after region failover --- .../apache/hadoop/hbase/io/hfile/HFileContext.java | 13 ++ .../src/main/protobuf/HFile.proto | 1 + .../hadoop/hbase/io/hfile/FixedFileTrailer.java | 28 +++- .../hadoop/hbase/io/hfile/HFileWriterImpl.java | 2 +- .../regionserver/AbstractMultiFileWriter.java | 13 +- .../apache/hadoop/hbase/regionserver/HStore.java | 19 +++ .../hadoop/hbase/regionserver/StoreFileWriter.java | 17 +++ .../compactions/DateTieredCompactor.java | 2 +- .../regionserver/compactions/DefaultCompactor.java | 2 +- .../regionserver/compactions/StripeCompactor.java | 2 +- .../TestCleanupCompactedFileAfterFailover.java | 165 +++++++++++++++++++++ 11 files changed, 253 insertions(+), 11 deletions(-) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCleanupCompactedFileAfterFailover.java diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileContext.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileContext.java index b5ccda2..615ac57 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileContext.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileContext.java @@ -17,6 +17,9 @@ */ package org.apache.hadoop.hbase.io.hfile; +import java.util.HashSet; +import java.util.Set; + import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.io.HeapSize; import org.apache.hadoop.hbase.io.compress.Compression; @@ -59,6 +62,8 @@ public class HFileContext implements HeapSize, Cloneable { private long fileCreateTime; private String hfileName; + private final Set compactedFiles = new HashSet<>(); + //Empty constructor. Go with setters public HFileContext() { } @@ -187,6 +192,14 @@ public class HFileContext implements HeapSize, Cloneable { return this.hfileName; } + public void addCompactedFile(String compactedFile) { + this.compactedFiles.add(compactedFile); + } + + public Set getCompactedFiles() { + return this.compactedFiles; + } + /** * HeapSize implementation * NOTE : The heapsize should be altered as and when new state variable are added diff --git a/hbase-protocol-shaded/src/main/protobuf/HFile.proto b/hbase-protocol-shaded/src/main/protobuf/HFile.proto index 33f89a2..c94d740 100644 --- a/hbase-protocol-shaded/src/main/protobuf/HFile.proto +++ b/hbase-protocol-shaded/src/main/protobuf/HFile.proto @@ -47,4 +47,5 @@ message FileTrailerProto { optional string comparator_class_name = 11; optional uint32 compression_codec = 12; optional bytes encryption_key = 13; + repeated string compacted_files = 14; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/FixedFileTrailer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/FixedFileTrailer.java index 3c74d11..ca60231 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/FixedFileTrailer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/FixedFileTrailer.java @@ -18,7 +18,6 @@ */ package org.apache.hadoop.hbase.io.hfile; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInput; @@ -26,21 +25,24 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.HashSet; +import java.util.Set; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.hbase.CellComparator; import org.apache.hadoop.hbase.CellComparatorImpl; import org.apache.hadoop.hbase.CellComparatorImpl.MetaCellComparator; import org.apache.hadoop.hbase.KeyValue; -import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.hbase.io.compress.Compression; -import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; import org.apache.hadoop.hbase.shaded.protobuf.generated.HFileProtos; import org.apache.hadoop.hbase.util.Bytes; - +import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.apache.hbase.thirdparty.com.google.protobuf.ProtocolStringList; +import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; + /** * The {@link HFile} has a fixed trailer which contains offsets to other * variable parts of the file. Also includes basic metadata on this file. The @@ -126,6 +128,8 @@ public class FixedFileTrailer { /** The {@link HFile} format minor version. */ private final int minorVersion; + private Set compactedFiles = new HashSet<>(); + FixedFileTrailer(int majorVersion, int minorVersion) { this.majorVersion = majorVersion; this.minorVersion = minorVersion; @@ -208,6 +212,9 @@ public class FixedFileTrailer { if (encryptionKey != null) { builder.setEncryptionKey(UnsafeByteOperations.unsafeWrap(encryptionKey)); } + if (compactedFiles != null) { + compactedFiles.forEach(builder::addCompactedFiles); + } return builder.build(); } @@ -316,6 +323,10 @@ public class FixedFileTrailer { if (trailerProto.hasEncryptionKey()) { encryptionKey = trailerProto.getEncryptionKey().toByteArray(); } + ProtocolStringList compactedFilesList = trailerProto.getCompactedFilesList(); + if (compactedFilesList != null) { + compactedFilesList.forEach(compactedFiles::add); + } } /** @@ -368,6 +379,7 @@ public class FixedFileTrailer { } append(sb, "majorVersion=" + majorVersion); append(sb, "minorVersion=" + minorVersion); + append(sb, "compactedFiles=" + compactedFiles); return sb.toString(); } @@ -666,6 +678,14 @@ public class FixedFileTrailer { this.encryptionKey = keyBytes; } + public void setCompactedFile(Set compactedFiles) { + this.compactedFiles = compactedFiles; + } + + public Set getCompactedFiles() { + return this.compactedFiles; + } + /** * Extracts the major version for a 4-byte serialized version data. * The major version is the 3 least significant bytes diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileWriterImpl.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileWriterImpl.java index 2726977..8e4ec9f 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileWriterImpl.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileWriterImpl.java @@ -665,7 +665,7 @@ public class HFileWriterImpl implements HFile.Writer { trailer.setLastDataBlockOffset(lastDataBlockOffset); trailer.setComparatorClass(comparator.getClass()); trailer.setDataIndexCount(dataBlockIndexWriter.getNumRootEntries()); - + trailer.setCompactedFile(hFileContext.getCompactedFiles()); finishClose(trailer); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/AbstractMultiFileWriter.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/AbstractMultiFileWriter.java index 43d0ad8..f9cc400 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/AbstractMultiFileWriter.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/AbstractMultiFileWriter.java @@ -20,6 +20,7 @@ package org.apache.hadoop.hbase.regionserver; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import org.apache.hadoop.fs.Path; import org.apache.yetus.audience.InterfaceAudience; @@ -62,18 +63,24 @@ public abstract class AbstractMultiFileWriter implements CellSink, ShipperListen * comments in HBASE-15400 for more details. */ public List commitWriters(long maxSeqId, boolean majorCompaction) throws IOException { + return commitWriters(maxSeqId, majorCompaction, Collections.EMPTY_SET); + } + + public List commitWriters(long maxSeqId, boolean majorCompaction, + Collection storeFiles) throws IOException { preCommitWriters(); Collection writers = this.writers(); if (LOG.isDebugEnabled()) { - LOG.debug("Commit " + writers.size() + " writers, maxSeqId=" + maxSeqId - + ", majorCompaction=" + majorCompaction); + LOG.debug( + "Commit " + writers.size() + " writers, maxSeqId=" + maxSeqId + ", majorCompaction=" + + majorCompaction); } List paths = new ArrayList<>(); for (StoreFileWriter writer : writers) { if (writer == null) { continue; } - writer.appendMetadata(maxSeqId, majorCompaction); + writer.appendMetadata(maxSeqId, majorCompaction, storeFiles); preCloseWriter(writer); paths.add(writer.getPath()); writer.close(); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java index 5056ad7..d51ade1 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java @@ -577,6 +577,7 @@ public class HStore implements Store, HeapSize, StoreConfigInformation, Propagat totalValidStoreFile++; } + Set compactedFiles = new HashSet<>(); ArrayList results = new ArrayList<>(files.size()); IOException ioe = null; try { @@ -586,6 +587,8 @@ public class HStore implements Store, HeapSize, StoreConfigInformation, Propagat if (storeFile != null) { LOG.debug("loaded {}", storeFile); results.add(storeFile); + compactedFiles + .addAll(storeFile.getReader().getHFileReader().getTrailer().getCompactedFiles()); } } catch (InterruptedException e) { if (ioe == null) ioe = new InterruptedIOException(e.getMessage()); @@ -612,6 +615,22 @@ public class HStore implements Store, HeapSize, StoreConfigInformation, Propagat throw ioe; } + // Remove the compacted files from result + final List filesToRemove = new ArrayList<>(compactedFiles.size()); + for (HStoreFile storeFile : results) { + if (compactedFiles.contains(storeFile.getPath().getName())) { + LOG.warn("Clearing the compacted file {} from this store", storeFile); + storeFile.getReader().close(true); + filesToRemove.add(storeFile); + } + } + results.removeAll(filesToRemove); + if (!filesToRemove.isEmpty() && this.isPrimaryReplicaStore()) { + LOG.debug("Moving the files {} to archive", filesToRemove); + // Only if this is successful it has to be removed + this.fs.removeStoreFiles(this.getColumnFamilyDescriptor().getNameAsString(), filesToRemove); + } + return results; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFileWriter.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFileWriter.java index b31df39..2f97d47 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFileWriter.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFileWriter.java @@ -28,6 +28,7 @@ import static org.apache.hadoop.hbase.regionserver.HStoreFile.TIMERANGE_KEY; import java.io.IOException; import java.net.InetSocketAddress; +import java.util.Collection; import java.util.UUID; import java.util.regex.Pattern; @@ -182,6 +183,22 @@ public class StoreFileWriter implements CellSink, ShipperListener { * Call before {@link #close()} since its written as meta data to this file. * @param maxSequenceId Maximum sequence id. * @param majorCompaction True if this file is product of a major compaction + * @throws IOException problem writing to FS + */ + public void appendMetadata(final long maxSequenceId, final boolean majorCompaction, + final Collection storeFiles) throws IOException { + writer.appendFileInfo(MAX_SEQ_ID_KEY, Bytes.toBytes(maxSequenceId)); + writer.appendFileInfo(MAJOR_COMPACTION_KEY, Bytes.toBytes(majorCompaction)); + storeFiles.forEach( + sf -> writer.getFileContext().addCompactedFile(sf.getFileInfo().getPath().getName())); + appendTrackedTimestampsToMetadata(); + } + + /** + * Writes meta data. + * Call before {@link #close()} since its written as meta data to this file. + * @param maxSequenceId Maximum sequence id. + * @param majorCompaction True if this file is product of a major compaction * @param mobCellsCount The number of mob cells. * @throws IOException problem writing to FS */ diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/DateTieredCompactor.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/DateTieredCompactor.java index 21eaa94..1bf5236 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/DateTieredCompactor.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/DateTieredCompactor.java @@ -78,6 +78,6 @@ public class DateTieredCompactor extends AbstractMultiOutputCompactor commitWriter(DateTieredMultiFileWriter writer, FileDetails fd, CompactionRequestImpl request) throws IOException { - return writer.commitWriters(fd.maxSeqId, request.isAllFiles()); + return writer.commitWriters(fd.maxSeqId, request.isAllFiles(), request.getFiles()); } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/DefaultCompactor.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/DefaultCompactor.java index 7a398ea..ac6a7dc 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/DefaultCompactor.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/DefaultCompactor.java @@ -86,7 +86,7 @@ public class DefaultCompactor extends Compactor { protected List commitWriter(StoreFileWriter writer, FileDetails fd, CompactionRequestImpl request) throws IOException { List newFiles = Lists.newArrayList(writer.getPath()); - writer.appendMetadata(fd.maxSeqId, request.isAllFiles()); + writer.appendMetadata(fd.maxSeqId, request.isAllFiles(), request.getFiles()); writer.close(); return newFiles; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/StripeCompactor.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/StripeCompactor.java index 41e0a71..fe07d9e 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/StripeCompactor.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/StripeCompactor.java @@ -127,7 +127,7 @@ public class StripeCompactor extends AbstractMultiOutputCompactor commitWriter(StripeMultiFileWriter writer, FileDetails fd, CompactionRequestImpl request) throws IOException { - List newFiles = writer.commitWriters(fd.maxSeqId, request.isMajor()); + List newFiles = writer.commitWriters(fd.maxSeqId, request.isMajor(), request.getFiles()); assert !newFiles.isEmpty() : "Should have produced an empty file to preserve metadata."; return newFiles; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCleanupCompactedFileAfterFailover.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCleanupCompactedFileAfterFailover.java new file mode 100644 index 0000000..3b2e89f --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCleanupCompactedFileAfterFailover.java @@ -0,0 +1,165 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.regionserver; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner; +import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.JVMClusterUtil; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Category({LargeTests.class}) +public class TestCleanupCompactedFileAfterFailover { + + private static final Logger LOG = + LoggerFactory.getLogger(TestCleanupCompactedFileAfterFailover.class); + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestCleanupCompactedFileAfterFailover.class); + + private static HBaseTestingUtility TEST_UTIL; + private static Admin admin; + private static Table table; + + private static TableName TABLE_NAME = TableName.valueOf("TestCleanupCompactedFileAfterFailover"); + private static byte[] ROW = Bytes.toBytes("row"); + private static byte[] FAMILY = Bytes.toBytes("cf"); + private static byte[] QUALIFIER = Bytes.toBytes("cq"); + private static byte[] VALUE = Bytes.toBytes("value"); + private static final int RS_NUMBER = 2; + + @BeforeClass + public static void beforeClass() throws Exception { + TEST_UTIL = new HBaseTestingUtility(); + // Set the scanner lease to 20min, so the scanner can't be closed by RegionServer + TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, 1200000); + TEST_UTIL.getConfiguration() + .setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_KEY, 100); + TEST_UTIL.getConfiguration().set("dfs.blocksize", "64000"); + TEST_UTIL.getConfiguration().set("dfs.namenode.fs-limits.min-block-size", "1024"); + TEST_UTIL.getConfiguration().set(TimeToLiveHFileCleaner.TTL_CONF_KEY, "0"); + TEST_UTIL.startMiniCluster(RS_NUMBER); + + admin = TEST_UTIL.getAdmin(); + TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TABLE_NAME); + builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)); + admin.createTable(builder.build()); + TEST_UTIL.waitTableAvailable(TABLE_NAME); + table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + } + + @AfterClass + public static void afterclass() throws Exception { + admin.disableTable(TABLE_NAME); + admin.deleteTable(TABLE_NAME); + TEST_UTIL.shutdownMiniCluster(); + } + + @Test + public void testCleanupAfterFailover() throws Exception { + HRegionServer rsServedTable = null; + List regions = new ArrayList<>(); + for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster() + .getRegionServerThreads()) { + HRegionServer rs = rsThread.getRegionServer(); + if (rs.getOnlineTables().contains(TABLE_NAME)) { + regions.addAll(rs.getRegions(TABLE_NAME)); + rsServedTable = rs; + } + } + assertNotNull(rsServedTable); + assertEquals("Table should only have one region", 1, regions.size()); + HRegion region = regions.get(0); + HStore store = region.getStore(FAMILY); + + writeDataAndFlush(3, region); + assertEquals(3, store.getStorefilesCount()); + + // Open a scanner and not close, then the storefile will be referenced + table.getScanner(new Scan()); + + region.compact(true); + assertEquals(1, store.getStorefilesCount()); + // The compacted file should not be archived as there are references by user scanner + assertEquals(3, store.getStoreEngine().getStoreFileManager().getCompactedfiles().size()); + + int walNum = rsServedTable.getWALs().size(); + // Roll WAL + rsServedTable.walRoller.requestRollAll(); + // Flush again + region.flush(true); + // The WAL which contains compaction event marker should be archived + assertEquals("The old WAL should be archived", walNum, rsServedTable.getWALs().size()); + + rsServedTable.kill(); + // Sleep to wait failover + Thread.sleep(3000); + TEST_UTIL.waitTableAvailable(TABLE_NAME); + + regions.clear(); + for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster() + .getRegionServerThreads()) { + HRegionServer rs = rsThread.getRegionServer(); + if (rs != rsServedTable && rs.getOnlineTables().contains(TABLE_NAME)) { + regions.addAll(rs.getRegions(TABLE_NAME)); + } + } + assertEquals("Table should only have one region", 1, regions.size()); + region = regions.get(0); + store = region.getStore(FAMILY); + // The compacted storefile should be cleaned and only have 1 storefile + assertEquals(1, store.getStorefilesCount()); + } + + private void writeDataAndFlush(int fileNum, HRegion region) throws Exception { + for (int i = 0; i < fileNum; i++) { + for (int j = 0; j < 100; j++) { + table.put(new Put(concat(ROW, j)).addColumn(FAMILY, QUALIFIER, concat(VALUE, j))); + } + region.flush(true); + } + } + + private byte[] concat(byte[] base, int index) { + return Bytes.toBytes(Bytes.toString(base) + "-" + index); + } +} -- 2.7.4