the-other-tim-brown commented on code in PR #13295: URL: https://github.com/apache/hudi/pull/13295#discussion_r2112548886
########## hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/client/SparkRDDMetadataWriteClient.java: ########## @@ -0,0 +1,98 @@ +/* + * 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; + +import org.apache.hudi.client.embedded.EmbeddedTimelineService; +import org.apache.hudi.common.data.HoodieData; +import org.apache.hudi.common.engine.HoodieEngineContext; +import org.apache.hudi.common.model.HoodieCommitMetadata; +import org.apache.hudi.common.model.HoodieKey; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.model.WriteOperationType; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.data.HoodieJavaRDD; +import org.apache.hudi.table.HoodieSparkMergeOnReadMetadataTable; +import org.apache.hudi.table.HoodieTable; +import org.apache.hudi.table.action.HoodieWriteMetadata; + +import org.apache.spark.api.java.JavaRDD; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.BiConsumer; + +/** + * Write client to assist with writing to metadata table. + * @param <T> + */ +public class SparkRDDMetadataWriteClient<T> extends SparkRDDWriteClient<T> { + + // tracks the instants for which preWrite has been invoked. + private final Set<String> preWriteCompletedInstants = new HashSet<>(); + + public SparkRDDMetadataWriteClient(HoodieEngineContext context, HoodieWriteConfig clientConfig) { + super(context, clientConfig); + } + + public SparkRDDMetadataWriteClient(HoodieEngineContext context, HoodieWriteConfig writeConfig, + Option<EmbeddedTimelineService> timelineService) { + super(context, writeConfig, timelineService); + } + + /** + * Upserts the given prepared records into the Hoodie table, at the supplied instantTime. + * <p> + * This implementation requires that the input records are already tagged, and de-duped if needed. + * + * @param preppedRecords Prepared HoodieRecords to upsert + * @param instantTime Instant time of the commit + * @return Collection of WriteStatus to inspect errors and counts + */ + public JavaRDD<WriteStatus> upsertPreppedRecords(JavaRDD<HoodieRecord<T>> preppedRecords, String instantTime, Option<List<Pair<String, String>>> partitionFileIdPairsOpt) { + HoodieTable<T, HoodieData<HoodieRecord<T>>, HoodieData<HoodieKey>, HoodieData<WriteStatus>> table = + initTable(WriteOperationType.UPSERT_PREPPED, Option.ofNullable(instantTime)); + table.validateUpsertSchema(); + boolean initialCall = preWriteCompletedInstants.contains(instantTime); + if (!initialCall) { Review Comment: I think the naming here is off, it seems like we want to perform `preWrite` on the initial invocation, right? ########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/metadata/HoodieBackedTableMetadataWriter.java: ########## @@ -1463,10 +1468,11 @@ protected abstract void bulkCommit( * Tag each record with the location in the given partition. * The record is tagged with respective file slice's location based on its record key. */ - protected HoodieData<HoodieRecord> prepRecords(Map<String, HoodieData<HoodieRecord>> partitionRecordsMap) { + protected Pair<HoodieData<HoodieRecord>, List<Pair<String, String>>> prepRecords(Map<String, HoodieData<HoodieRecord>> partitionRecordsMap) { Review Comment: Can you update the javadocs so it is clear what is being returned? It may also be worthwhile to introduce a POJO so it is more clear what the `Pair<String, String>` represents ########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/metadata/HoodieBackedTableMetadataWriter.java: ########## @@ -1481,18 +1487,21 @@ protected HoodieData<HoodieRecord> prepRecords(Map<String, HoodieData<HoodieReco ValidationUtils.checkArgument(fileGroupCount > 0, String.format("FileGroup count for MDT partition %s should be > 0", partitionName)); List<FileSlice> finalFileSlices = fileSlices; + Set<String> mappedFileIds = new HashSet<>(); HoodieData<HoodieRecord> rddSinglePartitionRecords = records.map(r -> { FileSlice slice = finalFileSlices.get(HoodieTableMetadataUtil.mapRecordKeyToFileGroupIndex(r.getRecordKey(), fileGroupCount)); r.unseal(); r.setCurrentLocation(new HoodieRecordLocation(slice.getBaseInstantTime(), slice.getFileId())); r.seal(); + mappedFileIds.add(slice.getFileId()); Review Comment: The `mappedFileIds` set will be serialized to the executor in the spark context. It will not be reported back to the driver so I don't think this implementation will work. ########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/BaseHoodieWriteClient.java: ########## @@ -142,6 +143,7 @@ public abstract class BaseHoodieWriteClient<T, I, K, O> extends BaseHoodieClient protected Set<String> pendingInflightAndRequestedInstants = Collections.emptySet(); protected BaseHoodieTableServiceClient<?, ?, O> tableServiceClient; + protected boolean isMetadataTable; Review Comment: This looks like it is unused now, can it be removed? -- 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]
