Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
Expand Down Expand Up @@ -99,16 +96,16 @@ protected WALPlayer(final Configuration c) {
* {@link CellSortReducer}
*/
static class WALKeyValueMapper extends Mapper<WALKey, WALEdit, WritableComparable<?>, Cell> {
private Set<String> tableSet = new HashSet<String>();
private final Map<TableName, TableName> tables = new TreeMap<>();
private boolean multiTableSupport = false;
private boolean diskBasedSortingEnabled = false;

@Override
public void map(WALKey key, WALEdit value, Context context) throws IOException {
try {
// skip all other tables
TableName table = key.getTableName();
if (tableSet.contains(table.getNameAsString())) {
TableName targetTable = tables.get(key.getTableName());
if (targetTable != null) {
for (Cell cell : value.getCells()) {
if (WALEdit.isMetaEditFamily(cell)) {
continue;
Expand All @@ -121,7 +118,8 @@ public void map(WALKey key, WALEdit value, Context context) throws IOException {
PrivateCellUtil.setSequenceId(cell, key.getSequenceId());

byte[] outKey = multiTableSupport
? Bytes.add(table.getName(), Bytes.toBytes(tableSeparator), CellUtil.cloneRow(cell))
? Bytes.add(targetTable.getName(), Bytes.toBytes(tableSeparator),
CellUtil.cloneRow(cell))
: CellUtil.cloneRow(cell);
ExtendedCell extendedCell = PrivateCellUtil.ensureExtendedCell(cell);
context.write(wrapKey(outKey, extendedCell), new MapReduceExtendedCell(extendedCell));
Expand All @@ -136,10 +134,19 @@ public void map(WALKey key, WALEdit value, Context context) throws IOException {
@Override
public void setup(Context context) throws IOException {
Configuration conf = context.getConfiguration();
String[] tables = conf.getStrings(TABLES_KEY);
String[] tableMap = conf.getStrings(TABLE_MAP_KEY);
String[] tablesToUse = conf.getStrings(TABLES_KEY);
if (tableMap == null) {
tableMap = tablesToUse;
}
if (tablesToUse.length != tableMap.length) {
throw new IOException("Incorrect table mapping specified.");
}
for (int i = 0; i < tablesToUse.length; i++) {
tables.put(TableName.valueOf(tablesToUse[i]), TableName.valueOf(tableMap[i]));
}
this.multiTableSupport = conf.getBoolean(MULTI_TABLES_SUPPORT, false);
this.diskBasedSortingEnabled = HFileOutputFormat2.diskBasedSortingEnabled(conf);
Collections.addAll(tableSet, tables);
}

private WritableComparable<?> wrapKey(byte[] key, ExtendedCell cell) {
Expand Down Expand Up @@ -349,7 +356,7 @@ public Job createSubmittableJob(String[] args) throws IOException {
true);

// the bulk HFile case
List<TableName> tableNames = getTableNameList(tables);
List<TableName> tableNames = getTableNameList(tableMap);

job.setMapperClass(WALKeyValueMapper.class);
if (diskBasedSortingEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,31 @@ public void testPlayingRecoveredEdit() throws Exception {
/**
* Tests that when you write multiple cells with the same timestamp they are properly sorted by
* their sequenceId in WALPlayer/CellSortReducer so that the correct one wins when querying from
* the resulting bulkloaded HFiles. See HBASE-27649
* the resulting bulkloaded HFiles. Also verifies that bulk output honors source-to-target table
* mappings. See HBASE-27649 and HBASE-30288.
*/
@Test
public void testWALPlayerBulkLoadWithOverriddenTimestamps(TestInfo testInfo) throws Exception {
final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName() + "1");
public void testWALPlayerBulkLoadWithTableMappingAndOverriddenTimestamps(TestInfo testInfo)
throws Exception {
final TableName sourceTableName =
TableName.valueOf(testInfo.getTestMethod().get().getName() + "Source");
final TableName targetTableName =
TableName.valueOf(testInfo.getTestMethod().get().getName() + "Target");
final byte[] family = Bytes.toBytes("family");
final byte[] column1 = Bytes.toBytes("c1");
final byte[] column2 = Bytes.toBytes("c2");
final byte[] row = Bytes.toBytes("row");
final Table table = TEST_UTIL.createTable(tableName, family);
final Table sourceTable = TEST_UTIL.createTable(sourceTableName, family);
final Table targetTable =
TEST_UTIL.createTable(targetTableName, family, new byte[][] { Bytes.toBytes("row0") });

long now = EnvironmentEdgeManager.currentTime();
// put a row into the first table
Put p = new Put(row);
p.addColumn(family, column1, now, column1);
p.addColumn(family, column2, now, column2);

table.put(p);
sourceTable.put(p);

byte[] lastVal = null;

Expand All @@ -163,7 +170,7 @@ public void testWALPlayerBulkLoadWithOverriddenTimestamps(TestInfo testInfo) thr
p = new Put(row);
p.addColumn(family, column1, now, lastVal);

table.put(p);
sourceTable.put(p);

// wal rolling is necessary to trigger the bug. otherwise no sorting
// needs to occur in the reducer because it's all sorted and coming from a single file.
Expand All @@ -187,24 +194,24 @@ public void testWALPlayerBulkLoadWithOverriddenTimestamps(TestInfo testInfo) thr
final byte[] finalLastVal = lastVal;

runWithDiskBasedSortingDisabledAndEnabled(() -> {
assertEquals(0, ToolRunner.run(configuration, player,
new String[] { walInputDir, tableName.getNameAsString() }));
assertEquals(0, ToolRunner.run(configuration, player, new String[] { walInputDir,
sourceTableName.getNameAsString(), targetTableName.getNameAsString() }));

Get g = new Get(row);
Result result = table.get(g);
Result result = sourceTable.get(g);
byte[] value = CellUtil.cloneValue(result.getColumnLatestCell(family, column1));
assertThat(Bytes.toStringBinary(value), equalTo(Bytes.toStringBinary(finalLastVal)));

TEST_UTIL.truncateTable(tableName);
TEST_UTIL.truncateTable(targetTableName);
g = new Get(row);
result = table.get(g);
result = targetTable.get(g);
assertThat(result.listCells(), nullValue());

BulkLoadHFiles.create(configuration).bulkLoad(tableName,
new Path(outPath, tableName.getNamespaceAsString() + "/" + tableName.getNameAsString()));
BulkLoadHFiles.create(configuration).bulkLoad(targetTableName, new Path(outPath,
targetTableName.getNamespaceAsString() + "/" + targetTableName.getNameAsString()));

g = new Get(row);
result = table.get(g);
result = targetTable.get(g);
value = CellUtil.cloneValue(result.getColumnLatestCell(family, column1));

assertThat(result.listCells(), notNullValue());
Expand Down