You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by "suryaprasanna (via GitHub)" <gi...@apache.org> on 2023/07/20 06:01:43 UTC
[GitHub] [hudi] suryaprasanna commented on a diff in pull request #8832: [HUDI-6288] Create IngestionPrimaryWriterBasedConflictResolutionStrategy to prioritize ingestion writers over other writers
suryaprasanna commented on code in PR #8832:
URL: https://github.com/apache/hudi/pull/8832#discussion_r1268969829
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/IngestionPrimaryWriterBasedConflictResolutionStrategy.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.hudi.client.transaction;
+
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.timeline.HoodieActiveTimeline;
+import org.apache.hudi.common.table.timeline.HoodieInstant;
+import org.apache.hudi.common.util.ClusteringUtils;
+import org.apache.hudi.common.util.CollectionUtils;
+import org.apache.hudi.common.util.Option;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.apache.hudi.common.table.timeline.HoodieTimeline.COMMIT_ACTION;
+import static org.apache.hudi.common.table.timeline.HoodieTimeline.COMPACTION_ACTION;
+import static org.apache.hudi.common.table.timeline.HoodieTimeline.DELTA_COMMIT_ACTION;
+import static org.apache.hudi.common.table.timeline.HoodieTimeline.REPLACE_COMMIT_ACTION;
+
+/**
+ * This class extends the base implementation of conflict resolution strategy.
+ * It gives preference to ingestion writers compared to table services.
+ */
+public class IngestionPrimaryWriterBasedConflictResolutionStrategy
+ extends SimpleConcurrentFileWritesConflictResolutionStrategy {
+
+ private static final Logger LOG = LoggerFactory.getLogger(IngestionPrimaryWriterBasedConflictResolutionStrategy.class);
+
+ /**
+ * For tableservices like replacecommit and compaction commits this method also returns ingestion inflight commits.
+ */
+ @Override
+ public Stream<HoodieInstant> getCandidateInstants(HoodieTableMetaClient metaClient, HoodieInstant currentInstant,
+ Option<HoodieInstant> lastSuccessfulInstant) {
+ HoodieActiveTimeline activeTimeline = metaClient.reloadActiveTimeline();
+ if ((REPLACE_COMMIT_ACTION.equals(currentInstant.getAction())
+ && ClusteringUtils.isClusteringCommit(metaClient, currentInstant))
+ || COMPACTION_ACTION.equals(currentInstant.getAction())) {
+ return getCandidateInstantsForTableServicesCommits(activeTimeline, currentInstant);
+ } else {
+ return getCandidateInstantsForNonTableServicesCommits(activeTimeline, currentInstant);
+ }
+ }
+
+ private Stream<HoodieInstant> getCandidateInstantsForNonTableServicesCommits(HoodieActiveTimeline activeTimeline, HoodieInstant currentInstant) {
+
+ // To findout which instants are conflicting, we apply the following logic
+ // Get all the completed instants timeline only for commits that have happened
+ // since the last successful write based on the transition times.
+ // We need to check for write conflicts since they may have mutated the same files
+ // that are being newly created by the current write.
+ List<HoodieInstant> completedCommitsInstants = activeTimeline
+ .getTimelineOfActions(CollectionUtils.createSet(COMMIT_ACTION, REPLACE_COMMIT_ACTION, COMPACTION_ACTION, DELTA_COMMIT_ACTION))
+ .filterCompletedInstants()
+ .findInstantsModifiedAfterByStateTransitionTime(currentInstant.getTimestamp())
+ .getInstantsOrderedByStateTransitionTime()
+ .collect(Collectors.toList());
+ LOG.info(String.format("Instants that may have conflict with %s are %s", currentInstant, completedCommitsInstants));
+ return completedCommitsInstants.stream();
+ }
+
+ /**
+ * To find which instants are conflicting, we apply the following logic
+ * Get both completed instants and ingestion inflight commits that have happened since the last successful write.
+ * We need to check for write conflicts since they may have mutated the same files
+ * that are being newly created by the current write.
+ */
+ private Stream<HoodieInstant> getCandidateInstantsForTableServicesCommits(HoodieActiveTimeline activeTimeline, HoodieInstant currentInstant) {
+ // Fetch list of completed commits.
+ Stream<HoodieInstant> completedCommitsStream =
+ activeTimeline
+ .getTimelineOfActions(CollectionUtils.createSet(COMMIT_ACTION, REPLACE_COMMIT_ACTION, COMPACTION_ACTION, DELTA_COMMIT_ACTION))
+ .filterCompletedInstants()
+ .findInstantsModifiedAfterByStateTransitionTime(currentInstant.getTimestamp())
+ .getInstantsAsStream();
+
+ // Fetch list of ingestion inflight commits.
+ Stream<HoodieInstant> inflightIngestionCommitsStream =
Review Comment:
@Zouxxyy I idea behind creating IngestionPrimaryWriterBasedConflictResolutionStrategy, is to give preference for Ingestion writers over clustering writers.
For example, you have clustering plans as mutable plans which can be created and removed by rollback, this is achievable by using SparkAllowUpdateStrategy. Clustering writers and Ingestion writers can run simultaneously on the same partition. If clustering writer were to complete writing first and about to create a completed replacecommit, in that stage it will check if there are any pending inflight ingestion instants in the timeline and fail if it finds an overlap.
For bulk insert operations, they create new files so there wont be ay conflict with clustering writers, so in that case replacecommit is allowed to commit. There could be a case where if users are using insert API, but it can still write to existing files, if small file handling is enabled. Even then possibility of overlap between clustering and ingestion can be reduced and even if there is overlap you can fail clustering commit and allow Ingestion to commit even if it is arriving late.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org