-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHADOOP-1540.patch
1049 lines (1017 loc) · 40.5 KB
/
HADOOP-1540.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/CopyFilter.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/CopyFilter.java
new file mode 100644
index 0000000..3da364c
--- /dev/null
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/CopyFilter.java
@@ -0,0 +1,60 @@
+/**
+ * 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.tools;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+
+/**
+ * Interface for excluding files from DistCp.
+ *
+ */
+public abstract class CopyFilter {
+
+ /**
+ * Default initialize method does nothing.
+ */
+ public void initialize() {}
+
+ /**
+ * Predicate to determine if a file can be excluded from copy.
+ *
+ * @param path a Path to be considered for copying
+ * @return boolean, true to copy, false to exclude
+ */
+ public abstract boolean shouldCopy(Path path);
+
+ /**
+ * Public factory method which returns the appropriate implementation of
+ * CopyFilter.
+ *
+ * @param conf DistCp configuratoin
+ * @return An instance of the appropriate CopyFilter
+ */
+ public static CopyFilter getCopyFilter(Configuration conf) {
+ String filtersFilename = conf.get(DistCpConstants.CONF_LABEL_FILTERS_FILE);
+
+ if (filtersFilename == null) {
+ return new TrueCopyFilter();
+ } else {
+ String filterFilename = conf.get(
+ DistCpConstants.CONF_LABEL_FILTERS_FILE);
+ return new RegexCopyFilter(filterFilename);
+ }
+ }
+}
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpConstants.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpConstants.java
index 7ecb6ce..21dca62 100644
--- a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpConstants.java
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpConstants.java
@@ -59,7 +59,8 @@
public static final String CONF_LABEL_APPEND = "distcp.copy.append";
public static final String CONF_LABEL_DIFF = "distcp.copy.diff";
public static final String CONF_LABEL_BANDWIDTH_MB = "distcp.map.bandwidth.mb";
-
+ public static final String CONF_LABEL_FILTERS_FILE =
+ "distcp.filters.file";
public static final String CONF_LABEL_MAX_CHUNKS_TOLERABLE =
"distcp.dynamic.max.chunks.tolerable";
public static final String CONF_LABEL_MAX_CHUNKS_IDEAL =
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpOptionSwitch.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpOptionSwitch.java
index f90319d..ed4a0b2 100644
--- a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpOptionSwitch.java
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpOptionSwitch.java
@@ -177,7 +177,16 @@
* Specify bandwidth per map in MB
*/
BANDWIDTH(DistCpConstants.CONF_LABEL_BANDWIDTH_MB,
- new Option("bandwidth", true, "Specify bandwidth per map in MB"));
+ new Option("bandwidth", true, "Specify bandwidth per map in MB")),
+
+ /**
+ * Path containing a list of strings, which when found in the path of
+ * a file to be copied excludes that file from the copy job.
+ */
+ FILTERS(DistCpConstants.CONF_LABEL_FILTERS_FILE,
+ new Option("filters", true, "The path to a file containing a list of"
+ + " strings for paths to be excluded from the copy."));
+
public static final String PRESERVE_STATUS_DEFAULT = "-prbugpct";
private final String confLabel;
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpOptions.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpOptions.java
index d8f3ff7..302b626 100644
--- a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpOptions.java
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/DistCpOptions.java
@@ -69,7 +69,12 @@
private Path targetPath;
- // targetPathExist is a derived field, it's initialized in the
+ /**
+ * The path to a file containing a list of paths to filter out of the copy.
+ */
+ private String filtersFile;
+
+ // targetPathExist is a derived field, it's initialized in the
// beginning of distcp.
private boolean targetPathExists = true;
@@ -139,6 +144,7 @@ public DistCpOptions(DistCpOptions that) {
this.sourcePaths = that.getSourcePaths();
this.targetPath = that.getTargetPath();
this.targetPathExists = that.getTargetPathExists();
+ this.filtersFile = that.getFiltersFile();
}
}
@@ -549,6 +555,23 @@ public boolean setTargetPathExists(boolean targetPathExists) {
return this.targetPathExists = targetPathExists;
}
+ /**
+ * File path that contains the list of patterns
+ * for paths to be filtered from the file copy.
+ * @return - Filter file path.
+ */
+ public final String getFiltersFile() {
+ return filtersFile;
+ }
+
+ /**
+ * Set filtersFile.
+ * @param filtersFilename The path to a list of patterns to exclude from copy.
+ */
+ public final void setFiltersFile(String filtersFilename) {
+ this.filtersFile = filtersFilename;
+ }
+
public void validate(DistCpOptionSwitch option, boolean value) {
boolean syncFolder = (option == DistCpOptionSwitch.SYNC_FOLDERS ?
@@ -623,6 +646,10 @@ public void appendToConf(Configuration conf) {
String.valueOf(mapBandwidth));
DistCpOptionSwitch.addToConf(conf, DistCpOptionSwitch.PRESERVE_STATUS,
DistCpUtils.packAttributes(preserveStatus));
+ if (filtersFile != null) {
+ DistCpOptionSwitch.addToConf(conf, DistCpOptionSwitch.FILTERS,
+ filtersFile);
+ }
}
/**
@@ -645,6 +672,7 @@ public String toString() {
", targetPath=" + targetPath +
", targetPathExists=" + targetPathExists +
", preserveRawXattrs=" + preserveRawXattrs +
+ ", filtersFile='" + filtersFile + '\'' +
'}';
}
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/OptionsParser.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/OptionsParser.java
index 1729479..37add1e 100644
--- a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/OptionsParser.java
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/OptionsParser.java
@@ -86,37 +86,7 @@ public static DistCpOptions parse(String args[]) throws IllegalArgumentException
Arrays.toString(args), e);
}
- DistCpOptions option;
- Path targetPath;
- List<Path> sourcePaths = new ArrayList<Path>();
-
- String leftOverArgs[] = command.getArgs();
- if (leftOverArgs == null || leftOverArgs.length < 1) {
- throw new IllegalArgumentException("Target path not specified");
- }
-
- //Last Argument is the target path
- targetPath = new Path(leftOverArgs[leftOverArgs.length -1].trim());
-
- //Copy any source paths in the arguments to the list
- for (int index = 0; index < leftOverArgs.length - 1; index++) {
- sourcePaths.add(new Path(leftOverArgs[index].trim()));
- }
-
- /* If command has source file listing, use it else, fall back on source paths in args
- If both are present, throw exception and bail */
- if (command.hasOption(DistCpOptionSwitch.SOURCE_FILE_LISTING.getSwitch())) {
- if (!sourcePaths.isEmpty()) {
- throw new IllegalArgumentException("Both source file listing and source paths present");
- }
- option = new DistCpOptions(new Path(getVal(command, DistCpOptionSwitch.
- SOURCE_FILE_LISTING.getSwitch())), targetPath);
- } else {
- if (sourcePaths.isEmpty()) {
- throw new IllegalArgumentException("Neither source file listing nor source paths present");
- }
- option = new DistCpOptions(sourcePaths, targetPath);
- }
+ DistCpOptions option = parseSourceAndTargetPaths(command);
//Process all the other option switches and set options appropriately
if (command.hasOption(DistCpOptionSwitch.IGNORE_FAILURES.getSwitch())) {
@@ -165,68 +135,23 @@ public static DistCpOptions parse(String args[]) throws IllegalArgumentException
option.setBlocking(false);
}
- if (command.hasOption(DistCpOptionSwitch.BANDWIDTH.getSwitch())) {
- try {
- Integer mapBandwidth = Integer.parseInt(
- getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()).trim());
- if (mapBandwidth.intValue() <= 0) {
- throw new IllegalArgumentException("Bandwidth specified is not positive: " +
- mapBandwidth);
- }
- option.setMapBandwidth(mapBandwidth);
- } catch (NumberFormatException e) {
- throw new IllegalArgumentException("Bandwidth specified is invalid: " +
- getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()), e);
- }
- }
+ parseBandwidth(command, option);
if (command.hasOption(DistCpOptionSwitch.SSL_CONF.getSwitch())) {
option.setSslConfigurationFile(command.
getOptionValue(DistCpOptionSwitch.SSL_CONF.getSwitch()));
}
- if (command.hasOption(DistCpOptionSwitch.NUM_LISTSTATUS_THREADS.getSwitch())) {
- try {
- Integer numThreads = Integer.parseInt(getVal(command,
- DistCpOptionSwitch.NUM_LISTSTATUS_THREADS.getSwitch()).trim());
- option.setNumListstatusThreads(numThreads);
- } catch (NumberFormatException e) {
- throw new IllegalArgumentException(
- "Number of liststatus threads is invalid: " + getVal(command,
- DistCpOptionSwitch.NUM_LISTSTATUS_THREADS.getSwitch()), e);
- }
- }
+ parseNumListStatusThreads(command, option);
- if (command.hasOption(DistCpOptionSwitch.MAX_MAPS.getSwitch())) {
- try {
- Integer maps = Integer.parseInt(
- getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()).trim());
- option.setMaxMaps(maps);
- } catch (NumberFormatException e) {
- throw new IllegalArgumentException("Number of maps is invalid: " +
- getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()), e);
- }
- }
+ parseMaxMaps(command, option);
if (command.hasOption(DistCpOptionSwitch.COPY_STRATEGY.getSwitch())) {
option.setCopyStrategy(
getVal(command, DistCpOptionSwitch.COPY_STRATEGY.getSwitch()));
}
- if (command.hasOption(DistCpOptionSwitch.PRESERVE_STATUS.getSwitch())) {
- String attributes =
- getVal(command, DistCpOptionSwitch.PRESERVE_STATUS.getSwitch());
- if (attributes == null || attributes.isEmpty()) {
- for (FileAttribute attribute : FileAttribute.values()) {
- option.preserve(attribute);
- }
- } else {
- for (int index = 0; index < attributes.length(); index++) {
- option.preserve(FileAttribute.
- getAttribute(attributes.charAt(index)));
- }
- }
- }
+ parsePreserveStatus(command, option);
if (command.hasOption(DistCpOptionSwitch.DIFF.getSwitch())) {
String[] snapshots = getVals(command, DistCpOptionSwitch.DIFF.getSwitch());
@@ -235,6 +160,47 @@ public static DistCpOptions parse(String args[]) throws IllegalArgumentException
option.setUseDiff(true, snapshots[0], snapshots[1]);
}
+ parseFileLimit(command);
+
+ parseSizeLimit(command);
+
+ if (command.hasOption(DistCpOptionSwitch.FILTERS.getSwitch())) {
+ option.setFiltersFile(getVal(command,
+ DistCpOptionSwitch.FILTERS.getSwitch()));
+ }
+
+ return option;
+ }
+
+ /**
+ * parseSizeLimit is a helper method for parsing the deprecated
+ * argument SIZE_LIMIT.
+ *
+ * @param command command line arguments
+ */
+ private static void parseSizeLimit(CommandLine command) {
+ if (command.hasOption(DistCpOptionSwitch.SIZE_LIMIT.getSwitch())) {
+ String sizeLimitString = getVal(command,
+ DistCpOptionSwitch.SIZE_LIMIT.getSwitch().trim());
+ try {
+ Long.parseLong(sizeLimitString);
+ }
+ catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Size-limit is invalid: "
+ + sizeLimitString, e);
+ }
+ LOG.warn(DistCpOptionSwitch.SIZE_LIMIT.getSwitch() + " is a deprecated" +
+ " option. Ignoring.");
+ }
+ }
+
+ /**
+ * parseFileLimit is a helper method for parsing the deprecated
+ * argument FILE_LIMIT.
+ *
+ * @param command command line arguments
+ */
+ private static void parseFileLimit(CommandLine command) {
if (command.hasOption(DistCpOptionSwitch.FILE_LIMIT.getSwitch())) {
String fileLimitString = getVal(command,
DistCpOptionSwitch.FILE_LIMIT.getSwitch().trim());
@@ -246,23 +212,144 @@ public static DistCpOptions parse(String args[]) throws IllegalArgumentException
+ fileLimitString, e);
}
LOG.warn(DistCpOptionSwitch.FILE_LIMIT.getSwitch() + " is a deprecated" +
- " option. Ignoring.");
+ " option. Ignoring.");
}
+ }
- if (command.hasOption(DistCpOptionSwitch.SIZE_LIMIT.getSwitch())) {
- String sizeLimitString = getVal(command,
- DistCpOptionSwitch.SIZE_LIMIT.getSwitch().trim());
+ /**
+ * parsePreserveStatus is a helper method for parsing PRESERVE_STATUS.
+ *
+ * @param command command line arguments
+ * @param option parsed distcp options
+ */
+ private static void parsePreserveStatus(CommandLine command,
+ DistCpOptions option) {
+ if (command.hasOption(DistCpOptionSwitch.PRESERVE_STATUS.getSwitch())) {
+ String attributes =
+ getVal(command, DistCpOptionSwitch.PRESERVE_STATUS.getSwitch());
+ if (attributes == null || attributes.isEmpty()) {
+ for (FileAttribute attribute : FileAttribute.values()) {
+ option.preserve(attribute);
+ }
+ } else {
+ for (int index = 0; index < attributes.length(); index++) {
+ option.preserve(FileAttribute.
+ getAttribute(attributes.charAt(index)));
+ }
+ }
+ }
+ }
+
+ /**
+ * parseMaxMaps is a helper method for parsing MAX_MAPS.
+ *
+ * @param command command line arguments
+ * @param option parsed distcp options
+ */
+ private static void parseMaxMaps(CommandLine command,
+ DistCpOptions option) {
+ if (command.hasOption(DistCpOptionSwitch.MAX_MAPS.getSwitch())) {
try {
- Long.parseLong(sizeLimitString);
+ Integer maps = Integer.parseInt(
+ getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()).trim());
+ option.setMaxMaps(maps);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Number of maps is invalid: " +
+ getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()), e);
}
- catch (NumberFormatException e) {
- throw new IllegalArgumentException("Size-limit is invalid: "
- + sizeLimitString, e);
+ }
+ }
+
+ /**
+ * parseNumListStatusThreads is a helper method for parsing
+ * NUM_LISTSTATUS_THREADS.
+ *
+ * @param command command line arguments
+ * @param option parsed distcp options
+ */
+ private static void parseNumListStatusThreads(CommandLine command,
+ DistCpOptions option) {
+ if (command.hasOption(
+ DistCpOptionSwitch.NUM_LISTSTATUS_THREADS.getSwitch())) {
+ try {
+ Integer numThreads = Integer.parseInt(getVal(command,
+ DistCpOptionSwitch.NUM_LISTSTATUS_THREADS.getSwitch()).trim());
+ option.setNumListstatusThreads(numThreads);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(
+ "Number of liststatus threads is invalid: " + getVal(command,
+ DistCpOptionSwitch.NUM_LISTSTATUS_THREADS.getSwitch()), e);
+ }
+ }
+ }
+
+ /**
+ * parseBandwidth is a helper method for parsing BANDWIDTH.
+ *
+ * @param command command line arguments
+ * @param option parsed distcp options
+ */
+ private static void parseBandwidth(CommandLine command,
+ DistCpOptions option) {
+ if (command.hasOption(DistCpOptionSwitch.BANDWIDTH.getSwitch())) {
+ try {
+ Integer mapBandwidth = Integer.parseInt(
+ getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()).trim());
+ if (mapBandwidth <= 0) {
+ throw new IllegalArgumentException("Bandwidth specified is not " +
+ "positive: " + mapBandwidth);
+ }
+ option.setMapBandwidth(mapBandwidth);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Bandwidth specified is invalid: " +
+ getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()), e);
}
- LOG.warn(DistCpOptionSwitch.SIZE_LIMIT.getSwitch() + " is a deprecated" +
- " option. Ignoring.");
}
+ }
+ /**
+ * parseSourceAndTargetPaths is a helper method for parsing the source
+ * and target paths.
+ *
+ * @param command command line arguments
+ * @return DistCpOptions
+ */
+ private static DistCpOptions parseSourceAndTargetPaths(
+ CommandLine command) {
+ DistCpOptions option;
+ Path targetPath;
+ List<Path> sourcePaths = new ArrayList<Path>();
+
+ String[] leftOverArgs = command.getArgs();
+ if (leftOverArgs == null || leftOverArgs.length < 1) {
+ throw new IllegalArgumentException("Target path not specified");
+ }
+
+ //Last Argument is the target path
+ targetPath = new Path(leftOverArgs[leftOverArgs.length - 1].trim());
+
+ //Copy any source paths in the arguments to the list
+ for (int index = 0; index < leftOverArgs.length - 1; index++) {
+ sourcePaths.add(new Path(leftOverArgs[index].trim()));
+ }
+
+ /* If command has source file listing, use it else, fall back on source
+ paths in args. If both are present, throw exception and bail */
+ if (command.hasOption(
+ DistCpOptionSwitch.SOURCE_FILE_LISTING.getSwitch())) {
+ if (!sourcePaths.isEmpty()) {
+ throw new IllegalArgumentException("Both source file listing and " +
+ "source paths present");
+ }
+ option = new DistCpOptions(new Path(getVal(command, DistCpOptionSwitch.
+ SOURCE_FILE_LISTING.getSwitch())), targetPath);
+ } else {
+ if (sourcePaths.isEmpty()) {
+ throw new IllegalArgumentException("Neither source file listing nor " +
+ "source paths present");
+ }
+ option = new DistCpOptions(sourcePaths, targetPath);
+ }
return option;
}
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/RegexCopyFilter.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/RegexCopyFilter.java
new file mode 100644
index 0000000..1c2b324
--- /dev/null
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/RegexCopyFilter.java
@@ -0,0 +1,98 @@
+/**
+ * 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.tools;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.fs.*;
+import org.apache.hadoop.io.IOUtils;
+
+import java.io.*;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A CopyFilter which compares Java Regex Patterns to each Path to determine
+ * whether a file should be copied.
+ */
+public class RegexCopyFilter extends CopyFilter {
+
+ private static final Log LOG = LogFactory.getLog(RegexCopyFilter.class);
+ private File filtersFile;
+ private List<Pattern> filters;
+
+ /**
+ * Constructor, sets up a File object to read filter patterns from and
+ * the List to store the patterns.
+ */
+ protected RegexCopyFilter(String filtersFilename) {
+ filtersFile = new File(filtersFilename);
+ filters = new ArrayList<>();
+ }
+
+ /**
+ * Loads a list of filter patterns for use in shouldCopy.
+ */
+ @Override
+ public void initialize() {
+ BufferedReader reader = null;
+ try {
+ InputStream is = new FileInputStream(filtersFile);
+ reader = new BufferedReader(new InputStreamReader(is,
+ Charset.forName("UTF-8")));
+ String line;
+ while ((line = reader.readLine()) != null) {
+ Pattern pattern = Pattern.compile(line);
+ filters.add(pattern);
+ }
+ } catch (FileNotFoundException notFound) {
+ LOG.error("Can't find filters file " + filtersFile);
+ } catch (IOException cantRead) {
+ LOG.error("An error occurred while attempting to read from " +
+ filtersFile);
+ } finally {
+ IOUtils.cleanup(LOG, reader);
+ }
+ }
+
+ /**
+ * Sets the list of filters to exclude files from copy.
+ * Simplifies testing of the filters feature.
+ *
+ * @param filtersList a list of Patterns to be excluded
+ */
+ @VisibleForTesting
+ protected final void setFilters(List<Pattern> filtersList) {
+ this.filters = filtersList;
+ }
+
+ @Override
+ public boolean shouldCopy(Path path) {
+ for (Pattern filter : filters) {
+ if (filter.matcher(path.toString()).matches()) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java
index 4ea1dc9..8f50913 100644
--- a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/SimpleCopyListing.java
@@ -58,6 +58,7 @@
private long totalBytesToCopy = 0;
private int numListstatusThreads = 1;
private final int maxRetries = 3;
+ private CopyFilter copyFilter;
/**
* Protected constructor, to initialize configuration.
@@ -71,6 +72,8 @@ protected SimpleCopyListing(Configuration configuration, Credentials credentials
numListstatusThreads = getConf().getInt(
DistCpConstants.CONF_LABEL_LISTSTATUS_THREADS,
DistCpConstants.DEFAULT_LISTSTATUS_THREADS);
+ copyFilter = CopyFilter.getCopyFilter(getConf());
+ copyFilter.initialize();
}
@VisibleForTesting
@@ -213,7 +216,7 @@ public void doBuildListing(SequenceFile.Writer fileListWriter,
preserveXAttrs && sourceStatus.isDirectory(),
preserveRawXAttrs && sourceStatus.isDirectory());
writeToFileListing(fileListWriter, sourceCopyListingStatus,
- sourcePathRoot, options);
+ sourcePathRoot);
if (sourceStatus.isDirectory()) {
if (LOG.isDebugEnabled()) {
@@ -264,11 +267,10 @@ private Path computeSourceRootPath(FileStatus sourceStatus,
* Provide an option to skip copy of a path, Allows for exclusion
* of files such as {@link org.apache.hadoop.mapreduce.lib.output.FileOutputCommitter#SUCCEEDED_FILE_NAME}
* @param path - Path being considered for copy while building the file listing
- * @param options - Input options passed during DistCp invocation
* @return - True if the path should be considered for copy, false otherwise
*/
- protected boolean shouldCopy(Path path, DistCpOptions options) {
- return true;
+ protected boolean shouldCopy(Path path) {
+ return copyFilter.shouldCopy(path);
}
/** {@inheritDoc} */
@@ -409,7 +411,7 @@ private void traverseDirectory(SequenceFile.Writer fileListWriter,
preserveXAttrs && child.isDirectory(),
preserveRawXattrs && child.isDirectory());
writeToFileListing(fileListWriter, childCopyListingStatus,
- sourcePathRoot, options);
+ sourcePathRoot);
}
if (retry < maxRetries) {
if (child.isDirectory()) {
@@ -443,26 +445,23 @@ private void writeToFileListingRoot(SequenceFile.Writer fileListWriter,
}
return;
}
- writeToFileListing(fileListWriter, fileStatus, sourcePathRoot, options);
+ writeToFileListing(fileListWriter, fileStatus, sourcePathRoot);
}
private void writeToFileListing(SequenceFile.Writer fileListWriter,
CopyListingFileStatus fileStatus,
- Path sourcePathRoot,
- DistCpOptions options) throws IOException {
+ Path sourcePathRoot) throws IOException {
if (LOG.isDebugEnabled()) {
LOG.debug("REL PATH: " + DistCpUtils.getRelativePath(sourcePathRoot,
fileStatus.getPath()) + ", FULL PATH: " + fileStatus.getPath());
}
- FileStatus status = fileStatus;
-
- if (!shouldCopy(fileStatus.getPath(), options)) {
+ if (!shouldCopy(fileStatus.getPath())) {
return;
}
fileListWriter.append(new Text(DistCpUtils.getRelativePath(sourcePathRoot,
- fileStatus.getPath())), status);
+ fileStatus.getPath())), fileStatus);
fileListWriter.sync();
if (!fileStatus.isDirectory()) {
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/TrueCopyFilter.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/TrueCopyFilter.java
new file mode 100644
index 0000000..b58dd9c
--- /dev/null
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/TrueCopyFilter.java
@@ -0,0 +1,33 @@
+/**
+ * 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.tools;
+
+import org.apache.hadoop.fs.Path;
+
+/**
+ * A CopyFilter which always returns true.
+ *
+ */
+public class TrueCopyFilter extends CopyFilter {
+
+ @Override
+ public boolean shouldCopy(Path path) {
+ return true;
+ }
+}
diff --git a/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/package-info.java b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/package-info.java
new file mode 100644
index 0000000..92278ed
--- /dev/null
+++ b/hadoop-tools/hadoop-distcp/src/main/java/org/apache/hadoop/tools/package-info.java
@@ -0,0 +1,26 @@
+/**
+ * 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.
+ */
+
+
+/**
+ * DistCp is a tool for replicating data using MapReduce jobs for concurrent
+ * copy operations.
+ *
+ * @version 2
+ */
+package org.apache.hadoop.tools;
\ No newline at end of file
diff --git a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestCopyListing.java b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestCopyListing.java
index 8381c1b..896763d 100644
--- a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestCopyListing.java
+++ b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestCopyListing.java
@@ -95,40 +95,6 @@ protected long getNumberOfPaths() {
}
@Test(timeout=10000)
- public void testSkipCopy() throws Exception {
- SimpleCopyListing listing = new SimpleCopyListing(getConf(), CREDENTIALS) {
- @Override
- protected boolean shouldCopy(Path path, DistCpOptions options) {
- return !path.getName().equals(FileOutputCommitter.SUCCEEDED_FILE_NAME);
- }
- };
- FileSystem fs = FileSystem.get(getConf());
- List<Path> srcPaths = new ArrayList<Path>();
- srcPaths.add(new Path("/tmp/in4/1"));
- srcPaths.add(new Path("/tmp/in4/2"));
- Path target = new Path("/tmp/out4/1");
- TestDistCpUtils.createFile(fs, "/tmp/in4/1/_SUCCESS");
- TestDistCpUtils.createFile(fs, "/tmp/in4/1/file");
- TestDistCpUtils.createFile(fs, "/tmp/in4/2");
- fs.mkdirs(target);
- DistCpOptions options = new DistCpOptions(srcPaths, target);
- Path listingFile = new Path("/tmp/list4");
- listing.buildListing(listingFile, options);
- Assert.assertEquals(listing.getNumberOfPaths(), 3);
- SequenceFile.Reader reader = new SequenceFile.Reader(getConf(),
- SequenceFile.Reader.file(listingFile));
- CopyListingFileStatus fileStatus = new CopyListingFileStatus();
- Text relativePath = new Text();
- Assert.assertTrue(reader.next(relativePath, fileStatus));
- Assert.assertEquals(relativePath.toString(), "/1");
- Assert.assertTrue(reader.next(relativePath, fileStatus));
- Assert.assertEquals(relativePath.toString(), "/1/file");
- Assert.assertTrue(reader.next(relativePath, fileStatus));
- Assert.assertEquals(relativePath.toString(), "/2");
- Assert.assertFalse(reader.next(relativePath, fileStatus));
- }
-
- @Test(timeout=10000)
public void testMultipleSrcToFile() {
FileSystem fs = null;
try {
diff --git a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestIntegration.java b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestIntegration.java
index 5726342..ee8e7cc 100644
--- a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestIntegration.java
+++ b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestIntegration.java
@@ -242,55 +242,6 @@ private void caseMultiFileTargetPresent(boolean sync) {
}
@Test(timeout=100000)
- public void testCustomCopyListing() {
-
- try {
- addEntries(listFile, "multifile1/file3", "multifile1/file4", "multifile1/file5");
- createFiles("multifile1/file3", "multifile1/file4", "multifile1/file5");
- mkdirs(target.toString());
-
- Configuration conf = getConf();
- try {
- conf.setClass(DistCpConstants.CONF_LABEL_COPY_LISTING_CLASS,
- CustomCopyListing.class, CopyListing.class);
- DistCpOptions options = new DistCpOptions(Arrays.
- asList(new Path(root + "/" + "multifile1")), target);
- options.setSyncFolder(true);
- options.setDeleteMissing(false);
- options.setOverwrite(false);
- try {
- new DistCp(conf, options).execute();
- } catch (Exception e) {
- LOG.error("Exception encountered ", e);
- throw new IOException(e);
- }
- } finally {
- conf.unset(DistCpConstants.CONF_LABEL_COPY_LISTING_CLASS);
- }
-
- checkResult(target, 2, "file4", "file5");
- } catch (IOException e) {
- LOG.error("Exception encountered while testing distcp", e);
- Assert.fail("distcp failure");
- } finally {
- TestDistCpUtils.delete(fs, root);
- }
- }
-
- private static class CustomCopyListing extends SimpleCopyListing {
-
- public CustomCopyListing(Configuration configuration,
- Credentials credentials) {
- super(configuration, credentials);
- }
-
- @Override
- protected boolean shouldCopy(Path path, DistCpOptions options) {
- return !path.getName().equals("file3");
- }
- }
-
- @Test(timeout=100000)
public void testMultiFileTargetMissing() {
caseMultiFileTargetMissing(false);
caseMultiFileTargetMissing(true);
diff --git a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestOptionsParser.java b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestOptionsParser.java
index 6eddfb2..b9d9ada 100644
--- a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestOptionsParser.java
+++ b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestOptionsParser.java
@@ -400,7 +400,7 @@ public void testToString() {
String val = "DistCpOptions{atomicCommit=false, syncFolder=false, deleteMissing=false, " +
"ignoreFailures=false, maxMaps=20, sslConfigurationFile='null', copyStrategy='uniformsize', " +
"sourceFileListing=abc, sourcePaths=null, targetPath=xyz, targetPathExists=true, " +
- "preserveRawXattrs=false}";
+ "preserveRawXattrs=false, filtersFile='null'}";
Assert.assertEquals(val, option.toString());
Assert.assertNotSame(DistCpOptionSwitch.ATOMIC_COMMIT.toString(),
DistCpOptionSwitch.ATOMIC_COMMIT.name());
@@ -718,4 +718,19 @@ public void testDiffOption() {
"Diff is valid only with update and delete options", e);
}
}
+
+ @Test
+ public void testExclusionsOption() {
+ DistCpOptions options = OptionsParser.parse(new String[] {
+ "hdfs://localhost:8020/source/first",
+ "hdfs://localhost:8020/target/"});
+ Assert.assertNull(options.getFiltersFile());
+
+ options = OptionsParser.parse(new String[] {
+ "-filters",
+ "/tmp/filters.txt",
+ "hdfs://localhost:8020/source/first",
+ "hdfs://localhost:8020/target/"});
+ Assert.assertEquals(options.getFiltersFile(), "/tmp/filters.txt");
+ }
}
diff --git a/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestRegexCopyFilter.java b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestRegexCopyFilter.java
new file mode 100644
index 0000000..5618a0b
--- /dev/null
+++ b/hadoop-tools/hadoop-distcp/src/test/java/org/apache/hadoop/tools/TestRegexCopyFilter.java
@@ -0,0 +1,113 @@
+/**
+ * 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.tools;
+
+import org.apache.hadoop.fs.Path;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+public class TestRegexCopyFilter {
+
+ @Test
+ public void testShouldCopyTrue() {
+ List<Pattern> filters = new ArrayList<>();
+ filters.add(Pattern.compile("user"));
+
+ RegexCopyFilter regexCopyFilter = new RegexCopyFilter("fakeFile");
+ regexCopyFilter.setFilters(filters);
+
+ Path shouldCopyPath = new Path("/user/bar");
+ Assert.assertTrue(regexCopyFilter.shouldCopy(shouldCopyPath));
+ }
+
+ @Test
+ public void testShouldCopyFalse() {
+ List<Pattern> filters = new ArrayList<>();
+ filters.add(Pattern.compile(".*test.*"));
+
+ RegexCopyFilter regexCopyFilter = new RegexCopyFilter("fakeFile");
+ regexCopyFilter.setFilters(filters);
+
+ Path shouldNotCopyPath = new Path("/user/testing");
+ Assert.assertFalse(regexCopyFilter.shouldCopy(shouldNotCopyPath));
+ }
+
+ @Test
+ public void testShouldCopyWithMultipleFilters() {
+ List<Pattern> filters = new ArrayList<>();
+ filters.add(Pattern.compile(".*test.*"));
+ filters.add(Pattern.compile("/user/b.*"));
+ filters.add(Pattern.compile(".*_SUCCESS"));
+
+ List<Path> toCopy = getTestPaths();
+
+ int shouldCopyCount = 0;
+
+ RegexCopyFilter regexCopyFilter = new RegexCopyFilter("fakeFile");
+ regexCopyFilter.setFilters(filters);
+
+ for (Path path: toCopy) {
+ if (regexCopyFilter.shouldCopy(path)) {
+ shouldCopyCount++;
+ }
+ }
+
+ Assert.assertEquals(2, shouldCopyCount);
+ }
+
+ @Test
+ public void testShouldExcludeAll() {
+ List<Pattern> filters = new ArrayList<>();
+ filters.add(Pattern.compile(".*test.*"));
+ filters.add(Pattern.compile("/user/b.*"));
+ filters.add(Pattern.compile(".*")); // exclude everything
+
+ List<Path> toCopy = getTestPaths();
+
+ int shouldCopyCount = 0;
+
+ RegexCopyFilter regexCopyFilter = new RegexCopyFilter("fakeFile");
+ regexCopyFilter.setFilters(filters);
+
+ for (Path path: toCopy) {
+ if (regexCopyFilter.shouldCopy(path)) {
+ shouldCopyCount++;
+ }
+ }
+
+ Assert.assertEquals(0, shouldCopyCount);
+ }
+
+ private List<Path> getTestPaths() {
+ List<Path> toCopy = new ArrayList<>();
+ toCopy.add(new Path("/user/bar"));
+ toCopy.add(new Path("/user/foo/_SUCCESS"));
+ toCopy.add(new Path("/hive/test_data"));