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