View Javadoc

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;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.FileStatus;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.fs.PathFilter;
34  import org.apache.hadoop.hbase.HRegionInfo;
35  import org.apache.hadoop.hbase.regionserver.HRegion;
36  import org.apache.hadoop.hbase.regionserver.StoreFile;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
39  import org.apache.hadoop.hbase.util.FSUtils;
40  import org.apache.hadoop.hbase.util.HFileArchiveUtil;
41  import org.apache.hadoop.io.MultipleIOException;
42  
43  import com.google.common.base.Function;
44  import com.google.common.base.Preconditions;
45  import com.google.common.collect.Collections2;
46  import com.google.common.collect.Lists;
47  
48  /**
49   * Utility class to handle the removal of HFiles (or the respective {@link StoreFile StoreFiles})
50   * for a HRegion from the {@link FileSystem}. The hfiles will be archived or deleted, depending on
51   * the state of the system. 
52   */
53  public class HFileArchiver {
54    private static final Log LOG = LogFactory.getLog(HFileArchiver.class);
55    private static final String SEPARATOR = ".";
56  
57    /** Number of retries in case of fs operation failure */
58    private static final int DEFAULT_RETRIES_NUMBER = 3;
59  
60    private HFileArchiver() {
61      // hidden ctor since this is just a util
62    }
63  
64    /**
65     * Cleans up all the files for a HRegion by archiving the HFiles to the
66     * archive directory
67     * @param conf the configuration to use
68     * @param fs the file system object
69     * @param info HRegionInfo for region to be deleted
70     * @throws IOException
71     */
72    public static void archiveRegion(Configuration conf, FileSystem fs, HRegionInfo info)
73        throws IOException {
74      Path rootDir = FSUtils.getRootDir(conf);
75      archiveRegion(fs, rootDir, FSUtils.getTableDir(rootDir, info.getTable()),
76        HRegion.getRegionDir(rootDir, info));
77    }
78  
79    /**
80     * Remove an entire region from the table directory via archiving the region's hfiles.
81     * @param fs {@link FileSystem} from which to remove the region
82     * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
83     *          the archive path)
84     * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
85     * @param regionDir {@link Path} to where a region is being stored (for building the archive path)
86     * @return <tt>true</tt> if the region was sucessfully deleted. <tt>false</tt> if the filesystem
87     *         operations could not complete.
88     * @throws IOException if the request cannot be completed
89     */
90    public static boolean archiveRegion(FileSystem fs, Path rootdir, Path tableDir, Path regionDir)
91        throws IOException {
92      if (LOG.isDebugEnabled()) {
93        LOG.debug("ARCHIVING " + regionDir.toString());
94      }
95  
96      // otherwise, we archive the files
97      // make sure we can archive
98      if (tableDir == null || regionDir == null) {
99        LOG.error("No archive directory could be found because tabledir (" + tableDir
100           + ") or regiondir (" + regionDir + "was null. Deleting files instead.");
101       deleteRegionWithoutArchiving(fs, regionDir);
102       // we should have archived, but failed to. Doesn't matter if we deleted
103       // the archived files correctly or not.
104       return false;
105     }
106 
107     // make sure the regiondir lives under the tabledir
108     Preconditions.checkArgument(regionDir.toString().startsWith(tableDir.toString()));
109     Path regionArchiveDir = HFileArchiveUtil.getRegionArchiveDir(rootdir,
110         FSUtils.getTableName(tableDir),
111         regionDir.getName());
112 
113     FileStatusConverter getAsFile = new FileStatusConverter(fs);
114     // otherwise, we attempt to archive the store files
115 
116     // build collection of just the store directories to archive
117     Collection<File> toArchive = new ArrayList<File>();
118     final PathFilter dirFilter = new FSUtils.DirFilter(fs);
119     PathFilter nonHidden = new PathFilter() {
120       @Override
121       public boolean accept(Path file) {
122         return dirFilter.accept(file) && !file.getName().toString().startsWith(".");
123       }
124     };
125     FileStatus[] storeDirs = FSUtils.listStatus(fs, regionDir, nonHidden);
126     // if there no files, we can just delete the directory and return;
127     if (storeDirs == null) {
128       LOG.debug("Region directory (" + regionDir + ") was empty, just deleting and returning!");
129       return deleteRegionWithoutArchiving(fs, regionDir);
130     }
131 
132     // convert the files in the region to a File
133     toArchive.addAll(Lists.transform(Arrays.asList(storeDirs), getAsFile));
134     LOG.debug("Archiving " + toArchive);
135     boolean success = false;
136     try {
137       success = resolveAndArchive(fs, regionArchiveDir, toArchive);
138     } catch (IOException e) {
139       LOG.error("Failed to archive " + toArchive, e);
140       success = false;
141     }
142 
143     // if that was successful, then we delete the region
144     if (success) {
145       return deleteRegionWithoutArchiving(fs, regionDir);
146     }
147 
148     throw new IOException("Received error when attempting to archive files (" + toArchive
149         + "), cannot delete region directory. ");
150   }
151 
152   /**
153    * Remove from the specified region the store files of the specified column family,
154    * either by archiving them or outright deletion
155    * @param fs the filesystem where the store files live
156    * @param conf {@link Configuration} to examine to determine the archive directory
157    * @param parent Parent region hosting the store files
158    * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
159    * @param family the family hosting the store files
160    * @throws IOException if the files could not be correctly disposed.
161    */
162   public static void archiveFamily(FileSystem fs, Configuration conf,
163       HRegionInfo parent, Path tableDir, byte[] family) throws IOException {
164     Path familyDir = new Path(tableDir, new Path(parent.getEncodedName(), Bytes.toString(family)));
165     archiveFamilyByFamilyDir(fs, conf, parent, familyDir, family);
166   }
167 
168   /**
169    * Removes from the specified region the store files of the specified column family,
170    * either by archiving them or outright deletion
171    * @param fs the filesystem where the store files live
172    * @param conf {@link Configuration} to examine to determine the archive directory
173    * @param parent Parent region hosting the store files
174    * @param familyDir {@link Path} to where the family is being stored
175    * @param family the family hosting the store files
176    * @throws IOException if the files could not be correctly disposed.
177    */
178   public static void archiveFamilyByFamilyDir(FileSystem fs, Configuration conf,
179       HRegionInfo parent, Path familyDir, byte[] family) throws IOException {
180     FileStatus[] storeFiles = FSUtils.listStatus(fs, familyDir);
181     if (storeFiles == null) {
182       LOG.debug("No store files to dispose for region=" + parent.getRegionNameAsString() +
183           ", family=" + Bytes.toString(family));
184       return;
185     }
186 
187     FileStatusConverter getAsFile = new FileStatusConverter(fs);
188     Collection<File> toArchive = Lists.transform(Arrays.asList(storeFiles), getAsFile);
189     Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, parent, family);
190 
191     // do the actual archive
192     if (!resolveAndArchive(fs, storeArchiveDir, toArchive)) {
193       throw new IOException("Failed to archive/delete all the files for region:"
194           + Bytes.toString(parent.getRegionName()) + ", family:" + Bytes.toString(family)
195           + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.");
196     }
197   }
198 
199   /**
200    * Remove the store files, either by archiving them or outright deletion
201    * @param conf {@link Configuration} to examine to determine the archive directory
202    * @param fs the filesystem where the store files live
203    * @param regionInfo {@link HRegionInfo} of the region hosting the store files
204    * @param family the family hosting the store files
205    * @param compactedFiles files to be disposed of. No further reading of these files should be
206    *          attempted; otherwise likely to cause an {@link IOException}
207    * @throws IOException if the files could not be correctly disposed.
208    */
209   public static void archiveStoreFiles(Configuration conf, FileSystem fs, HRegionInfo regionInfo,
210       Path tableDir, byte[] family, Collection<StoreFile> compactedFiles) throws IOException {
211 
212     // sometimes in testing, we don't have rss, so we need to check for that
213     if (fs == null) {
214       LOG.warn("Passed filesystem is null, so just deleting the files without archiving for region:"
215           + Bytes.toString(regionInfo.getRegionName()) + ", family:" + Bytes.toString(family));
216       deleteStoreFilesWithoutArchiving(compactedFiles);
217       return;
218     }
219 
220     // short circuit if we don't have any files to delete
221     if (compactedFiles.size() == 0) {
222       LOG.debug("No store files to dispose, done!");
223       return;
224     }
225 
226     // build the archive path
227     if (regionInfo == null || family == null) throw new IOException(
228         "Need to have a region and a family to archive from.");
229 
230     Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, regionInfo, tableDir, family);
231 
232     // make sure we don't archive if we can't and that the archive dir exists
233     if (!fs.mkdirs(storeArchiveDir)) {
234       throw new IOException("Could not make archive directory (" + storeArchiveDir + ") for store:"
235           + Bytes.toString(family) + ", deleting compacted files instead.");
236     }
237 
238     // otherwise we attempt to archive the store files
239     if (LOG.isDebugEnabled()) LOG.debug("Archiving compacted store files.");
240 
241     // Wrap the storefile into a File
242     StoreToFile getStorePath = new StoreToFile(fs);
243     Collection<File> storeFiles = Collections2.transform(compactedFiles, getStorePath);
244 
245     // do the actual archive
246     if (!resolveAndArchive(fs, storeArchiveDir, storeFiles)) {
247       throw new IOException("Failed to archive/delete all the files for region:"
248           + Bytes.toString(regionInfo.getRegionName()) + ", family:" + Bytes.toString(family)
249           + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.");
250     }
251   }
252 
253   /**
254    * Archive the store file
255    * @param fs the filesystem where the store files live
256    * @param regionInfo region hosting the store files
257    * @param conf {@link Configuration} to examine to determine the archive directory
258    * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
259    * @param family the family hosting the store files
260    * @param storeFile file to be archived
261    * @throws IOException if the files could not be correctly disposed.
262    */
263   public static void archiveStoreFile(Configuration conf, FileSystem fs, HRegionInfo regionInfo,
264       Path tableDir, byte[] family, Path storeFile) throws IOException {
265     Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, regionInfo, tableDir, family);
266     // make sure we don't archive if we can't and that the archive dir exists
267     if (!fs.mkdirs(storeArchiveDir)) {
268       throw new IOException("Could not make archive directory (" + storeArchiveDir + ") for store:"
269           + Bytes.toString(family) + ", deleting compacted files instead.");
270     }
271 
272     // do the actual archive
273     long start = EnvironmentEdgeManager.currentTime();
274     File file = new FileablePath(fs, storeFile);
275     if (!resolveAndArchiveFile(storeArchiveDir, file, Long.toString(start))) {
276       throw new IOException("Failed to archive/delete the file for region:"
277           + regionInfo.getRegionNameAsString() + ", family:" + Bytes.toString(family)
278           + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.");
279     }
280   }
281 
282   /**
283    * Archive the given files and resolve any conflicts with existing files via appending the time
284    * archiving started (so all conflicts in the same group have the same timestamp appended).
285    * <p>
286    * If any of the passed files to archive are directories, archives all the files under that
287    * directory. Archive directory structure for children is the base archive directory name + the
288    * parent directory and is built recursively is passed files are directories themselves.
289    * @param fs {@link FileSystem} on which to archive the files
290    * @param baseArchiveDir base archive directory to archive the given files
291    * @param toArchive files to be archived
292    * @return <tt>true</tt> on success, <tt>false</tt> otherwise
293    * @throws IOException on unexpected failure
294    */
295   private static boolean resolveAndArchive(FileSystem fs, Path baseArchiveDir,
296       Collection<File> toArchive) throws IOException {
297     if (LOG.isTraceEnabled()) LOG.trace("Starting to archive " + toArchive);
298     long start = EnvironmentEdgeManager.currentTime();
299     List<File> failures = resolveAndArchive(fs, baseArchiveDir, toArchive, start);
300 
301     // notify that some files were not archived.
302     // We can't delete the files otherwise snapshots or other backup system
303     // that relies on the archiver end up with data loss.
304     if (failures.size() > 0) {
305       LOG.warn("Failed to complete archive of: " + failures +
306         ". Those files are still in the original location, and they may slow down reads.");
307       return false;
308     }
309     return true;
310   }
311 
312   /**
313    * Resolve any conflict with an existing archive file via timestamp-append
314    * renaming of the existing file and then archive the passed in files.
315    * @param fs {@link FileSystem} on which to archive the files
316    * @param baseArchiveDir base archive directory to store the files. If any of
317    *          the files to archive are directories, will append the name of the
318    *          directory to the base archive directory name, creating a parallel
319    *          structure.
320    * @param toArchive files/directories that need to be archvied
321    * @param start time the archiving started - used for resolving archive
322    *          conflicts.
323    * @return the list of failed to archive files.
324    * @throws IOException if an unexpected file operation exception occured
325    */
326   private static List<File> resolveAndArchive(FileSystem fs, Path baseArchiveDir,
327       Collection<File> toArchive, long start) throws IOException {
328     // short circuit if no files to move
329     if (toArchive.size() == 0) return Collections.emptyList();
330 
331     if (LOG.isTraceEnabled()) LOG.trace("moving files to the archive directory: " + baseArchiveDir);
332 
333     // make sure the archive directory exists
334     if (!fs.exists(baseArchiveDir)) {
335       if (!fs.mkdirs(baseArchiveDir)) {
336         throw new IOException("Failed to create the archive directory:" + baseArchiveDir
337             + ", quitting archive attempt.");
338       }
339       if (LOG.isTraceEnabled()) LOG.trace("Created archive directory:" + baseArchiveDir);
340     }
341 
342     List<File> failures = new ArrayList<File>();
343     String startTime = Long.toString(start);
344     for (File file : toArchive) {
345       // if its a file archive it
346       try {
347         if (LOG.isTraceEnabled()) LOG.trace("Archiving: " + file);
348         if (file.isFile()) {
349           // attempt to archive the file
350           if (!resolveAndArchiveFile(baseArchiveDir, file, startTime)) {
351             LOG.warn("Couldn't archive " + file + " into backup directory: " + baseArchiveDir);
352             failures.add(file);
353           }
354         } else {
355           // otherwise its a directory and we need to archive all files
356           if (LOG.isTraceEnabled()) LOG.trace(file + " is a directory, archiving children files");
357           // so we add the directory name to the one base archive
358           Path parentArchiveDir = new Path(baseArchiveDir, file.getName());
359           // and then get all the files from that directory and attempt to
360           // archive those too
361           Collection<File> children = file.getChildren();
362           failures.addAll(resolveAndArchive(fs, parentArchiveDir, children, start));
363         }
364       } catch (IOException e) {
365         LOG.warn("Failed to archive " + file, e);
366         failures.add(file);
367       }
368     }
369     return failures;
370   }
371 
372   /**
373    * Attempt to archive the passed in file to the archive directory.
374    * <p>
375    * If the same file already exists in the archive, it is moved to a timestamped directory under
376    * the archive directory and the new file is put in its place.
377    * @param archiveDir {@link Path} to the directory that stores the archives of the hfiles
378    * @param currentFile {@link Path} to the original HFile that will be archived
379    * @param archiveStartTime time the archiving started, to resolve naming conflicts
380    * @return <tt>true</tt> if the file is successfully archived. <tt>false</tt> if there was a
381    *         problem, but the operation still completed.
382    * @throws IOException on failure to complete {@link FileSystem} operations.
383    */
384   private static boolean resolveAndArchiveFile(Path archiveDir, File currentFile,
385       String archiveStartTime) throws IOException {
386     // build path as it should be in the archive
387     String filename = currentFile.getName();
388     Path archiveFile = new Path(archiveDir, filename);
389     FileSystem fs = currentFile.getFileSystem();
390 
391     // if the file already exists in the archive, move that one to a timestamped backup. This is a
392     // really, really unlikely situtation, where we get the same name for the existing file, but
393     // is included just for that 1 in trillion chance.
394     if (fs.exists(archiveFile)) {
395       if (LOG.isDebugEnabled()) {
396         LOG.debug("File:" + archiveFile + " already exists in archive, moving to "
397             + "timestamped backup and overwriting current.");
398       }
399 
400       // move the archive file to the stamped backup
401       Path backedupArchiveFile = new Path(archiveDir, filename + SEPARATOR + archiveStartTime);
402       if (!fs.rename(archiveFile, backedupArchiveFile)) {
403         LOG.error("Could not rename archive file to backup: " + backedupArchiveFile
404             + ", deleting existing file in favor of newer.");
405         // try to delete the exisiting file, if we can't rename it
406         if (!fs.delete(archiveFile, false)) {
407           throw new IOException("Couldn't delete existing archive file (" + archiveFile
408               + ") or rename it to the backup file (" + backedupArchiveFile
409               + ") to make room for similarly named file.");
410         }
411       }
412       LOG.debug("Backed up archive file from " + archiveFile);
413     }
414 
415     if (LOG.isTraceEnabled()) {
416       LOG.trace("No existing file in archive for: " + archiveFile +
417         ", free to archive original file.");
418     }
419 
420     // at this point, we should have a free spot for the archive file
421     boolean success = false;
422     for (int i = 0; !success && i < DEFAULT_RETRIES_NUMBER; ++i) {
423       if (i > 0) {
424         // Ensure that the archive directory exists.
425         // The previous "move to archive" operation has failed probably because
426         // the cleaner has removed our archive directory (HBASE-7643).
427         // (we're in a retry loop, so don't worry too much about the exception)
428         try {
429           if (!fs.exists(archiveDir)) {
430             if (fs.mkdirs(archiveDir)) {
431               LOG.debug("Created archive directory:" + archiveDir);
432             }
433           }
434         } catch (IOException e) {
435           LOG.warn("Failed to create directory: " + archiveDir, e);
436         }
437       }
438 
439       try {
440         success = currentFile.moveAndClose(archiveFile);
441       } catch (IOException e) {
442         LOG.warn("Failed to archive " + currentFile + " on try #" + i, e);
443         success = false;
444       }
445     }
446 
447     if (!success) {
448       LOG.error("Failed to archive " + currentFile);
449       return false;
450     }
451 
452     if (LOG.isDebugEnabled()) {
453       LOG.debug("Finished archiving from " + currentFile + ", to " + archiveFile);
454     }
455     return true;
456   }
457 
458   /**
459    * Without regard for backup, delete a region. Should be used with caution.
460    * @param regionDir {@link Path} to the region to be deleted.
461    * @param fs FileSystem from which to delete the region
462    * @return <tt>true</tt> on successful deletion, <tt>false</tt> otherwise
463    * @throws IOException on filesystem operation failure
464    */
465   private static boolean deleteRegionWithoutArchiving(FileSystem fs, Path regionDir)
466       throws IOException {
467     if (fs.delete(regionDir, true)) {
468       LOG.debug("Deleted all region files in: " + regionDir);
469       return true;
470     }
471     LOG.debug("Failed to delete region directory:" + regionDir);
472     return false;
473   }
474 
475   /**
476    * Just do a simple delete of the given store files
477    * <p>
478    * A best effort is made to delete each of the files, rather than bailing on the first failure.
479    * <p>
480    * This method is preferable to {@link #deleteFilesWithoutArchiving(Collection)} since it consumes
481    * less resources, but is limited in terms of usefulness
482    * @param compactedFiles store files to delete from the file system.
483    * @throws IOException if a file cannot be deleted. All files will be attempted to deleted before
484    *           throwing the exception, rather than failing at the first file.
485    */
486   private static void deleteStoreFilesWithoutArchiving(Collection<StoreFile> compactedFiles)
487       throws IOException {
488     LOG.debug("Deleting store files without archiving.");
489     List<IOException> errors = new ArrayList<IOException>(0);
490     for (StoreFile hsf : compactedFiles) {
491       try {
492         hsf.deleteReader();
493       } catch (IOException e) {
494         LOG.error("Failed to delete store file:" + hsf.getPath());
495         errors.add(e);
496       }
497     }
498     if (errors.size() > 0) {
499       throw MultipleIOException.createIOException(errors);
500     }
501   }
502 
503   /**
504    * Adapt a type to match the {@link File} interface, which is used internally for handling
505    * archival/removal of files
506    * @param <T> type to adapt to the {@link File} interface
507    */
508   private static abstract class FileConverter<T> implements Function<T, File> {
509     protected final FileSystem fs;
510 
511     public FileConverter(FileSystem fs) {
512       this.fs = fs;
513     }
514   }
515 
516   /**
517    * Convert a FileStatus to something we can manage in the archiving
518    */
519   private static class FileStatusConverter extends FileConverter<FileStatus> {
520     public FileStatusConverter(FileSystem fs) {
521       super(fs);
522     }
523 
524     @Override
525     public File apply(FileStatus input) {
526       return new FileablePath(fs, input.getPath());
527     }
528   }
529 
530   /**
531    * Convert the {@link StoreFile} into something we can manage in the archive
532    * methods
533    */
534   private static class StoreToFile extends FileConverter<StoreFile> {
535     public StoreToFile(FileSystem fs) {
536       super(fs);
537     }
538 
539     @Override
540     public File apply(StoreFile input) {
541       return new FileableStoreFile(fs, input);
542     }
543   }
544 
545   /**
546    * Wrapper to handle file operations uniformly
547    */
548   private static abstract class File {
549     protected final FileSystem fs;
550 
551     public File(FileSystem fs) {
552       this.fs = fs;
553     }
554 
555     /**
556      * Delete the file
557      * @throws IOException on failure
558      */
559     abstract void delete() throws IOException;
560 
561     /**
562      * Check to see if this is a file or a directory
563      * @return <tt>true</tt> if it is a file, <tt>false</tt> otherwise
564      * @throws IOException on {@link FileSystem} connection error
565      */
566     abstract boolean isFile() throws IOException;
567 
568     /**
569      * @return if this is a directory, returns all the children in the
570      *         directory, otherwise returns an empty list
571      * @throws IOException
572      */
573     abstract Collection<File> getChildren() throws IOException;
574 
575     /**
576      * close any outside readers of the file
577      * @throws IOException
578      */
579     abstract void close() throws IOException;
580 
581     /**
582      * @return the name of the file (not the full fs path, just the individual
583      *         file name)
584      */
585     abstract String getName();
586 
587     /**
588      * @return the path to this file
589      */
590     abstract Path getPath();
591 
592     /**
593      * Move the file to the given destination
594      * @param dest
595      * @return <tt>true</tt> on success
596      * @throws IOException
597      */
598     public boolean moveAndClose(Path dest) throws IOException {
599       this.close();
600       Path p = this.getPath();
601       return FSUtils.renameAndSetModifyTime(fs, p, dest);
602     }
603 
604     /**
605      * @return the {@link FileSystem} on which this file resides
606      */
607     public FileSystem getFileSystem() {
608       return this.fs;
609     }
610 
611     @Override
612     public String toString() {
613       return this.getClass() + ", file:" + getPath().toString();
614     }
615   }
616 
617   /**
618    * A {@link File} that wraps a simple {@link Path} on a {@link FileSystem}.
619    */
620   private static class FileablePath extends File {
621     private final Path file;
622     private final FileStatusConverter getAsFile;
623 
624     public FileablePath(FileSystem fs, Path file) {
625       super(fs);
626       this.file = file;
627       this.getAsFile = new FileStatusConverter(fs);
628     }
629 
630     @Override
631     public void delete() throws IOException {
632       if (!fs.delete(file, true)) throw new IOException("Failed to delete:" + this.file);
633     }
634 
635     @Override
636     public String getName() {
637       return file.getName();
638     }
639 
640     @Override
641     public Collection<File> getChildren() throws IOException {
642       if (fs.isFile(file)) return Collections.emptyList();
643       return Collections2.transform(Arrays.asList(fs.listStatus(file)), getAsFile);
644     }
645 
646     @Override
647     public boolean isFile() throws IOException {
648       return fs.isFile(file);
649     }
650 
651     @Override
652     public void close() throws IOException {
653       // NOOP - files are implicitly closed on removal
654     }
655 
656     @Override
657     Path getPath() {
658       return file;
659     }
660   }
661 
662   /**
663    * {@link File} adapter for a {@link StoreFile} living on a {@link FileSystem}
664    * .
665    */
666   private static class FileableStoreFile extends File {
667     StoreFile file;
668 
669     public FileableStoreFile(FileSystem fs, StoreFile store) {
670       super(fs);
671       this.file = store;
672     }
673 
674     @Override
675     public void delete() throws IOException {
676       file.deleteReader();
677     }
678 
679     @Override
680     public String getName() {
681       return file.getPath().getName();
682     }
683 
684     @Override
685     public boolean isFile() {
686       return true;
687     }
688 
689     @Override
690     public Collection<File> getChildren() throws IOException {
691       // storefiles don't have children
692       return Collections.emptyList();
693     }
694 
695     @Override
696     public void close() throws IOException {
697       file.closeReader(true);
698     }
699 
700     @Override
701     Path getPath() {
702       return file.getPath();
703     }
704   }
705 }