-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HBASE-28456 HBase Restore restores old data if data for the same timestamp is in different hfiles #5775
HBASE-28456 HBase Restore restores old data if data for the same timestamp is in different hfiles #5775
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
/* | ||
* 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.backup; | ||
|
||
import static org.apache.hadoop.hbase.backup.BackupInfo.BackupState.COMPLETE; | ||
import static org.apache.hadoop.hbase.backup.BackupType.FULL; | ||
import static org.junit.Assert.*; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.time.Instant; | ||
import java.util.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.hbase.Cell; | ||
import org.apache.hadoop.hbase.HBaseClassTestRule; | ||
import org.apache.hadoop.hbase.HBaseCommonTestingUtil; | ||
import org.apache.hadoop.hbase.HBaseConfiguration; | ||
import org.apache.hadoop.hbase.KeyValue; | ||
import org.apache.hadoop.hbase.TableName; | ||
import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl; | ||
import org.apache.hadoop.hbase.backup.impl.BackupManager; | ||
import org.apache.hadoop.hbase.client.*; | ||
import org.apache.hadoop.hbase.io.hfile.HFile; | ||
import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder; | ||
import org.apache.hadoop.hbase.testclassification.MediumTests; | ||
import org.apache.hadoop.hbase.testing.TestingHBaseCluster; | ||
import org.apache.hadoop.hbase.testing.TestingHBaseClusterOption; | ||
import org.apache.hadoop.hbase.tool.BulkLoadHFiles; | ||
import org.apache.hadoop.hbase.util.Bytes; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Category(MediumTests.class) | ||
@RunWith(Parameterized.class) | ||
public class TestBackupRestoreWithModifications { | ||
|
||
private static final Logger LOG = | ||
LoggerFactory.getLogger(TestBackupRestoreWithModifications.class); | ||
|
||
@ClassRule | ||
public static final HBaseClassTestRule CLASS_RULE = | ||
HBaseClassTestRule.forClass(TestBackupRestoreWithModifications.class); | ||
|
||
@Parameterized.Parameters(name = "{index}: useBulkLoad={0}") | ||
public static Iterable<Object[]> data() { | ||
return HBaseCommonTestingUtil.BOOLEAN_PARAMETERIZED; | ||
} | ||
|
||
@Parameterized.Parameter(0) | ||
public boolean useBulkLoad; | ||
|
||
private TableName sourceTable; | ||
private TableName targetTable; | ||
|
||
private List<TableName> allTables; | ||
private static TestingHBaseCluster cluster; | ||
private static final Path BACKUP_ROOT_DIR = new Path("backupIT"); | ||
private static final byte[] COLUMN_FAMILY = Bytes.toBytes("0"); | ||
|
||
@BeforeClass | ||
public static void beforeClass() throws Exception { | ||
Configuration conf = HBaseConfiguration.create(); | ||
enableBackup(conf); | ||
cluster = TestingHBaseCluster.create(TestingHBaseClusterOption.builder().conf(conf).build()); | ||
cluster.start(); | ||
} | ||
|
||
@AfterClass | ||
public static void afterClass() throws Exception { | ||
cluster.stop(); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
sourceTable = TableName.valueOf("table-" + useBulkLoad); | ||
targetTable = TableName.valueOf("another-table-" + useBulkLoad); | ||
allTables = Arrays.asList(sourceTable, targetTable); | ||
createTable(sourceTable); | ||
createTable(targetTable); | ||
} | ||
|
||
@Test | ||
public void testModificationsOnTable() throws Exception { | ||
Instant timestamp = Instant.now(); | ||
|
||
// load some data | ||
load(sourceTable, timestamp, "data"); | ||
|
||
String backupId = backup(FULL, allTables); | ||
BackupInfo backupInfo = verifyBackup(backupId, FULL, COMPLETE); | ||
assertTrue(backupInfo.getTables().contains(sourceTable)); | ||
|
||
restore(backupId, sourceTable, targetTable); | ||
validateDataEquals(sourceTable, "data"); | ||
validateDataEquals(targetTable, "data"); | ||
|
||
// load new data on the same timestamp | ||
load(sourceTable, timestamp, "changed_data"); | ||
|
||
backupId = backup(FULL, allTables); | ||
backupInfo = verifyBackup(backupId, FULL, COMPLETE); | ||
assertTrue(backupInfo.getTables().contains(sourceTable)); | ||
|
||
restore(backupId, sourceTable, targetTable); | ||
validateDataEquals(sourceTable, "changed_data"); | ||
validateDataEquals(targetTable, "changed_data"); | ||
} | ||
|
||
private void createTable(TableName tableName) throws IOException { | ||
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName) | ||
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(COLUMN_FAMILY)); | ||
try (Connection connection = ConnectionFactory.createConnection(cluster.getConf()); | ||
Admin admin = connection.getAdmin()) { | ||
admin.createTable(builder.build()); | ||
} | ||
} | ||
|
||
private void load(TableName tableName, Instant timestamp, String data) throws IOException { | ||
if (useBulkLoad) { | ||
hFileBulkLoad(tableName, timestamp, data); | ||
} else { | ||
putLoad(tableName, timestamp, data); | ||
} | ||
} | ||
|
||
private void putLoad(TableName tableName, Instant timestamp, String data) throws IOException { | ||
LOG.info("Writing new data to HBase using normal Puts: {}", data); | ||
try (Connection connection = ConnectionFactory.createConnection(cluster.getConf())) { | ||
Table table = connection.getTable(sourceTable); | ||
List<Put> puts = new ArrayList<>(); | ||
for (int i = 0; i < 10; i++) { | ||
Put put = new Put(Bytes.toBytes(i), timestamp.toEpochMilli()); | ||
put.addColumn(COLUMN_FAMILY, Bytes.toBytes("data"), Bytes.toBytes(data)); | ||
puts.add(put); | ||
|
||
if (i % 100 == 0) { | ||
table.put(puts); | ||
puts.clear(); | ||
} | ||
} | ||
if (!puts.isEmpty()) { | ||
table.put(puts); | ||
} | ||
connection.getAdmin().flush(tableName); | ||
} | ||
} | ||
|
||
private void hFileBulkLoad(TableName tableName, Instant timestamp, String data) | ||
throws IOException { | ||
FileSystem fs = FileSystem.get(cluster.getConf()); | ||
LOG.info("Writing new data to HBase using BulkLoad: {}", data); | ||
// HFiles require this strict directory structure to allow to load them | ||
Path hFileRootPath = new Path("/tmp/hfiles_" + UUID.randomUUID()); | ||
fs.mkdirs(hFileRootPath); | ||
Path hFileFamilyPath = new Path(hFileRootPath, Bytes.toString(COLUMN_FAMILY)); | ||
fs.mkdirs(hFileFamilyPath); | ||
try (HFile.Writer writer = HFile.getWriterFactoryNoCache(cluster.getConf()) | ||
.withPath(fs, new Path(hFileFamilyPath, "hfile_" + UUID.randomUUID())) | ||
.withFileContext(new HFileContextBuilder().withTableName(tableName.toBytes()) | ||
.withColumnFamily(COLUMN_FAMILY).build()) | ||
.create()) { | ||
for (int i = 0; i < 10; i++) { | ||
writer.append(new KeyValue(Bytes.toBytes(i), COLUMN_FAMILY, Bytes.toBytes("data"), | ||
timestamp.toEpochMilli(), Bytes.toBytes(data))); | ||
} | ||
} | ||
Map<BulkLoadHFiles.LoadQueueItem, ByteBuffer> result = | ||
BulkLoadHFiles.create(cluster.getConf()).bulkLoad(tableName, hFileRootPath); | ||
assertFalse(result.isEmpty()); | ||
} | ||
|
||
private String backup(BackupType backupType, List<TableName> tables) throws IOException { | ||
LOG.info("Creating the backup ..."); | ||
|
||
try (Connection connection = ConnectionFactory.createConnection(cluster.getConf()); | ||
BackupAdmin backupAdmin = new BackupAdminImpl(connection)) { | ||
BackupRequest backupRequest = | ||
new BackupRequest.Builder().withTargetRootDir(BACKUP_ROOT_DIR.toString()) | ||
.withTableList(new ArrayList<>(tables)).withBackupType(backupType).build(); | ||
return backupAdmin.backupTables(backupRequest); | ||
} | ||
|
||
} | ||
|
||
private void restore(String backupId, TableName sourceTableName, TableName targetTableName) | ||
throws IOException { | ||
LOG.info("Restoring data ..."); | ||
try (Connection connection = ConnectionFactory.createConnection(cluster.getConf()); | ||
BackupAdmin backupAdmin = new BackupAdminImpl(connection)) { | ||
RestoreRequest restoreRequest = new RestoreRequest.Builder().withBackupId(backupId) | ||
.withBackupRootDir(BACKUP_ROOT_DIR.toString()).withOvewrite(true) | ||
.withFromTables(new TableName[] { sourceTableName }) | ||
.withToTables(new TableName[] { targetTableName }).build(); | ||
backupAdmin.restore(restoreRequest); | ||
} | ||
} | ||
|
||
private void validateDataEquals(TableName tableName, String expectedData) throws IOException { | ||
try (Connection connection = ConnectionFactory.createConnection(cluster.getConf()); | ||
Table table = connection.getTable(tableName)) { | ||
Scan scan = new Scan(); | ||
scan.readAllVersions(); | ||
scan.setRaw(true); | ||
scan.setBatch(100); | ||
|
||
for (Result sourceResult : table.getScanner(scan)) { | ||
List<Cell> sourceCells = sourceResult.listCells(); | ||
for (Cell cell : sourceCells) { | ||
assertEquals(expectedData, Bytes.toStringBinary(cell.getValueArray(), | ||
cell.getValueOffset(), cell.getValueLength())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private BackupInfo verifyBackup(String backupId, BackupType expectedType, | ||
BackupInfo.BackupState expectedState) throws IOException { | ||
try (Connection connection = ConnectionFactory.createConnection(cluster.getConf()); | ||
BackupAdmin backupAdmin = new BackupAdminImpl(connection)) { | ||
BackupInfo backupInfo = backupAdmin.getBackupInfo(backupId); | ||
|
||
// Verify managed backup in HBase | ||
assertEquals(backupId, backupInfo.getBackupId()); | ||
assertEquals(expectedState, backupInfo.getState()); | ||
assertEquals(expectedType, backupInfo.getType()); | ||
return backupInfo; | ||
} | ||
} | ||
|
||
private static void enableBackup(Configuration conf) { | ||
// Enable backup | ||
conf.setBoolean(BackupRestoreConstants.BACKUP_ENABLE_KEY, true); | ||
BackupManager.decorateMasterConfiguration(conf); | ||
BackupManager.decorateRegionServerConfiguration(conf); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,15 +21,18 @@ | |
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.OptionalLong; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FileStatus; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.fs.PathFilter; | ||
import org.apache.hadoop.hbase.Cell; | ||
import org.apache.hadoop.hbase.PrivateCellUtil; | ||
import org.apache.hadoop.hbase.io.hfile.HFile; | ||
import org.apache.hadoop.hbase.io.hfile.HFile.Reader; | ||
import org.apache.hadoop.hbase.io.hfile.HFileScanner; | ||
import org.apache.hadoop.hbase.regionserver.StoreFileInfo; | ||
import org.apache.hadoop.io.NullWritable; | ||
import org.apache.hadoop.mapreduce.InputSplit; | ||
import org.apache.hadoop.mapreduce.JobContext; | ||
|
@@ -78,6 +81,7 @@ private static class HFileRecordReader extends RecordReader<NullWritable, Cell> | |
private Cell value = null; | ||
private long count; | ||
private boolean seeked = false; | ||
private OptionalLong bulkloadSeqId; | ||
|
||
@Override | ||
public void initialize(InputSplit split, TaskAttemptContext context) | ||
|
@@ -88,6 +92,7 @@ public void initialize(InputSplit split, TaskAttemptContext context) | |
FileSystem fs = path.getFileSystem(conf); | ||
LOG.info("Initialize HFileRecordReader for {}", path); | ||
this.in = HFile.createReader(fs, path, conf); | ||
this.bulkloadSeqId = StoreFileInfo.getBulkloadSeqId(path); | ||
|
||
// The file info must be loaded before the scanner can be used. | ||
// This seems like a bug in HBase, but it's easily worked around. | ||
|
@@ -109,6 +114,9 @@ public boolean nextKeyValue() throws IOException, InterruptedException { | |
return false; | ||
} | ||
value = scanner.getCell(); | ||
if (value != null && bulkloadSeqId.isPresent()) { | ||
PrivateCellUtil.setSequenceId(value, bulkloadSeqId.getAsLong()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can not use bulkloadSeqId.ifPresent because PrivateCellUtil.setSequenceId will throw exceptions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea setSequenceId throws IOException. Figured it looks cleaner this way |
||
} | ||
count++; | ||
return true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,10 +162,10 @@ protected static byte[] combineTableNameSuffix(byte[] tableName, byte[] suffix) | |
|
||
/** | ||
* ExtendedCell and ExtendedCellSerialization are InterfaceAudience.Private. We expose this config | ||
* package-private for internal usage for jobs like WALPlayer which need to use features of | ||
* ExtendedCell. | ||
* for internal usage in jobs like WALPlayer which need to use features of ExtendedCell. | ||
*/ | ||
static final String EXTENDED_CELL_SERIALIZATION_ENABLED_KEY = | ||
@InterfaceAudience.Private | ||
public static final String EXTENDED_CELL_SERIALIZATION_ENABLED_KEY = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field is becoming part of the public API. It's worth updating the comment about its usage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ndimiduk I decided to annotate that as IA.Private. I know we generally prefer not to do that for fields/methods, but for better or worse there's already a strong convention of doing it in this class (11 methods annotated IA.Private). It's probably worth a larger refactor/cleanup of HFileOutputFormat2 Let me know if you strongly disagree with this approach There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good by me. |
||
"hbase.mapreduce.hfileoutputformat.extendedcell.enabled"; | ||
static final boolean EXTENDED_CELL_SERIALIZATION_ENABLED_DEFULT = false; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid star imports.