Java Code Examples for org.apache.hadoop.hbase.snapshot.SnapshotManifest#getTableDescriptor()

The following examples show how to use org.apache.hadoop.hbase.snapshot.SnapshotManifest#getTableDescriptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestoreTool.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Get table descriptor
 * @param tableName is the table backed up
 * @return {@link TableDescriptor} saved in backup image of the table
 */
TableDescriptor getTableDesc(TableName tableName) throws IOException {
  Path tableInfoPath = this.getTableInfoPath(tableName);
  SnapshotDescription desc = SnapshotDescriptionUtils.readSnapshotInfo(fs, tableInfoPath);
  SnapshotManifest manifest = SnapshotManifest.open(conf, fs, tableInfoPath, desc);
  TableDescriptor tableDescriptor = manifest.getTableDescriptor();
  if (!tableDescriptor.getTableName().equals(tableName)) {
    LOG.error("couldn't find Table Desc for table: " + tableName + " under tableInfoPath: "
            + tableInfoPath.toString());
    LOG.error("tableDescriptor.getNameAsString() = "
            + tableDescriptor.getTableName().getNameAsString());
    throw new FileNotFoundException("couldn't find Table Desc for table: " + tableName
        + " under tableInfoPath: " + tableInfoPath.toString());
  }
  return tableDescriptor;
}
 
Example 2
Source File: TableSnapshotScanner.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void openWithoutRestoringSnapshot() throws IOException {
  Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
  SnapshotProtos.SnapshotDescription snapshotDesc =
      SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);

  SnapshotManifest manifest = SnapshotManifest.open(conf, fs, snapshotDir, snapshotDesc);
  List<SnapshotRegionManifest> regionManifests = manifest.getRegionManifests();
  if (regionManifests == null) {
    throw new IllegalArgumentException("Snapshot seems empty, snapshotName: " + snapshotName);
  }

  regions = new ArrayList<>(regionManifests.size());
  regionManifests.stream().map(r -> ProtobufUtil.toRegionInfo(r.getRegionInfo()))
    .filter(this::isValidRegion).sorted().forEach(r -> regions.add(r));
  htd = manifest.getTableDescriptor();
}
 
Example 3
Source File: MasterSnapshotVerifier.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Check that the table descriptor for the snapshot is a valid table descriptor
 * @param manifest snapshot manifest to inspect
 */
private void verifyTableInfo(final SnapshotManifest manifest) throws IOException {
  TableDescriptor htd = manifest.getTableDescriptor();
  if (htd == null) {
    throw new CorruptedSnapshotException("Missing Table Descriptor",
      ProtobufUtil.createSnapshotDesc(snapshot));
  }

  if (!htd.getTableName().getNameAsString().equals(snapshot.getTable())) {
    throw new CorruptedSnapshotException(
        "Invalid Table Descriptor. Expected " + snapshot.getTable() + " name, got "
            + htd.getTableName().getNameAsString(), ProtobufUtil.createSnapshotDesc(snapshot));
  }
}
 
Example 4
Source File: RestoreTool.java    From hbase with Apache License 2.0 4 votes vote down vote up
private void createAndRestoreTable(Connection conn, TableName tableName, TableName newTableName,
    Path tableBackupPath, boolean truncateIfExists, String lastIncrBackupId) throws IOException {
  if (newTableName == null) {
    newTableName = tableName;
  }
  FileSystem fileSys = tableBackupPath.getFileSystem(this.conf);

  // get table descriptor first
  TableDescriptor tableDescriptor = getTableDescriptor(fileSys, tableName, lastIncrBackupId);
  if (tableDescriptor != null) {
    LOG.debug("Retrieved descriptor: " + tableDescriptor + " thru " + lastIncrBackupId);
  }

  if (tableDescriptor == null) {
    Path tableSnapshotPath = getTableSnapshotPath(backupRootPath, tableName, backupId);
    if (fileSys.exists(tableSnapshotPath)) {
      // snapshot path exist means the backup path is in HDFS
      // check whether snapshot dir already recorded for target table
      if (snapshotMap.get(tableName) != null) {
        SnapshotDescription desc =
            SnapshotDescriptionUtils.readSnapshotInfo(fileSys, tableSnapshotPath);
        SnapshotManifest manifest = SnapshotManifest.open(conf, fileSys, tableSnapshotPath, desc);
        tableDescriptor = manifest.getTableDescriptor();
      } else {
        tableDescriptor = getTableDesc(tableName);
        snapshotMap.put(tableName, getTableInfoPath(tableName));
      }
      if (tableDescriptor == null) {
        LOG.debug("Found no table descriptor in the snapshot dir, previous schema would be lost");
      }
    } else {
      throw new IOException("Table snapshot directory: " +
          tableSnapshotPath + " does not exist.");
    }
  }

  Path tableArchivePath = getTableArchivePath(tableName);
  if (tableArchivePath == null) {
    if (tableDescriptor != null) {
      // find table descriptor but no archive dir means the table is empty, create table and exit
      if (LOG.isDebugEnabled()) {
        LOG.debug("find table descriptor but no archive dir for table " + tableName
            + ", will only create table");
      }
      tableDescriptor = TableDescriptorBuilder.copy(newTableName, tableDescriptor);
      checkAndCreateTable(conn, tableBackupPath, tableName, newTableName, null, tableDescriptor,
        truncateIfExists);
      return;
    } else {
      throw new IllegalStateException("Cannot restore hbase table because directory '"
          + " tableArchivePath is null.");
    }
  }

  if (tableDescriptor == null) {
    tableDescriptor = TableDescriptorBuilder.newBuilder(newTableName).build();
  } else {
    tableDescriptor = TableDescriptorBuilder.copy(newTableName, tableDescriptor);
  }

  // record all region dirs:
  // load all files in dir
  try {
    ArrayList<Path> regionPathList = getRegionList(tableName);

    // should only try to create the table with all region informations, so we could pre-split
    // the regions in fine grain
    checkAndCreateTable(conn, tableBackupPath, tableName, newTableName, regionPathList,
      tableDescriptor, truncateIfExists);
    RestoreJob restoreService = BackupRestoreFactory.getRestoreJob(conf);
    Path[] paths = new Path[regionPathList.size()];
    regionPathList.toArray(paths);
    restoreService.run(paths, new TableName[]{tableName}, new TableName[] {newTableName}, true);

  } catch (Exception e) {
    LOG.error(e.toString(), e);
    throw new IllegalStateException("Cannot restore hbase table", e);
  }
}
 
Example 5
Source File: SnapshotManager.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Restore or Clone the specified snapshot
 * @param reqSnapshot
 * @param nonceKey unique identifier to prevent duplicated RPC
 * @throws IOException
 */
public long restoreOrCloneSnapshot(final SnapshotDescription reqSnapshot, final NonceKey nonceKey,
    final boolean restoreAcl) throws IOException {
  FileSystem fs = master.getMasterFileSystem().getFileSystem();
  Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(reqSnapshot, rootDir);

  // check if the snapshot exists
  if (!fs.exists(snapshotDir)) {
    LOG.error("A Snapshot named '" + reqSnapshot.getName() + "' does not exist.");
    throw new SnapshotDoesNotExistException(
      ProtobufUtil.createSnapshotDesc(reqSnapshot));
  }

  // Get snapshot info from file system. The reqSnapshot is a "fake" snapshotInfo with
  // just the snapshot "name" and table name to restore. It does not contains the "real" snapshot
  // information.
  SnapshotDescription snapshot = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);
  SnapshotManifest manifest = SnapshotManifest.open(master.getConfiguration(), fs,
      snapshotDir, snapshot);
  TableDescriptor snapshotTableDesc = manifest.getTableDescriptor();
  TableName tableName = TableName.valueOf(reqSnapshot.getTable());

  // sanity check the new table descriptor
  TableDescriptorChecker.sanityCheck(master.getConfiguration(), snapshotTableDesc);

  // stop tracking "abandoned" handlers
  cleanupSentinels();

  // Verify snapshot validity
  SnapshotReferenceUtil.verifySnapshot(master.getConfiguration(), fs, manifest);

  // Execute the restore/clone operation
  long procId;
  if (MetaTableAccessor.tableExists(master.getConnection(), tableName)) {
    procId = restoreSnapshot(reqSnapshot, tableName, snapshot, snapshotTableDesc, nonceKey,
      restoreAcl);
  } else {
    procId =
        cloneSnapshot(reqSnapshot, tableName, snapshot, snapshotTableDesc, nonceKey, restoreAcl);
  }
  return procId;
}