All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.uber.hoodie.common.util.TimelineDiffHelper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2019 Uber Technologies, Inc. ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.uber.hoodie.common.util;

import com.uber.hoodie.common.table.HoodieTimeline;
import com.uber.hoodie.common.table.timeline.HoodieInstant;
import com.uber.hoodie.common.table.timeline.HoodieInstant.State;
import com.uber.hoodie.common.util.collection.Pair;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


public class TimelineDiffHelper {

  protected static Logger log = LogManager.getLogger(TimelineDiffHelper.class);

  public static TimelineDiffResult getNewInstantsForIncrementalSync(HoodieTimeline oldTimeline,
      HoodieTimeline newTimeline) {

    HoodieTimeline oldT = oldTimeline.filterCompletedAndCompactionInstants();
    HoodieTimeline newT = newTimeline.filterCompletedAndCompactionInstants();

    Optional lastSeenInstant = oldT.lastInstant();
    Optional firstInstantInNewTimeline = newT.firstInstant();

    if (lastSeenInstant.isPresent() && firstInstantInNewTimeline.isPresent()) {
      if (HoodieTimeline.compareTimestamps(lastSeenInstant.get().getTimestamp(),
          firstInstantInNewTimeline.get().getTimestamp(), HoodieTimeline.LESSER)) {
        // The last seen instant is no longer in the timeline. Do not incrementally Sync.
        return TimelineDiffResult.UNSAFE_SYNC_RESULT;
      }
      Set oldTimelineInstants = oldT.getInstants().collect(Collectors.toSet());

      List newInstants = new ArrayList<>();

      // Check If any pending compaction is lost. If so, do not allow incremental timeline sync
      List> compactionInstants = getPendingCompactionTransitions(oldT, newT);
      List lostPendingCompactions =
          compactionInstants.stream().filter(instantPair -> instantPair.getValue() == null).map(Pair::getKey)
              .collect(Collectors.toList());
      if (!lostPendingCompactions.isEmpty()) {
        // If a compaction is unscheduled, fall back to complete refresh of fs view since some log files could have been
        // moved. Its unsafe to incrementally sync in that case.
        log.warn("Some pending compactions are no longer in new timeline (unscheduled ?)."
            + "They are :" + lostPendingCompactions);
        return TimelineDiffResult.UNSAFE_SYNC_RESULT;
      }
      List finishedCompactionInstants = compactionInstants.stream().filter(instantPair ->
          instantPair.getValue().getAction().equals(HoodieTimeline.COMMIT_ACTION)
              && instantPair.getValue().isCompleted()).map(Pair::getKey).collect(Collectors.toList());

      newT.getInstants().filter(instant -> !oldTimelineInstants.contains(instant)).forEach(newInstants::add);
      return new TimelineDiffResult(newInstants, finishedCompactionInstants, true);
    } else {
      // One or more timelines is empty
      log.warn("One or more timelines is empty");
      return TimelineDiffResult.UNSAFE_SYNC_RESULT;
    }
  }

  private static List> getPendingCompactionTransitions(HoodieTimeline oldTimeline,
      HoodieTimeline newTimeline) {
    Set newTimelineInstants = newTimeline.getInstants().collect(Collectors.toSet());

    return oldTimeline.filterPendingCompactionTimeline().getInstants().map(instant -> {
      if (newTimelineInstants.contains(instant)) {
        return Pair.of(instant, instant);
      } else {
        HoodieInstant compacted =
            new HoodieInstant(State.COMPLETED, HoodieTimeline.COMMIT_ACTION, instant.getTimestamp());
        if (newTimelineInstants.contains(compacted)) {
          return Pair.of(instant, compacted);
        }
        return Pair.of(instant, null);
      }
    }).collect(Collectors.toList());
  }

  public static class TimelineDiffResult {

    private final List newlySeenInstants;
    private final List finishedCompactionInstants;
    private final boolean canSyncIncrementally;

    public static final TimelineDiffResult UNSAFE_SYNC_RESULT = new TimelineDiffResult(null, null, false);

    public TimelineDiffResult(List newlySeenInstants, List finishedCompactionInstants,
        boolean canSyncIncrementally) {
      this.newlySeenInstants = newlySeenInstants;
      this.finishedCompactionInstants = finishedCompactionInstants;
      this.canSyncIncrementally = canSyncIncrementally;
    }

    public List getNewlySeenInstants() {
      return newlySeenInstants;
    }

    public List getFinishedCompactionInstants() {
      return finishedCompactionInstants;
    }

    public boolean canSyncIncrementally() {
      return canSyncIncrementally;
    }

    @Override
    public String toString() {
      return "TimelineDiffResult{"
          + "newlySeenInstants=" + newlySeenInstants
          + ", finishedCompactionInstants=" + finishedCompactionInstants
          + ", canSyncIncrementally=" + canSyncIncrementally
          + '}';
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy