Skip to content

Commit eaa6721

Browse files
committed
more enhancements
1 parent 286c8d0 commit eaa6721

3 files changed

Lines changed: 54 additions & 13 deletions

File tree

hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/mapreduce/MapReduceBackupMergeJob.java

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

2020
import static org.apache.hadoop.hbase.backup.util.BackupUtils.succeeded;
2121

22+
import java.io.FileNotFoundException;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.io.OutputStream;
@@ -343,6 +344,11 @@ protected void moveData(FileSystem fs, String backupRoot, Path bulkOutputPath,
343344
Path dest =
344345
new Path(HBackupFileSystem.getTableBackupDir(backupRoot, mergedBackupId, tableName));
345346

347+
// Delete all *data* files in dest
348+
if (!deleteData(fs, dest)) {
349+
throw new IOException("Could not delete " + dest);
350+
}
351+
346352
FileStatus[] fsts = fs.listStatus(bulkOutputPath);
347353
for (FileStatus fst : fsts) {
348354
if (fst.isDirectory()) {
@@ -361,6 +367,39 @@ protected void moveData(FileSystem fs, String backupRoot, Path bulkOutputPath,
361367
}
362368
}
363369

370+
/**
371+
* Deletes only data files and keeps all META
372+
* @param fs file system instance
373+
* @param dest destination location
374+
* @return true, if success, false - otherwise
375+
* @throws FileNotFoundException exception
376+
* @throws IOException exception
377+
*/
378+
private boolean deleteData(FileSystem fs, Path dest) throws FileNotFoundException, IOException {
379+
RemoteIterator<LocatedFileStatus> it = fs.listFiles(dest, true);
380+
List<Path> toDelete = new ArrayList<Path>();
381+
while (it.hasNext()) {
382+
Path p = it.next().getPath();
383+
if (fs.isDirectory(p)) {
384+
continue;
385+
}
386+
// Keep meta
387+
String fileName = p.toString();
388+
if (fileName.indexOf(FSTableDescriptors.TABLEINFO_DIR) > 0 ||
389+
fileName.indexOf(HRegionFileSystem.REGION_INFO_FILE) > 0) {
390+
continue;
391+
}
392+
toDelete.add(p);
393+
}
394+
for (Path p : toDelete) {
395+
boolean result = fs.delete(p, false);
396+
if (!result) {
397+
return false;
398+
}
399+
}
400+
return true;
401+
}
402+
364403
protected TableName[] getTableNamesInBackupImages(String[] backupIds) throws IOException {
365404
Set<TableName> allSet = new HashSet<>();
366405

hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException {
264264
} else {
265265
tableNameBytes = Bytes.toBytes(writeTableNames);
266266
}
267+
String tableName = Bytes.toString(tableNameBytes);
267268
Path tableRelPath = getTableRelativePath(tableNameBytes);
268269
byte[] tableAndFamily = getTableNameSuffixedWithFamily(tableNameBytes, family);
269270

@@ -294,7 +295,6 @@ public void write(ImmutableBytesWritable row, V cell) throws IOException {
294295
if (conf.getBoolean(LOCALITY_SENSITIVE_CONF_KEY, DEFAULT_LOCALITY_SENSITIVE)) {
295296
HRegionLocation loc = null;
296297

297-
String tableName = Bytes.toString(tableNameBytes);
298298
if (tableName != null) {
299299
try (
300300
Connection connection =

hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/WALPlayer.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,22 @@ public void setup(Context context) throws IOException {
143143
* A mapper that just writes out Cells. This one can be used together with {@link CellSortReducer}
144144
*/
145145
static class WALCellMapper extends Mapper<WALKey, WALEdit, ImmutableBytesWritable, Cell> {
146-
private byte[] table;
146+
private Set<String> tableSet = new HashSet<>();
147+
private boolean multiTableSupport = false;
147148

148149
@Override
149150
public void map(WALKey key, WALEdit value, Context context) throws IOException {
150151
try {
151-
// skip all other tables
152-
if (Bytes.equals(table, key.getTableName().getName())) {
152+
TableName table = key.getTableName();
153+
if (tableSet.contains(table.getNameAsString())) {
153154
for (Cell cell : value.getCells()) {
154155
if (WALEdit.isMetaEditFamily(cell)) {
155156
continue;
156157
}
157-
context.write(new ImmutableBytesWritable(CellUtil.cloneRow(cell)),
158-
new MapReduceExtendedCell(cell));
158+
byte[] outKey = multiTableSupport
159+
? Bytes.add(table.getName(), Bytes.toBytes(tableSeparator), CellUtil.cloneRow(cell))
160+
: CellUtil.cloneRow(cell);
161+
context.write(new ImmutableBytesWritable(outKey), new MapReduceExtendedCell(cell));
159162
}
160163
}
161164
} catch (InterruptedException e) {
@@ -165,13 +168,12 @@ public void map(WALKey key, WALEdit value, Context context) throws IOException {
165168

166169
@Override
167170
public void setup(Context context) throws IOException {
168-
// only a single table is supported when HFiles are generated with HFileOutputFormat
169-
String[] tables = context.getConfiguration().getStrings(TABLES_KEY);
170-
if (tables == null || tables.length != 1) {
171-
// this can only happen when WALMapper is used directly by a class other than WALPlayer
172-
throw new IOException("Exactly one table must be specified for bulk HFile case.");
171+
Configuration conf = context.getConfiguration();
172+
String[] tables = conf.getStrings(TABLES_KEY);
173+
this.multiTableSupport = conf.getBoolean(MULTI_TABLES_SUPPORT, false);
174+
for (String table : tables) {
175+
tableSet.add(table);
173176
}
174-
table = Bytes.toBytes(tables[0]);
175177
}
176178
}
177179

@@ -363,7 +365,7 @@ public Job createSubmittableJob(String[] args) throws IOException {
363365
FileOutputFormat.setOutputPath(job, outputDir);
364366
job.setMapOutputValueClass(KeyValue.class);
365367
try (Connection conn = ConnectionFactory.createConnection(conf);) {
366-
List<TableInfo> tableInfoList = new ArrayList<TableInfo>();
368+
List<TableInfo> tableInfoList = new ArrayList<>();
367369
for (TableName tableName : tableNames) {
368370
Table table = conn.getTable(tableName);
369371
RegionLocator regionLocator = conn.getRegionLocator(tableName);

0 commit comments

Comments
 (0)