001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup;
019
020import java.io.FileNotFoundException;
021import java.io.IOException;
022import java.io.InterruptedIOException;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.List;
028import java.util.concurrent.ExecutionException;
029import java.util.concurrent.Future;
030import java.util.concurrent.ThreadFactory;
031import java.util.concurrent.ThreadPoolExecutor;
032import java.util.concurrent.TimeUnit;
033import java.util.concurrent.atomic.AtomicInteger;
034
035import org.apache.hadoop.conf.Configuration;
036import org.apache.hadoop.fs.FileStatus;
037import org.apache.hadoop.fs.FileSystem;
038import org.apache.hadoop.fs.Path;
039import org.apache.hadoop.fs.PathFilter;
040import org.apache.hadoop.hbase.client.RegionInfo;
041import org.apache.hadoop.hbase.regionserver.HRegion;
042import org.apache.hadoop.hbase.regionserver.HStoreFile;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
045import org.apache.hadoop.hbase.util.FSUtils;
046import org.apache.hadoop.hbase.util.HFileArchiveUtil;
047import org.apache.hadoop.hbase.util.Threads;
048import org.apache.hadoop.io.MultipleIOException;
049import org.apache.yetus.audience.InterfaceAudience;
050import org.slf4j.Logger;
051import org.slf4j.LoggerFactory;
052import org.apache.hbase.thirdparty.com.google.common.base.Function;
053import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
054import org.apache.hbase.thirdparty.com.google.common.collect.Collections2;
055import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
056
057/**
058 * Utility class to handle the removal of HFiles (or the respective {@link HStoreFile StoreFiles})
059 * for a HRegion from the {@link FileSystem}. The hfiles will be archived or deleted, depending on
060 * the state of the system.
061 */
062@InterfaceAudience.Private
063public class HFileArchiver {
064  private static final Logger LOG = LoggerFactory.getLogger(HFileArchiver.class);
065  private static final String SEPARATOR = ".";
066
067  /** Number of retries in case of fs operation failure */
068  private static final int DEFAULT_RETRIES_NUMBER = 3;
069
070  private static final Function<File, Path> FUNC_FILE_TO_PATH =
071      new Function<File, Path>() {
072        @Override
073        public Path apply(File file) {
074          return file == null ? null : file.getPath();
075        }
076      };
077
078  private static ThreadPoolExecutor archiveExecutor;
079
080  private HFileArchiver() {
081    // hidden ctor since this is just a util
082  }
083
084  /**
085   * @return True if the Region exits in the filesystem.
086   */
087  public static boolean exists(Configuration conf, FileSystem fs, RegionInfo info)
088      throws IOException {
089    Path rootDir = FSUtils.getRootDir(conf);
090    Path regionDir = HRegion.getRegionDir(rootDir, info);
091    return fs.exists(regionDir);
092  }
093
094  /**
095   * Cleans up all the files for a HRegion by archiving the HFiles to the
096   * archive directory
097   * @param conf the configuration to use
098   * @param fs the file system object
099   * @param info RegionInfo for region to be deleted
100   * @throws IOException
101   */
102  public static void archiveRegion(Configuration conf, FileSystem fs, RegionInfo info)
103      throws IOException {
104    Path rootDir = FSUtils.getRootDir(conf);
105    archiveRegion(fs, rootDir, FSUtils.getTableDir(rootDir, info.getTable()),
106      HRegion.getRegionDir(rootDir, info));
107  }
108
109  /**
110   * Remove an entire region from the table directory via archiving the region's hfiles.
111   * @param fs {@link FileSystem} from which to remove the region
112   * @param rootdir {@link Path} to the root directory where hbase files are stored (for building
113   *          the archive path)
114   * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
115   * @param regionDir {@link Path} to where a region is being stored (for building the archive path)
116   * @return <tt>true</tt> if the region was successfully deleted. <tt>false</tt> if the filesystem
117   *         operations could not complete.
118   * @throws IOException if the request cannot be completed
119   */
120  public static boolean archiveRegion(FileSystem fs, Path rootdir, Path tableDir, Path regionDir)
121      throws IOException {
122    // otherwise, we archive the files
123    // make sure we can archive
124    if (tableDir == null || regionDir == null) {
125      LOG.error("No archive directory could be found because tabledir (" + tableDir
126          + ") or regiondir (" + regionDir + "was null. Deleting files instead.");
127      deleteRegionWithoutArchiving(fs, regionDir);
128      // we should have archived, but failed to. Doesn't matter if we deleted
129      // the archived files correctly or not.
130      return false;
131    }
132
133    LOG.debug("ARCHIVING {}", regionDir);
134
135    // make sure the regiondir lives under the tabledir
136    Preconditions.checkArgument(regionDir.toString().startsWith(tableDir.toString()));
137    Path regionArchiveDir = HFileArchiveUtil.getRegionArchiveDir(rootdir,
138        FSUtils.getTableName(tableDir),
139        regionDir.getName());
140
141    FileStatusConverter getAsFile = new FileStatusConverter(fs);
142    // otherwise, we attempt to archive the store files
143
144    // build collection of just the store directories to archive
145    Collection<File> toArchive = new ArrayList<>();
146    final PathFilter dirFilter = new FSUtils.DirFilter(fs);
147    PathFilter nonHidden = new PathFilter() {
148      @Override
149      public boolean accept(Path file) {
150        return dirFilter.accept(file) && !file.getName().startsWith(".");
151      }
152    };
153    FileStatus[] storeDirs = FSUtils.listStatus(fs, regionDir, nonHidden);
154    // if there no files, we can just delete the directory and return;
155    if (storeDirs == null) {
156      LOG.debug("Directory {} empty.", regionDir);
157      return deleteRegionWithoutArchiving(fs, regionDir);
158    }
159
160    // convert the files in the region to a File
161    toArchive.addAll(Lists.transform(Arrays.asList(storeDirs), getAsFile));
162    LOG.debug("Archiving " + toArchive);
163    List<File> failedArchive = resolveAndArchive(fs, regionArchiveDir, toArchive,
164        EnvironmentEdgeManager.currentTime());
165    if (!failedArchive.isEmpty()) {
166      throw new FailedArchiveException("Failed to archive/delete all the files for region:"
167          + regionDir.getName() + " into " + regionArchiveDir
168          + ". Something is probably awry on the filesystem.",
169          Collections2.transform(failedArchive, FUNC_FILE_TO_PATH));
170    }
171    // if that was successful, then we delete the region
172    return deleteRegionWithoutArchiving(fs, regionDir);
173  }
174
175  /**
176   * Archive the specified regions in parallel.
177   * @param conf the configuration to use
178   * @param fs {@link FileSystem} from which to remove the region
179   * @param rootDir {@link Path} to the root directory where hbase files are stored (for building
180   *                            the archive path)
181   * @param tableDir {@link Path} to where the table is being stored (for building the archive
182   *                             path)
183   * @param regionDirList {@link Path} to where regions are being stored (for building the archive
184   *                                  path)
185   * @throws IOException if the request cannot be completed
186   */
187  public static void archiveRegions(Configuration conf, FileSystem fs, Path rootDir, Path tableDir,
188    List<Path> regionDirList) throws IOException {
189    List<Future<Void>> futures = new ArrayList<>(regionDirList.size());
190    for (Path regionDir: regionDirList) {
191      Future<Void> future = getArchiveExecutor(conf).submit(() -> {
192        archiveRegion(fs, rootDir, tableDir, regionDir);
193        return null;
194      });
195      futures.add(future);
196    }
197    try {
198      for (Future<Void> future: futures) {
199        future.get();
200      }
201    } catch (InterruptedException e) {
202      throw new InterruptedIOException(e.getMessage());
203    } catch (ExecutionException e) {
204      throw new IOException(e.getCause());
205    }
206  }
207
208  private static synchronized ThreadPoolExecutor getArchiveExecutor(final Configuration conf) {
209    if (archiveExecutor == null) {
210      int maxThreads = conf.getInt("hbase.hfilearchiver.thread.pool.max", 8);
211      archiveExecutor = Threads.getBoundedCachedThreadPool(maxThreads, 30L, TimeUnit.SECONDS,
212        getThreadFactory());
213
214      // Shutdown this ThreadPool in a shutdown hook
215      Runtime.getRuntime().addShutdownHook(new Thread(() -> archiveExecutor.shutdown()));
216    }
217    return archiveExecutor;
218  }
219
220  // We need this method instead of Threads.getNamedThreadFactory() to pass some tests.
221  // The difference from Threads.getNamedThreadFactory() is that it doesn't fix ThreadGroup for
222  // new threads. If we use Threads.getNamedThreadFactory(), we will face ThreadGroup related
223  // issues in some tests.
224  private static ThreadFactory getThreadFactory() {
225    return new ThreadFactory() {
226      final AtomicInteger threadNumber = new AtomicInteger(1);
227
228      @Override
229      public Thread newThread(Runnable r) {
230        final String name = "HFileArchiver-" + threadNumber.getAndIncrement();
231        return new Thread(r, name);
232      }
233    };
234  }
235
236  /**
237   * Remove from the specified region the store files of the specified column family,
238   * either by archiving them or outright deletion
239   * @param fs the filesystem where the store files live
240   * @param conf {@link Configuration} to examine to determine the archive directory
241   * @param parent Parent region hosting the store files
242   * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
243   * @param family the family hosting the store files
244   * @throws IOException if the files could not be correctly disposed.
245   */
246  public static void archiveFamily(FileSystem fs, Configuration conf,
247      RegionInfo parent, Path tableDir, byte[] family) throws IOException {
248    Path familyDir = new Path(tableDir, new Path(parent.getEncodedName(), Bytes.toString(family)));
249    archiveFamilyByFamilyDir(fs, conf, parent, familyDir, family);
250  }
251
252  /**
253   * Removes from the specified region the store files of the specified column family,
254   * either by archiving them or outright deletion
255   * @param fs the filesystem where the store files live
256   * @param conf {@link Configuration} to examine to determine the archive directory
257   * @param parent Parent region hosting the store files
258   * @param familyDir {@link Path} to where the family is being stored
259   * @param family the family hosting the store files
260   * @throws IOException if the files could not be correctly disposed.
261   */
262  public static void archiveFamilyByFamilyDir(FileSystem fs, Configuration conf,
263      RegionInfo parent, Path familyDir, byte[] family) throws IOException {
264    FileStatus[] storeFiles = FSUtils.listStatus(fs, familyDir);
265    if (storeFiles == null) {
266      LOG.debug("No files to dispose of in {}, family={}", parent.getRegionNameAsString(),
267          Bytes.toString(family));
268      return;
269    }
270
271    FileStatusConverter getAsFile = new FileStatusConverter(fs);
272    Collection<File> toArchive = Lists.transform(Arrays.asList(storeFiles), getAsFile);
273    Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, parent, family);
274
275    // do the actual archive
276    List<File> failedArchive = resolveAndArchive(fs, storeArchiveDir, toArchive,
277        EnvironmentEdgeManager.currentTime());
278    if (!failedArchive.isEmpty()){
279      throw new FailedArchiveException("Failed to archive/delete all the files for region:"
280          + Bytes.toString(parent.getRegionName()) + ", family:" + Bytes.toString(family)
281          + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.",
282          Collections2.transform(failedArchive, FUNC_FILE_TO_PATH));
283    }
284  }
285
286  /**
287   * Remove the store files, either by archiving them or outright deletion
288   * @param conf {@link Configuration} to examine to determine the archive directory
289   * @param fs the filesystem where the store files live
290   * @param regionInfo {@link RegionInfo} of the region hosting the store files
291   * @param family the family hosting the store files
292   * @param compactedFiles files to be disposed of. No further reading of these files should be
293   *          attempted; otherwise likely to cause an {@link IOException}
294   * @throws IOException if the files could not be correctly disposed.
295   */
296  public static void archiveStoreFiles(Configuration conf, FileSystem fs, RegionInfo regionInfo,
297      Path tableDir, byte[] family, Collection<HStoreFile> compactedFiles)
298      throws IOException, FailedArchiveException {
299
300    // sometimes in testing, we don't have rss, so we need to check for that
301    if (fs == null) {
302      LOG.warn("Passed filesystem is null, so just deleting files without archiving for {}," +
303              "family={}", Bytes.toString(regionInfo.getRegionName()), Bytes.toString(family));
304      deleteStoreFilesWithoutArchiving(compactedFiles);
305      return;
306    }
307
308    // short circuit if we don't have any files to delete
309    if (compactedFiles.isEmpty()) {
310      LOG.debug("No files to dispose of, done!");
311      return;
312    }
313
314    // build the archive path
315    if (regionInfo == null || family == null) throw new IOException(
316        "Need to have a region and a family to archive from.");
317
318    Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, regionInfo, tableDir, family);
319
320    // make sure we don't archive if we can't and that the archive dir exists
321    if (!fs.mkdirs(storeArchiveDir)) {
322      throw new IOException("Could not make archive directory (" + storeArchiveDir + ") for store:"
323          + Bytes.toString(family) + ", deleting compacted files instead.");
324    }
325
326    // otherwise we attempt to archive the store files
327    LOG.debug("Archiving compacted files.");
328
329    // Wrap the storefile into a File
330    StoreToFile getStorePath = new StoreToFile(fs);
331    Collection<File> storeFiles = Collections2.transform(compactedFiles, getStorePath);
332
333    // do the actual archive
334    List<File> failedArchive = resolveAndArchive(fs, storeArchiveDir, storeFiles,
335        EnvironmentEdgeManager.currentTime());
336
337    if (!failedArchive.isEmpty()){
338      throw new FailedArchiveException("Failed to archive/delete all the files for region:"
339          + Bytes.toString(regionInfo.getRegionName()) + ", family:" + Bytes.toString(family)
340          + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.",
341          Collections2.transform(failedArchive, FUNC_FILE_TO_PATH));
342    }
343  }
344
345  /**
346   * Archive the store file
347   * @param fs the filesystem where the store files live
348   * @param regionInfo region hosting the store files
349   * @param conf {@link Configuration} to examine to determine the archive directory
350   * @param tableDir {@link Path} to where the table is being stored (for building the archive path)
351   * @param family the family hosting the store files
352   * @param storeFile file to be archived
353   * @throws IOException if the files could not be correctly disposed.
354   */
355  public static void archiveStoreFile(Configuration conf, FileSystem fs, RegionInfo regionInfo,
356      Path tableDir, byte[] family, Path storeFile) throws IOException {
357    Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, regionInfo, tableDir, family);
358    // make sure we don't archive if we can't and that the archive dir exists
359    if (!fs.mkdirs(storeArchiveDir)) {
360      throw new IOException("Could not make archive directory (" + storeArchiveDir + ") for store:"
361          + Bytes.toString(family) + ", deleting compacted files instead.");
362    }
363
364    // do the actual archive
365    long start = EnvironmentEdgeManager.currentTime();
366    File file = new FileablePath(fs, storeFile);
367    if (!resolveAndArchiveFile(storeArchiveDir, file, Long.toString(start))) {
368      throw new IOException("Failed to archive/delete the file for region:"
369          + regionInfo.getRegionNameAsString() + ", family:" + Bytes.toString(family)
370          + " into " + storeArchiveDir + ". Something is probably awry on the filesystem.");
371    }
372  }
373
374  /**
375   * Resolve any conflict with an existing archive file via timestamp-append
376   * renaming of the existing file and then archive the passed in files.
377   * @param fs {@link FileSystem} on which to archive the files
378   * @param baseArchiveDir base archive directory to store the files. If any of
379   *          the files to archive are directories, will append the name of the
380   *          directory to the base archive directory name, creating a parallel
381   *          structure.
382   * @param toArchive files/directories that need to be archvied
383   * @param start time the archiving started - used for resolving archive
384   *          conflicts.
385   * @return the list of failed to archive files.
386   * @throws IOException if an unexpected file operation exception occurred
387   */
388  private static List<File> resolveAndArchive(FileSystem fs, Path baseArchiveDir,
389      Collection<File> toArchive, long start) throws IOException {
390    // short circuit if no files to move
391    if (toArchive.isEmpty()) {
392      return Collections.emptyList();
393    }
394
395    LOG.trace("Moving files to the archive directory {}", baseArchiveDir);
396
397    // make sure the archive directory exists
398    if (!fs.exists(baseArchiveDir)) {
399      if (!fs.mkdirs(baseArchiveDir)) {
400        throw new IOException("Failed to create the archive directory:" + baseArchiveDir
401            + ", quitting archive attempt.");
402      }
403      LOG.trace("Created archive directory {}", baseArchiveDir);
404    }
405
406    List<File> failures = new ArrayList<>();
407    String startTime = Long.toString(start);
408    for (File file : toArchive) {
409      // if its a file archive it
410      try {
411        LOG.trace("Archiving {}", file);
412        if (file.isFile()) {
413          // attempt to archive the file
414          if (!resolveAndArchiveFile(baseArchiveDir, file, startTime)) {
415            LOG.warn("Couldn't archive " + file + " into backup directory: " + baseArchiveDir);
416            failures.add(file);
417          }
418        } else {
419          // otherwise its a directory and we need to archive all files
420          LOG.trace("{} is a directory, archiving children files", file);
421          // so we add the directory name to the one base archive
422          Path parentArchiveDir = new Path(baseArchiveDir, file.getName());
423          // and then get all the files from that directory and attempt to
424          // archive those too
425          Collection<File> children = file.getChildren();
426          failures.addAll(resolveAndArchive(fs, parentArchiveDir, children, start));
427        }
428      } catch (IOException e) {
429        LOG.warn("Failed to archive {}", file, e);
430        failures.add(file);
431      }
432    }
433    return failures;
434  }
435
436  /**
437   * Attempt to archive the passed in file to the archive directory.
438   * <p>
439   * If the same file already exists in the archive, it is moved to a timestamped directory under
440   * the archive directory and the new file is put in its place.
441   * @param archiveDir {@link Path} to the directory that stores the archives of the hfiles
442   * @param currentFile {@link Path} to the original HFile that will be archived
443   * @param archiveStartTime time the archiving started, to resolve naming conflicts
444   * @return <tt>true</tt> if the file is successfully archived. <tt>false</tt> if there was a
445   *         problem, but the operation still completed.
446   * @throws IOException on failure to complete {@link FileSystem} operations.
447   */
448  private static boolean resolveAndArchiveFile(Path archiveDir, File currentFile,
449      String archiveStartTime) throws IOException {
450    // build path as it should be in the archive
451    String filename = currentFile.getName();
452    Path archiveFile = new Path(archiveDir, filename);
453    FileSystem fs = currentFile.getFileSystem();
454
455    // if the file already exists in the archive, move that one to a timestamped backup. This is a
456    // really, really unlikely situtation, where we get the same name for the existing file, but
457    // is included just for that 1 in trillion chance.
458    if (fs.exists(archiveFile)) {
459      LOG.debug("{} already exists in archive, moving to timestamped backup and " +
460          "overwriting current.", archiveFile);
461
462      // move the archive file to the stamped backup
463      Path backedupArchiveFile = new Path(archiveDir, filename + SEPARATOR + archiveStartTime);
464      if (!fs.rename(archiveFile, backedupArchiveFile)) {
465        LOG.error("Could not rename archive file to backup: " + backedupArchiveFile
466            + ", deleting existing file in favor of newer.");
467        // try to delete the exisiting file, if we can't rename it
468        if (!fs.delete(archiveFile, false)) {
469          throw new IOException("Couldn't delete existing archive file (" + archiveFile
470              + ") or rename it to the backup file (" + backedupArchiveFile
471              + ") to make room for similarly named file.");
472        }
473      }
474      LOG.debug("Backed up archive file from " + archiveFile);
475    }
476
477    LOG.trace("No existing file in archive for {}, free to archive original file.", archiveFile);
478
479    // at this point, we should have a free spot for the archive file
480    boolean success = false;
481    for (int i = 0; !success && i < DEFAULT_RETRIES_NUMBER; ++i) {
482      if (i > 0) {
483        // Ensure that the archive directory exists.
484        // The previous "move to archive" operation has failed probably because
485        // the cleaner has removed our archive directory (HBASE-7643).
486        // (we're in a retry loop, so don't worry too much about the exception)
487        try {
488          if (!fs.exists(archiveDir)) {
489            if (fs.mkdirs(archiveDir)) {
490              LOG.debug("Created archive directory {}", archiveDir);
491            }
492          }
493        } catch (IOException e) {
494          LOG.warn("Failed to create directory {}", archiveDir, e);
495        }
496      }
497
498      try {
499        success = currentFile.moveAndClose(archiveFile);
500      } catch (FileNotFoundException fnfe) {
501        LOG.warn("Failed to archive " + currentFile +
502            " because it does not exist! Skipping and continuing on.", fnfe);
503        success = true;
504      } catch (IOException e) {
505        LOG.warn("Failed to archive " + currentFile + " on try #" + i, e);
506        success = false;
507      }
508    }
509
510    if (!success) {
511      LOG.error("Failed to archive " + currentFile);
512      return false;
513    }
514
515    LOG.debug("Archived from {} to {}", currentFile, archiveFile);
516    return true;
517  }
518
519  /**
520   * Without regard for backup, delete a region. Should be used with caution.
521   * @param regionDir {@link Path} to the region to be deleted.
522   * @param fs FileSystem from which to delete the region
523   * @return <tt>true</tt> on successful deletion, <tt>false</tt> otherwise
524   * @throws IOException on filesystem operation failure
525   */
526  private static boolean deleteRegionWithoutArchiving(FileSystem fs, Path regionDir)
527      throws IOException {
528    if (fs.delete(regionDir, true)) {
529      LOG.debug("Deleted {}", regionDir);
530      return true;
531    }
532    LOG.debug("Failed to delete directory {}", regionDir);
533    return false;
534  }
535
536  /**
537   * Just do a simple delete of the given store files
538   * <p>
539   * A best effort is made to delete each of the files, rather than bailing on the first failure.
540   * <p>
541   * @param compactedFiles store files to delete from the file system.
542   * @throws IOException if a file cannot be deleted. All files will be attempted to deleted before
543   *           throwing the exception, rather than failing at the first file.
544   */
545  private static void deleteStoreFilesWithoutArchiving(Collection<HStoreFile> compactedFiles)
546      throws IOException {
547    LOG.debug("Deleting files without archiving.");
548    List<IOException> errors = new ArrayList<>(0);
549    for (HStoreFile hsf : compactedFiles) {
550      try {
551        hsf.deleteStoreFile();
552      } catch (IOException e) {
553        LOG.error("Failed to delete {}", hsf.getPath());
554        errors.add(e);
555      }
556    }
557    if (errors.size() > 0) {
558      throw MultipleIOException.createIOException(errors);
559    }
560  }
561
562  /**
563   * Adapt a type to match the {@link File} interface, which is used internally for handling
564   * archival/removal of files
565   * @param <T> type to adapt to the {@link File} interface
566   */
567  private static abstract class FileConverter<T> implements Function<T, File> {
568    protected final FileSystem fs;
569
570    public FileConverter(FileSystem fs) {
571      this.fs = fs;
572    }
573  }
574
575  /**
576   * Convert a FileStatus to something we can manage in the archiving
577   */
578  private static class FileStatusConverter extends FileConverter<FileStatus> {
579    public FileStatusConverter(FileSystem fs) {
580      super(fs);
581    }
582
583    @Override
584    public File apply(FileStatus input) {
585      return new FileablePath(fs, input.getPath());
586    }
587  }
588
589  /**
590   * Convert the {@link HStoreFile} into something we can manage in the archive
591   * methods
592   */
593  private static class StoreToFile extends FileConverter<HStoreFile> {
594    public StoreToFile(FileSystem fs) {
595      super(fs);
596    }
597
598    @Override
599    public File apply(HStoreFile input) {
600      return new FileableStoreFile(fs, input);
601    }
602  }
603
604  /**
605   * Wrapper to handle file operations uniformly
606   */
607  private static abstract class File {
608    protected final FileSystem fs;
609
610    public File(FileSystem fs) {
611      this.fs = fs;
612    }
613
614    /**
615     * Delete the file
616     * @throws IOException on failure
617     */
618    abstract void delete() throws IOException;
619
620    /**
621     * Check to see if this is a file or a directory
622     * @return <tt>true</tt> if it is a file, <tt>false</tt> otherwise
623     * @throws IOException on {@link FileSystem} connection error
624     */
625    abstract boolean isFile() throws IOException;
626
627    /**
628     * @return if this is a directory, returns all the children in the
629     *         directory, otherwise returns an empty list
630     * @throws IOException
631     */
632    abstract Collection<File> getChildren() throws IOException;
633
634    /**
635     * close any outside readers of the file
636     * @throws IOException
637     */
638    abstract void close() throws IOException;
639
640    /**
641     * @return the name of the file (not the full fs path, just the individual
642     *         file name)
643     */
644    abstract String getName();
645
646    /**
647     * @return the path to this file
648     */
649    abstract Path getPath();
650
651    /**
652     * Move the file to the given destination
653     * @param dest
654     * @return <tt>true</tt> on success
655     * @throws IOException
656     */
657    public boolean moveAndClose(Path dest) throws IOException {
658      this.close();
659      Path p = this.getPath();
660      return FSUtils.renameAndSetModifyTime(fs, p, dest);
661    }
662
663    /**
664     * @return the {@link FileSystem} on which this file resides
665     */
666    public FileSystem getFileSystem() {
667      return this.fs;
668    }
669
670    @Override
671    public String toString() {
672      return this.getClass().getSimpleName() + ", " + getPath().toString();
673    }
674  }
675
676  /**
677   * A {@link File} that wraps a simple {@link Path} on a {@link FileSystem}.
678   */
679  private static class FileablePath extends File {
680    private final Path file;
681    private final FileStatusConverter getAsFile;
682
683    public FileablePath(FileSystem fs, Path file) {
684      super(fs);
685      this.file = file;
686      this.getAsFile = new FileStatusConverter(fs);
687    }
688
689    @Override
690    public void delete() throws IOException {
691      if (!fs.delete(file, true)) throw new IOException("Failed to delete:" + this.file);
692    }
693
694    @Override
695    public String getName() {
696      return file.getName();
697    }
698
699    @Override
700    public Collection<File> getChildren() throws IOException {
701      if (fs.isFile(file)) return Collections.emptyList();
702      return Collections2.transform(Arrays.asList(fs.listStatus(file)), getAsFile);
703    }
704
705    @Override
706    public boolean isFile() throws IOException {
707      return fs.isFile(file);
708    }
709
710    @Override
711    public void close() throws IOException {
712      // NOOP - files are implicitly closed on removal
713    }
714
715    @Override
716    Path getPath() {
717      return file;
718    }
719  }
720
721  /**
722   * {@link File} adapter for a {@link HStoreFile} living on a {@link FileSystem}
723   * .
724   */
725  private static class FileableStoreFile extends File {
726    HStoreFile file;
727
728    public FileableStoreFile(FileSystem fs, HStoreFile store) {
729      super(fs);
730      this.file = store;
731    }
732
733    @Override
734    public void delete() throws IOException {
735      file.deleteStoreFile();
736    }
737
738    @Override
739    public String getName() {
740      return file.getPath().getName();
741    }
742
743    @Override
744    public boolean isFile() {
745      return true;
746    }
747
748    @Override
749    public Collection<File> getChildren() throws IOException {
750      // storefiles don't have children
751      return Collections.emptyList();
752    }
753
754    @Override
755    public void close() throws IOException {
756      file.closeStoreFile(true);
757    }
758
759    @Override
760    Path getPath() {
761      return file.getPath();
762    }
763  }
764}