jonvex commented on code in PR #8043:
URL: https://github.com/apache/hudi/pull/8043#discussion_r1118895471


##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/deltastreamer/HoodieDeltaStreamerMultiwriterCheckpoint.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.utilities.deltastreamer;
+
+import org.apache.hudi.common.model.HoodieCommitMetadata;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.StringUtils;
+import org.apache.hudi.exception.HoodieException;
+import org.apache.hudi.exception.HoodieIOException;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.BiConsumer;
+
+import static 
org.apache.hudi.utilities.deltastreamer.HoodieDeltaStreamer.CHECKPOINT_KEY;
+
+/**
+ * This is used as an extraPreCommitFunc in BaseHoodieWriteClient
+ * It adds the checkpoint to deltacommit metadata. It must be implemented this 
way
+ * because it needs the lock to ensure that it does not overwrite another 
deltastreamers
+ * latest checkpoint with an older one.
+ */
+public class HoodieDeltaStreamerMultiwriterCheckpoint implements 
BiConsumer<HoodieTableMetaClient, HoodieCommitMetadata> {
+  private static final ObjectMapper OM = new ObjectMapper();
+
+  //deltastreamer id
+  private final String id;
+
+  //the deltastreamer
+  private final DeltaSync ds;
+  private final String checkpoint;
+  private final String latestCheckpointWritten;
+  
+  public HoodieDeltaStreamerMultiwriterCheckpoint(DeltaSync ds, String 
checkpoint, String latestCheckpointWritten) {
+    this.ds = ds;
+    this.id = ds.getId();
+    this.checkpoint = checkpoint;
+    this.latestCheckpointWritten = latestCheckpointWritten;
+  }
+
+  @Override
+  public void accept(HoodieTableMetaClient metaClient, HoodieCommitMetadata 
commitMetadata) {
+    //Get last completed deltacommit
+    Option<HoodieCommitMetadata> latestCommitMetadata;
+    try {
+      ds.refreshTimeline();
+      latestCommitMetadata = 
ds.getLatestCommitMetadataWithValidCheckpointInfo(metaClient.getActiveTimeline().getCommitsTimeline());
+    } catch (IOException e) {
+      throw new HoodieIOException("Failed to get the latest commit metadata", 
e);
+    }
+
+    //Get checkpoint map
+    Map<String,String> checkpointMap;
+    if (latestCommitMetadata.isPresent()) {
+      String value = commitMetadata.getMetadata(CHECKPOINT_KEY);
+      try {
+        checkpointMap = OM.readValue(value, Map.class);
+      } catch (IOException e) {
+        throw new HoodieIOException("Failed to parse checkpoint as map", e);
+      }
+    } else {
+      checkpointMap = new HashMap<>();
+    }
+
+    if (!StringUtils.isNullOrEmpty(latestCheckpointWritten)
+        && checkpointMap.containsKey(id) && 
!checkpointMap.get(id).equals(latestCheckpointWritten)) {
+      throw new HoodieException(String.format("Multiple DeltaStreamer 
instances with id: %s detected. Each Deltastreamer must have a unique id.", 
id));

Review Comment:
   If the checkpoint in the latest commit does not match the last checkpoint 
this deltastreamer wrote, it means that another deltstreamer has the same id



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to