Skip to content

Commit 4a85bb8

Browse files
rmdmattinglyRay Mattingly
authored andcommitted
HBASE-28686 MapReduceBackupCopyJob should support custom DistCp options (#6017)
Co-authored-by: Ray Mattingly <rmattingly@hubspot.com> Signed-off-by: Duo Zhang <zhangduo@apache.org> Signed-off-by: Nick Dimiduk <ndimiduk@apache.org>
1 parent 0865bd6 commit 4a85bb8

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.InvocationTargetException;
2323
import java.lang.reflect.Method;
2424
import java.math.BigDecimal;
25+
import java.util.ArrayList;
2526
import java.util.Arrays;
2627
import java.util.List;
2728
import java.util.Objects;
@@ -45,6 +46,7 @@
4546
import org.apache.hadoop.tools.CopyListingFileStatus;
4647
import org.apache.hadoop.tools.DistCp;
4748
import org.apache.hadoop.tools.DistCpConstants;
49+
import org.apache.hadoop.tools.DistCpOptionSwitch;
4850
import org.apache.hadoop.tools.DistCpOptions;
4951
import org.apache.yetus.audience.InterfaceAudience;
5052
import org.slf4j.Logger;
@@ -58,6 +60,10 @@
5860
@InterfaceAudience.Private
5961
public class MapReduceBackupCopyJob implements BackupCopyJob {
6062
public static final String NUMBER_OF_LEVELS_TO_PRESERVE_KEY = "num.levels.preserve";
63+
64+
// This prefix specifies the DistCp options to be used during backup copy
65+
public static final String BACKUP_COPY_OPTION_PREFIX = "hbase.backup.copy.";
66+
6167
private static final Logger LOG = LoggerFactory.getLogger(MapReduceBackupCopyJob.class);
6268

6369
private Configuration conf;
@@ -394,7 +400,15 @@ public int copy(BackupInfo context, BackupManager backupManager, Configuration c
394400
if (!destfs.exists(dest)) {
395401
destfs.mkdirs(dest);
396402
}
397-
res = distcp.run(newOptions);
403+
404+
List<String> distCpOptionsFromConf = parseDistCpOptions(conf);
405+
String[] finalOptions = new String[newOptions.length + distCpOptionsFromConf.size()];
406+
for (int i = 0; i < distCpOptionsFromConf.size(); i++) {
407+
finalOptions[i] = distCpOptionsFromConf.get(i);
408+
}
409+
System.arraycopy(newOptions, 0, finalOptions, distCpOptionsFromConf.size(),
410+
newOptions.length);
411+
res = distcp.run(finalOptions);
398412
}
399413
return res;
400414

@@ -425,4 +439,25 @@ public void cancel(String jobId) throws IOException {
425439
}
426440
}
427441

442+
protected static List<String> parseDistCpOptions(Configuration conf) {
443+
List<String> extraArgsFromConf = new ArrayList<>();
444+
445+
for (DistCpOptionSwitch optionSwitch : DistCpOptionSwitch.values()) {
446+
String configLabel = BACKUP_COPY_OPTION_PREFIX + optionSwitch.getConfigLabel();
447+
if (conf.get(configLabel) != null) {
448+
if (optionSwitch.getOption().hasArg()) {
449+
extraArgsFromConf.add("-" + optionSwitch.getOption().getOpt());
450+
extraArgsFromConf.add(conf.get(configLabel));
451+
} else {
452+
boolean value = conf.getBoolean(configLabel, false);
453+
if (value) {
454+
extraArgsFromConf.add("-" + optionSwitch.getOption().getOpt());
455+
}
456+
}
457+
}
458+
}
459+
460+
return extraArgsFromConf;
461+
}
462+
428463
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.backup.mapreduce;
19+
20+
import static org.apache.hadoop.hbase.backup.mapreduce.MapReduceBackupCopyJob.BACKUP_COPY_OPTION_PREFIX;
21+
import static org.apache.hadoop.tools.DistCpConstants.CONF_LABEL_DIRECT_WRITE;
22+
import static org.apache.hadoop.tools.DistCpConstants.CONF_LABEL_MAX_MAPS;
23+
import static org.junit.Assert.assertEquals;
24+
25+
import java.util.List;
26+
import org.apache.hadoop.conf.Configuration;
27+
import org.apache.hadoop.hbase.HBaseClassTestRule;
28+
import org.apache.hadoop.hbase.testclassification.SmallTests;
29+
import org.junit.ClassRule;
30+
import org.junit.Test;
31+
import org.junit.experimental.categories.Category;
32+
33+
import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
34+
35+
@Category(SmallTests.class)
36+
public class TestMapReduceBackupCopyJob {
37+
38+
@ClassRule
39+
public static final HBaseClassTestRule CLASS_RULE =
40+
HBaseClassTestRule.forClass(TestMapReduceBackupCopyJob.class);
41+
42+
@Test
43+
public void testDistCpOptionParsing() {
44+
Configuration conf = new Configuration();
45+
conf.setInt(BACKUP_COPY_OPTION_PREFIX + CONF_LABEL_MAX_MAPS, 1000);
46+
conf.setBoolean(BACKUP_COPY_OPTION_PREFIX + CONF_LABEL_DIRECT_WRITE, true);
47+
List<String> args = MapReduceBackupCopyJob.parseDistCpOptions(conf);
48+
49+
List<String> expectedArgs =
50+
ImmutableList.<String> builder().add("-m", "1000").add("-direct").build();
51+
52+
assertEquals(args, expectedArgs);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)