Search in sources :

Example 1 with HoodieRestorePlan

use of org.apache.hudi.avro.model.HoodieRestorePlan in project hudi by apache.

the class BaseHoodieWriteClient method restoreToInstant.

/**
 * NOTE : This action requires all writers (ingest and compact) to a table to be stopped before proceeding. Revert
 * the (inflight/committed) record changes for all commits after the provided instant time.
 *
 * @param instantTime Instant time to which restoration is requested
 */
public HoodieRestoreMetadata restoreToInstant(final String instantTime) throws HoodieRestoreException {
    LOG.info("Begin restore to instant " + instantTime);
    final String restoreInstantTime = HoodieActiveTimeline.createNewInstantTime();
    Timer.Context timerContext = metrics.getRollbackCtx();
    try {
        HoodieTable<T, I, K, O> table = initTable(WriteOperationType.UNKNOWN, Option.empty());
        Option<HoodieRestorePlan> restorePlanOption = table.scheduleRestore(context, restoreInstantTime, instantTime);
        if (restorePlanOption.isPresent()) {
            HoodieRestoreMetadata restoreMetadata = table.restore(context, restoreInstantTime, instantTime);
            if (timerContext != null) {
                final long durationInMs = metrics.getDurationInMs(timerContext.stop());
                final long totalFilesDeleted = restoreMetadata.getHoodieRestoreMetadata().values().stream().flatMap(Collection::stream).mapToLong(HoodieRollbackMetadata::getTotalFilesDeleted).sum();
                metrics.updateRollbackMetrics(durationInMs, totalFilesDeleted);
            }
            return restoreMetadata;
        } else {
            throw new HoodieRestoreException("Failed to restore " + config.getBasePath() + " to commit " + instantTime);
        }
    } catch (Exception e) {
        throw new HoodieRestoreException("Failed to restore to " + instantTime, e);
    }
}
Also used : HoodieRestoreMetadata(org.apache.hudi.avro.model.HoodieRestoreMetadata) HoodieRestorePlan(org.apache.hudi.avro.model.HoodieRestorePlan) HoodieException(org.apache.hudi.exception.HoodieException) ParseException(java.text.ParseException) HoodieRollbackException(org.apache.hudi.exception.HoodieRollbackException) HoodieRestoreException(org.apache.hudi.exception.HoodieRestoreException) HoodieCommitException(org.apache.hudi.exception.HoodieCommitException) HoodieSavepointException(org.apache.hudi.exception.HoodieSavepointException) IOException(java.io.IOException) HoodieIOException(org.apache.hudi.exception.HoodieIOException) Timer(com.codahale.metrics.Timer) Collection(java.util.Collection) HoodieRestoreException(org.apache.hudi.exception.HoodieRestoreException)

Example 2 with HoodieRestorePlan

use of org.apache.hudi.avro.model.HoodieRestorePlan in project hudi by apache.

the class BaseRestoreActionExecutor method getInstantsToRollback.

private List<HoodieInstant> getInstantsToRollback(HoodieInstant restoreInstant) throws IOException {
    List<HoodieInstant> instantsToRollback = new ArrayList<>();
    HoodieRestorePlan restorePlan = RestoreUtils.getRestorePlan(table.getMetaClient(), restoreInstant);
    for (HoodieInstantInfo instantInfo : restorePlan.getInstantsToRollback()) {
        // If restore crashed mid-way, there are chances that some commits are already rolled back,
        // but some are not. so, we can ignore those commits which are fully rolledback in previous attempt if any.
        Option<HoodieInstant> rollbackInstantOpt = table.getActiveTimeline().getWriteTimeline().filter(instant -> instant.getTimestamp().equals(instantInfo.getCommitTime()) && instant.getAction().equals(instantInfo.getAction())).firstInstant();
        if (rollbackInstantOpt.isPresent()) {
            instantsToRollback.add(rollbackInstantOpt.get());
        } else {
            LOG.warn("Ignoring already rolledback instant " + instantInfo.toString());
        }
    }
    return instantsToRollback;
}
Also used : HoodieInstant(org.apache.hudi.common.table.timeline.HoodieInstant) HoodieTable(org.apache.hudi.table.HoodieTable) BaseActionExecutor(org.apache.hudi.table.action.BaseActionExecutor) HoodieRestorePlan(org.apache.hudi.avro.model.HoodieRestorePlan) HoodieRestoreException(org.apache.hudi.exception.HoodieRestoreException) HoodieInstant(org.apache.hudi.common.table.timeline.HoodieInstant) Option(org.apache.hudi.common.util.Option) HashMap(java.util.HashMap) HoodieEngineContext(org.apache.hudi.common.engine.HoodieEngineContext) TransactionManager(org.apache.hudi.client.transaction.TransactionManager) HoodieTimer(org.apache.hudi.common.util.HoodieTimer) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) Map(java.util.Map) HoodieRollbackMetadata(org.apache.hudi.avro.model.HoodieRollbackMetadata) HoodieActiveTimeline(org.apache.hudi.common.table.timeline.HoodieActiveTimeline) HoodieTimeline(org.apache.hudi.common.table.timeline.HoodieTimeline) ValidationUtils(org.apache.hudi.common.util.ValidationUtils) HoodieRollbackException(org.apache.hudi.exception.HoodieRollbackException) HoodieWriteConfig(org.apache.hudi.config.HoodieWriteConfig) TimelineMetadataUtils(org.apache.hudi.common.table.timeline.TimelineMetadataUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) HoodieInstantInfo(org.apache.hudi.avro.model.HoodieInstantInfo) HoodieRecordPayload(org.apache.hudi.common.model.HoodieRecordPayload) List(java.util.List) HoodieRestoreMetadata(org.apache.hudi.avro.model.HoodieRestoreMetadata) LogManager(org.apache.log4j.LogManager) Collections(java.util.Collections) HoodieInstantInfo(org.apache.hudi.avro.model.HoodieInstantInfo) ArrayList(java.util.ArrayList) HoodieRestorePlan(org.apache.hudi.avro.model.HoodieRestorePlan)

Example 3 with HoodieRestorePlan

use of org.apache.hudi.avro.model.HoodieRestorePlan in project hudi by apache.

the class RestorePlanActionExecutor method execute.

@Override
public Option<HoodieRestorePlan> execute() {
    final HoodieInstant restoreInstant = new HoodieInstant(HoodieInstant.State.REQUESTED, HoodieTimeline.RESTORE_ACTION, instantTime);
    try {
        // Get all the commits on the timeline after the provided commit time
        // rollback pending clustering instants first before other instants (See HUDI-3362)
        List<HoodieInstant> pendingClusteringInstantsToRollback = table.getActiveTimeline().filterPendingReplaceTimeline().filter(instant -> ClusteringUtils.isPendingClusteringInstant(table.getMetaClient(), instant)).getReverseOrderedInstants().filter(instant -> HoodieActiveTimeline.GREATER_THAN.test(instant.getTimestamp(), restoreInstantTime)).collect(Collectors.toList());
        // Get all the commits on the timeline after the provided commit time
        List<HoodieInstant> commitInstantsToRollback = table.getActiveTimeline().getWriteTimeline().getReverseOrderedInstants().filter(instant -> HoodieActiveTimeline.GREATER_THAN.test(instant.getTimestamp(), restoreInstantTime)).filter(instant -> !pendingClusteringInstantsToRollback.contains(instant)).collect(Collectors.toList());
        // Combine both lists - first rollback pending clustering and then rollback all other commits
        List<HoodieInstantInfo> instantsToRollback = Stream.concat(pendingClusteringInstantsToRollback.stream(), commitInstantsToRollback.stream()).map(entry -> new HoodieInstantInfo(entry.getTimestamp(), entry.getAction())).collect(Collectors.toList());
        HoodieRestorePlan restorePlan = new HoodieRestorePlan(instantsToRollback, LATEST_RESTORE_PLAN_VERSION);
        table.getActiveTimeline().saveToRestoreRequested(restoreInstant, TimelineMetadataUtils.serializeRestorePlan(restorePlan));
        table.getMetaClient().reloadActiveTimeline();
        LOG.info("Requesting Restore with instant time " + restoreInstant);
        return Option.of(restorePlan);
    } catch (IOException e) {
        LOG.error("Got exception when saving restore requested file", e);
        throw new HoodieIOException(e.getMessage(), e);
    }
}
Also used : HoodieInstant(org.apache.hudi.common.table.timeline.HoodieInstant) HoodieTable(org.apache.hudi.table.HoodieTable) HoodieWriteConfig(org.apache.hudi.config.HoodieWriteConfig) BaseActionExecutor(org.apache.hudi.table.action.BaseActionExecutor) HoodieRestorePlan(org.apache.hudi.avro.model.HoodieRestorePlan) HoodieInstant(org.apache.hudi.common.table.timeline.HoodieInstant) TimelineMetadataUtils(org.apache.hudi.common.table.timeline.TimelineMetadataUtils) Option(org.apache.hudi.common.util.Option) IOException(java.io.IOException) HoodieEngineContext(org.apache.hudi.common.engine.HoodieEngineContext) Collectors(java.util.stream.Collectors) HoodieInstantInfo(org.apache.hudi.avro.model.HoodieInstantInfo) HoodieRecordPayload(org.apache.hudi.common.model.HoodieRecordPayload) Logger(org.apache.log4j.Logger) List(java.util.List) Stream(java.util.stream.Stream) ClusteringUtils(org.apache.hudi.common.util.ClusteringUtils) HoodieIOException(org.apache.hudi.exception.HoodieIOException) LogManager(org.apache.log4j.LogManager) HoodieActiveTimeline(org.apache.hudi.common.table.timeline.HoodieActiveTimeline) HoodieTimeline(org.apache.hudi.common.table.timeline.HoodieTimeline) HoodieIOException(org.apache.hudi.exception.HoodieIOException) HoodieInstantInfo(org.apache.hudi.avro.model.HoodieInstantInfo) IOException(java.io.IOException) HoodieIOException(org.apache.hudi.exception.HoodieIOException) HoodieRestorePlan(org.apache.hudi.avro.model.HoodieRestorePlan)

Aggregations

IOException (java.io.IOException)3 HoodieRestorePlan (org.apache.hudi.avro.model.HoodieRestorePlan)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 HoodieInstantInfo (org.apache.hudi.avro.model.HoodieInstantInfo)2 HoodieRestoreMetadata (org.apache.hudi.avro.model.HoodieRestoreMetadata)2 HoodieEngineContext (org.apache.hudi.common.engine.HoodieEngineContext)2 HoodieRecordPayload (org.apache.hudi.common.model.HoodieRecordPayload)2 HoodieActiveTimeline (org.apache.hudi.common.table.timeline.HoodieActiveTimeline)2 HoodieInstant (org.apache.hudi.common.table.timeline.HoodieInstant)2 HoodieTimeline (org.apache.hudi.common.table.timeline.HoodieTimeline)2 TimelineMetadataUtils (org.apache.hudi.common.table.timeline.TimelineMetadataUtils)2 Option (org.apache.hudi.common.util.Option)2 HoodieWriteConfig (org.apache.hudi.config.HoodieWriteConfig)2 HoodieIOException (org.apache.hudi.exception.HoodieIOException)2 HoodieRestoreException (org.apache.hudi.exception.HoodieRestoreException)2 HoodieRollbackException (org.apache.hudi.exception.HoodieRollbackException)2 HoodieTable (org.apache.hudi.table.HoodieTable)2 BaseActionExecutor (org.apache.hudi.table.action.BaseActionExecutor)2 LogManager (org.apache.log4j.LogManager)2