/**
   * Restore region by removing files not in the snapshot and adding the missing ones from the
   * snapshot.
   */
  private void restoreRegion(HRegionInfo regionInfo) throws IOException {
    Path snapshotRegionDir = new Path(snapshotDir, regionInfo.getEncodedName());
    Map<String, List<String>> snapshotFiles =
        SnapshotReferenceUtil.getRegionHFileReferences(fs, snapshotRegionDir);
    Path regionDir = new Path(tableDir, regionInfo.getEncodedName());
    String tableName = tableDesc.getNameAsString();

    // Restore families present in the table
    for (Path familyDir : FSUtils.getFamilyDirs(fs, regionDir)) {
      byte[] family = Bytes.toBytes(familyDir.getName());
      Set<String> familyFiles = getTableRegionFamilyFiles(familyDir);
      List<String> snapshotFamilyFiles = snapshotFiles.remove(familyDir.getName());
      if (snapshotFamilyFiles != null) {
        List<String> hfilesToAdd = new LinkedList<String>();
        for (String hfileName : snapshotFamilyFiles) {
          if (familyFiles.contains(hfileName)) {
            // HFile already present
            familyFiles.remove(hfileName);
          } else {
            // HFile missing
            hfilesToAdd.add(hfileName);
          }
        }

        // Restore Missing files
        for (String hfileName : hfilesToAdd) {
          LOG.trace(
              "Adding HFileLink "
                  + hfileName
                  + " to region="
                  + regionInfo.getEncodedName()
                  + " table="
                  + tableName);
          restoreStoreFile(familyDir, regionInfo, hfileName);
        }

        // Remove hfiles not present in the snapshot
        for (String hfileName : familyFiles) {
          Path hfile = new Path(familyDir, hfileName);
          LOG.trace(
              "Removing hfile="
                  + hfile
                  + " from region="
                  + regionInfo.getEncodedName()
                  + " table="
                  + tableName);
          HFileArchiver.archiveStoreFile(fs, regionInfo, conf, tableDir, family, hfile);
        }
      } else {
        // Family doesn't exists in the snapshot
        LOG.trace(
            "Removing family="
                + Bytes.toString(family)
                + " from region="
                + regionInfo.getEncodedName()
                + " table="
                + tableName);
        HFileArchiver.archiveFamily(fs, conf, regionInfo, tableDir, family);
        fs.delete(familyDir, true);
      }
    }

    // Add families not present in the table
    for (Map.Entry<String, List<String>> familyEntry : snapshotFiles.entrySet()) {
      Path familyDir = new Path(regionDir, familyEntry.getKey());
      if (!fs.mkdirs(familyDir)) {
        throw new IOException("Unable to create familyDir=" + familyDir);
      }

      for (String hfileName : familyEntry.getValue()) {
        LOG.trace("Adding HFileLink " + hfileName + " to table=" + tableName);
        restoreStoreFile(familyDir, regionInfo, hfileName);
      }
    }
  }
Esempio n. 2
0
 /**
  * Archives the specified store file from the specified family.
  *
  * @param familyName Family that contains the store files
  * @param filePath {@link Path} to the store file to remove
  * @throws IOException if the archiving fails
  */
 public void removeStoreFile(final String familyName, final Path filePath) throws IOException {
   HFileArchiver.archiveStoreFile(
       this.conf, this.fs, this.regionInfo, this.tableDir, Bytes.toBytes(familyName), filePath);
 }