|
20 | 20 | import org.apache.hadoop.classification.VisibleForTesting;
|
21 | 21 | import org.apache.hadoop.HadoopIllegalArgumentException;
|
22 | 22 | import org.apache.hadoop.conf.Configuration;
|
| 23 | +import org.apache.hadoop.fs.CommonPathCapabilities; |
23 | 24 | import org.apache.hadoop.fs.FileStatus;
|
24 | 25 | import org.apache.hadoop.fs.FileSystem;
|
25 | 26 | import org.apache.hadoop.fs.Path;
|
26 | 27 | import org.apache.hadoop.hdfs.DFSUtilClient;
|
27 |
| -import org.apache.hadoop.hdfs.DistributedFileSystem; |
28 | 28 | import org.apache.hadoop.hdfs.protocol.HdfsConstants;
|
29 | 29 | import org.apache.hadoop.hdfs.protocol.SnapshotDiffReport;
|
30 |
| -import org.apache.hadoop.hdfs.web.WebHdfsFileSystem; |
31 | 30 | import org.apache.hadoop.tools.CopyListing.InvalidInputException;
|
32 | 31 |
|
33 | 32 | import java.io.FileNotFoundException;
|
34 | 33 | import java.io.IOException;
|
| 34 | +import java.lang.reflect.InvocationTargetException; |
| 35 | +import java.lang.reflect.Method; |
35 | 36 | import java.util.Arrays;
|
36 | 37 | import java.util.List;
|
37 | 38 | import java.util.Random;
|
@@ -106,20 +107,7 @@ private boolean preSyncCheck() throws IOException {
|
106 | 107 | final FileSystem snapshotDiffFs = isRdiff() ? tgtFs : srcFs;
|
107 | 108 | final Path snapshotDiffDir = isRdiff() ? targetDir : sourceDir;
|
108 | 109 |
|
109 |
| - // currently we require both the source and the target file system are |
110 |
| - // DistributedFileSystem or (S)WebHdfsFileSystem. |
111 |
| - if (!(srcFs instanceof DistributedFileSystem |
112 |
| - || srcFs instanceof WebHdfsFileSystem)) { |
113 |
| - throw new IllegalArgumentException("Unsupported source file system: " |
114 |
| - + srcFs.getScheme() + "://. " + |
115 |
| - "Supported file systems: hdfs://, webhdfs:// and swebhdfs://."); |
116 |
| - } |
117 |
| - if (!(tgtFs instanceof DistributedFileSystem |
118 |
| - || tgtFs instanceof WebHdfsFileSystem)) { |
119 |
| - throw new IllegalArgumentException("Unsupported target file system: " |
120 |
| - + tgtFs.getScheme() + "://. " + |
121 |
| - "Supported file systems: hdfs://, webhdfs:// and swebhdfs://."); |
122 |
| - } |
| 110 | + checkFilesystemSupport(sourceDir,targetDir,srcFs, tgtFs); |
123 | 111 |
|
124 | 112 | // make sure targetFS has no change between from and the current states
|
125 | 113 | if (!checkNoChange(tgtFs, targetDir)) {
|
@@ -165,6 +153,42 @@ private boolean preSyncCheck() throws IOException {
|
165 | 153 | return true;
|
166 | 154 | }
|
167 | 155 |
|
| 156 | + /** |
| 157 | + * Check if the source and target filesystems support snapshots. |
| 158 | + */ |
| 159 | + private void checkFilesystemSupport(Path sourceDir, Path targetDir, |
| 160 | + FileSystem srcFs, FileSystem tgtFs) throws IOException { |
| 161 | + if (!srcFs.hasPathCapability(sourceDir, |
| 162 | + CommonPathCapabilities.FS_SNAPSHOTS)) { |
| 163 | + throw new UnsupportedOperationException( |
| 164 | + "The source file system " + srcFs.getScheme() |
| 165 | + + " does not support snapshot."); |
| 166 | + } |
| 167 | + if (!tgtFs.hasPathCapability(targetDir, |
| 168 | + CommonPathCapabilities.FS_SNAPSHOTS)) { |
| 169 | + throw new UnsupportedOperationException( |
| 170 | + "The target file system " + tgtFs.getScheme() |
| 171 | + + " does not support snapshot."); |
| 172 | + } |
| 173 | + try { |
| 174 | + getSnapshotDiffReportMethod(srcFs); |
| 175 | + } catch (NoSuchMethodException e) { |
| 176 | + throw new UnsupportedOperationException( |
| 177 | + "The source file system " + srcFs.getScheme() |
| 178 | + + " does not support getSnapshotDiffReport", |
| 179 | + e); |
| 180 | + } |
| 181 | + try { |
| 182 | + getSnapshotDiffReportMethod(tgtFs); |
| 183 | + } catch (NoSuchMethodException e) { |
| 184 | + throw new UnsupportedOperationException( |
| 185 | + "The target file system " + tgtFs.getScheme() |
| 186 | + + " does not support getSnapshotDiffReport", |
| 187 | + e); |
| 188 | + } |
| 189 | + |
| 190 | + } |
| 191 | + |
168 | 192 | public boolean sync() throws IOException {
|
169 | 193 | if (!preSyncCheck()) {
|
170 | 194 | return false;
|
@@ -211,21 +235,10 @@ private boolean getAllDiffs() throws IOException {
|
211 | 235 | context.getTargetPath() : context.getSourcePaths().get(0);
|
212 | 236 |
|
213 | 237 | try {
|
214 |
| - SnapshotDiffReport report = null; |
215 |
| - FileSystem fs = ssDir.getFileSystem(conf); |
216 | 238 | final String from = getSnapshotName(context.getFromSnapshot());
|
217 | 239 | final String to = getSnapshotName(context.getToSnapshot());
|
218 |
| - if (fs instanceof DistributedFileSystem) { |
219 |
| - DistributedFileSystem dfs = (DistributedFileSystem)fs; |
220 |
| - report = dfs.getSnapshotDiffReport(ssDir, from, to); |
221 |
| - } else if (fs instanceof WebHdfsFileSystem) { |
222 |
| - WebHdfsFileSystem webHdfs = (WebHdfsFileSystem)fs; |
223 |
| - report = webHdfs.getSnapshotDiffReport(ssDir, from, to); |
224 |
| - } else { |
225 |
| - throw new IllegalArgumentException("Unsupported file system: " + |
226 |
| - fs.getScheme() + "://. " + |
227 |
| - "Supported file systems: hdfs://, webhdfs:// and swebhdfs://."); |
228 |
| - } |
| 240 | + SnapshotDiffReport report = |
| 241 | + getSnapshotDiffReport(ssDir.getFileSystem(conf), ssDir, from, to); |
229 | 242 |
|
230 | 243 | this.diffMap = new EnumMap<>(SnapshotDiffReport.DiffType.class);
|
231 | 244 | for (SnapshotDiffReport.DiffType type :
|
@@ -286,6 +299,36 @@ private boolean getAllDiffs() throws IOException {
|
286 | 299 | return false;
|
287 | 300 | }
|
288 | 301 |
|
| 302 | + /** |
| 303 | + * Check if the filesystem implementation has a method named |
| 304 | + * getSnapshotDiffReport. |
| 305 | + */ |
| 306 | + private static Method getSnapshotDiffReportMethod(FileSystem fs) |
| 307 | + throws NoSuchMethodException { |
| 308 | + return fs.getClass().getMethod( |
| 309 | + "getSnapshotDiffReport", Path.class, String.class, String.class); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Get the snapshotDiff b/w the fromSnapshot & toSnapshot for the given |
| 314 | + * filesystem. |
| 315 | + */ |
| 316 | + private static SnapshotDiffReport getSnapshotDiffReport( |
| 317 | + final FileSystem fs, |
| 318 | + final Path snapshotDir, |
| 319 | + final String fromSnapshot, |
| 320 | + final String toSnapshot) throws IOException { |
| 321 | + try { |
| 322 | + return (SnapshotDiffReport) getSnapshotDiffReportMethod(fs).invoke( |
| 323 | + fs, snapshotDir, fromSnapshot, toSnapshot); |
| 324 | + } catch (InvocationTargetException e) { |
| 325 | + throw new IOException(e.getCause()); |
| 326 | + } catch (NoSuchMethodException|IllegalAccessException e) { |
| 327 | + throw new IllegalArgumentException( |
| 328 | + "Failed to invoke getSnapshotDiffReport.", e); |
| 329 | + } |
| 330 | + } |
| 331 | + |
289 | 332 | private String getSnapshotName(String name) {
|
290 | 333 | return Path.CUR_DIR.equals(name) ? "" : name;
|
291 | 334 | }
|
@@ -327,14 +370,7 @@ private void deleteTargetTmpDir(FileSystem targetFs,
|
327 | 370 | private boolean checkNoChange(FileSystem fs, Path path) {
|
328 | 371 | try {
|
329 | 372 | final String from = getSnapshotName(context.getFromSnapshot());
|
330 |
| - SnapshotDiffReport targetDiff = null; |
331 |
| - if (fs instanceof DistributedFileSystem) { |
332 |
| - DistributedFileSystem dfs = (DistributedFileSystem)fs; |
333 |
| - targetDiff = dfs.getSnapshotDiffReport(path, from, ""); |
334 |
| - } else { |
335 |
| - WebHdfsFileSystem webHdfs = (WebHdfsFileSystem)fs; |
336 |
| - targetDiff = webHdfs.getSnapshotDiffReport(path, from, ""); |
337 |
| - } |
| 373 | + SnapshotDiffReport targetDiff = getSnapshotDiffReport(fs, path, from, ""); |
338 | 374 | if (!targetDiff.getDiffList().isEmpty()) {
|
339 | 375 | DistCp.LOG.warn("The target has been modified since snapshot "
|
340 | 376 | + context.getFromSnapshot());
|
|
0 commit comments