public void restore()

in priam/src/main/java/com/netflix/priam/restore/AbstractRestore.java [191:301]


    public void restore(DateUtil.DateRange dateRange) throws Exception {
        // fail early if post restore hook has invalid parameters
        if (!postRestoreHook.hasValidParameters()) {
            throw new PostRestoreHookException("Invalid PostRestoreHook parameters");
        }

        Date endTime = new Date(dateRange.getEndTime().toEpochMilli());
        IMetaProxy metaProxy = metaV1Proxy;
        if (backupRestoreConfig.enableV2Restore()) metaProxy = metaV2Proxy;

        // Set the restore status.
        instanceState.getRestoreStatus().resetStatus();
        instanceState
                .getRestoreStatus()
                .setStartDateRange(
                        LocalDateTime.ofInstant(dateRange.getStartTime(), ZoneId.of("UTC")));
        instanceState.getRestoreStatus().setEndDateRange(DateUtil.convert(endTime));
        instanceState.getRestoreStatus().setExecutionStartTime(LocalDateTime.now());
        instanceState.setRestoreStatus(Status.STARTED);
        String origToken = instanceIdentity.getInstance().getToken();

        try {
            if (config.isRestoreClosestToken()) {
                restoreToken =
                        tokenSelector.getClosestToken(
                                new BigInteger(origToken),
                                new Date(dateRange.getStartTime().toEpochMilli()));
                instanceIdentity.getInstance().setToken(restoreToken.toString());
            }

            // Stop cassandra if its running
            stopCassProcess();

            // Cleanup local data
            File dataDir = new File(config.getDataFileLocation());
            if (dataDir.exists() && dataDir.isDirectory()) FileUtils.cleanDirectory(dataDir);

            // Find latest valid meta file.
            Optional<AbstractBackupPath> latestValidMetaFile =
                    BackupRestoreUtil.getLatestValidMetaPath(metaProxy, dateRange);

            if (!latestValidMetaFile.isPresent()) {
                logger.info("No valid snapshot meta file found, Restore Failed.");
                instanceState.getRestoreStatus().setExecutionEndTime(LocalDateTime.now());
                instanceState.setRestoreStatus(Status.FAILED);
                return;
            }

            logger.info(
                    "Snapshot Meta file for restore {}", latestValidMetaFile.get().getRemotePath());
            instanceState
                    .getRestoreStatus()
                    .setSnapshotMetaFile(latestValidMetaFile.get().getRemotePath());

            List<AbstractBackupPath> allFiles =
                    BackupRestoreUtil.getMostRecentSnapshotPaths(
                            latestValidMetaFile.get(), metaProxy, pathProvider);
            if (!config.skipIncrementalRestore()) {
                allFiles.addAll(
                        BackupRestoreUtil.getIncrementalPaths(
                                latestValidMetaFile.get(), dateRange, metaProxy));
            }

            // Download snapshot which is listed in the meta file.
            List<Future<Path>> futureList = new ArrayList<>();
            futureList.addAll(download(allFiles.iterator(), false));

            // Downloading CommitLogs
            // Note for Backup V2.0 we do not backup commit logs, as saving them is cost-expensive.
            if (config.isBackingUpCommitLogs()) {
                logger.info(
                        "Delete all backuped commitlog files in {}",
                        config.getBackupCommitLogLocation());
                SystemUtils.cleanupDir(config.getBackupCommitLogLocation(), null);

                logger.info("Delete all commitlog files in {}", config.getCommitLogLocation());
                SystemUtils.cleanupDir(config.getCommitLogLocation(), null);
                String prefix = fs.getPrefix().toString();
                Iterator<AbstractBackupPath> commitLogPathIterator =
                        fs.list(prefix, latestValidMetaFile.get().getTime(), endTime);
                futureList.addAll(
                        downloadCommitLogs(
                                commitLogPathIterator, config.maxCommitLogsRestore(), false));
            }

            // Wait for all the futures to finish.
            waitForCompletion(futureList);

            // Given that files are restored now, kick off post restore hook
            logger.info("Starting post restore hook");
            postRestoreHook.execute();
            logger.info("Completed executing post restore hook");

            // Declare restore as finished.
            instanceState.getRestoreStatus().setExecutionEndTime(LocalDateTime.now());
            instanceState.setRestoreStatus(Status.FINISHED);

            // Start cassandra if restore is successful.
            if (!config.doesCassandraStartManually()) cassProcess.start(true);
            else
                logger.info(
                        "config.doesCassandraStartManually() is set to True, hence Cassandra needs to be started manually ...");
        } catch (Exception e) {
            instanceState.setRestoreStatus(Status.FAILED);
            instanceState.getRestoreStatus().setExecutionEndTime(LocalDateTime.now());
            logger.error("Error while trying to restore: {}", e.getMessage(), e);
            throw e;
        } finally {
            instanceIdentity.getInstance().setToken(origToken);
        }
    }