danny0405 commented on code in PR #9209:
URL: https://github.com/apache/hudi/pull/9209#discussion_r1288140061


##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/utils/ArchivedTimelineWriter.java:
##########
@@ -0,0 +1,382 @@
+/*
+ * 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.utils;
+
+import org.apache.hudi.avro.model.HoodieArchivedInstant;
+import org.apache.hudi.common.engine.HoodieEngineContext;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.model.HoodieArchivedManifest;
+import org.apache.hudi.common.model.HoodieAvroIndexedRecord;
+import org.apache.hudi.common.model.HoodieFileFormat;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.timeline.HoodieArchivedTimeline;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.VisibleForTesting;
+import org.apache.hudi.common.util.collection.ClosableIterator;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.exception.HoodieCommitException;
+import org.apache.hudi.exception.HoodieException;
+import org.apache.hudi.exception.HoodieIOException;
+import org.apache.hudi.io.storage.HoodieAvroParquetReader;
+import org.apache.hudi.io.storage.HoodieFileReaderFactory;
+import org.apache.hudi.io.storage.HoodieFileWriter;
+import org.apache.hudi.io.storage.HoodieFileWriterFactory;
+import org.apache.hudi.table.HoodieTable;
+import org.apache.hudi.table.marker.WriteMarkers;
+import org.apache.hudi.table.marker.WriteMarkersFactory;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.IndexedRecord;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * An archived timeline writer which organizes the files as an LSM tree.
+ */
+public class ArchivedTimelineWriter {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ArchivedTimelineWriter.class);
+
+  private final HoodieWriteConfig config;
+  private final HoodieTable<?, ?, ?, ?> table;
+  private final HoodieTableMetaClient metaClient;
+
+  private HoodieWriteConfig writeConfig;
+
+  private ArchivedTimelineWriter(HoodieWriteConfig config, HoodieTable<?, ?, 
?, ?> table) {
+    this.config = config;
+    this.table = table;
+    this.metaClient = table.getMetaClient();
+  }
+
+  public static ArchivedTimelineWriter getInstance(HoodieWriteConfig config, 
HoodieTable<?, ?, ?, ?> table) {
+    return new ArchivedTimelineWriter(config, table);
+  }
+
+  public void write(HoodieEngineContext context, List<ActiveInstant> instants) 
throws HoodieCommitException {
+    Path filePath = new Path(metaClient.getArchivePath(),
+        newFileName(instants.get(0).getInstantTime(), 
instants.get(instants.size() - 1).getInstantTime(), 
HoodieArchivedTimeline.FILE_LAYER_ZERO));
+    try (HoodieFileWriter writer = openWriter(filePath)) {
+      Schema wrapperSchema = HoodieArchivedInstant.getClassSchema();
+      LOG.info("Archiving schema " + wrapperSchema.toString());
+      for (ActiveInstant triple : instants) {
+        try {
+          deleteAnyLeftOverMarkers(context, triple);
+          // in local FS and HDFS, there could be empty completed instants due 
to crash.
+          final HoodieArchivedInstant metaEntry = 
MetadataConversionUtils.createArchivedInstant(triple, metaClient);
+          writer.write(metaEntry.getInstantTime(), new 
HoodieAvroIndexedRecord(metaEntry), wrapperSchema);
+        } catch (Exception e) {
+          LOG.error("Failed to archive instant: " + triple.getInstantTime(), 
e);
+          if (this.config.isFailOnTimelineArchivingEnabled()) {
+            throw e;
+          }
+        }
+      }
+      updateManifest(filePath.getName());
+    } catch (Exception e) {
+      throw new HoodieCommitException("Failed to archive commits", e);
+    }
+  }
+
+  public void updateManifest(String fileToAdd) throws IOException {
+    // 1. read the latest manifest version file;
+    // 2. read the latest manifest file for valid files;
+    // 3. add this new file to the existing file list from step2.
+    int latestVersion = 
HoodieArchivedTimeline.latestSnapshotVersion(metaClient);
+    HoodieArchivedManifest latestManifest = 
HoodieArchivedTimeline.latestSnapshotManifest(metaClient, latestVersion);
+    HoodieArchivedManifest newManifest = latestManifest.copy();
+    newManifest.addFile(getFileEntry(fileToAdd));
+    createManifestFile(newManifest, latestVersion);
+  }
+
+  public void updateManifest(List<String> filesToRemove, String fileToAdd) 
throws IOException {

Review Comment:
   We can add when necessary, it is quite easy to extend.



-- 
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-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to