TengHuo commented on code in PR #6000:
URL: https://github.com/apache/hudi/pull/6000#discussion_r953286608


##########
hudi-common/src/main/java/org/apache/hudi/common/table/timeline/HoodieActiveTimeline.java:
##########
@@ -75,16 +75,56 @@ public class HoodieActiveTimeline extends 
HoodieDefaultTimeline {
       REQUESTED_REPLACE_COMMIT_EXTENSION, INFLIGHT_REPLACE_COMMIT_EXTENSION, 
REPLACE_COMMIT_EXTENSION,
       REQUESTED_INDEX_COMMIT_EXTENSION, INFLIGHT_INDEX_COMMIT_EXTENSION, 
INDEX_COMMIT_EXTENSION,
       REQUESTED_SAVE_SCHEMA_ACTION_EXTENSION, 
INFLIGHT_SAVE_SCHEMA_ACTION_EXTENSION, SAVE_SCHEMA_ACTION_EXTENSION));
+
+  private static final Set<String> NOT_PARSABLE_TIMESTAMPS = new 
HashSet<String>(3) {{
+      add(HoodieTimeline.INIT_INSTANT_TS);
+      add(HoodieTimeline.METADATA_BOOTSTRAP_INSTANT_TS);
+      add(HoodieTimeline.FULL_BOOTSTRAP_INSTANT_TS);
+    }};
+
   private static final Logger LOG = 
LogManager.getLogger(HoodieActiveTimeline.class);
   protected HoodieTableMetaClient metaClient;
 
   /**
    * Parse the timestamp of an Instant and return a {@code Date}.
+   * Throw ParseException if timestamp not valid format as
+   *  {@link 
org.apache.hudi.common.table.timeline.HoodieInstantTimeGenerator#SECS_INSTANT_TIMESTAMP_FORMAT}.
+   *
+   * @param timestamp a timestamp String which follow pattern as
+   *  {@link 
org.apache.hudi.common.table.timeline.HoodieInstantTimeGenerator#SECS_INSTANT_TIMESTAMP_FORMAT}.
+   * @return Date of instant timestamp
    */
   public static Date parseDateFromInstantTime(String timestamp) throws 
ParseException {
     return HoodieInstantTimeGenerator.parseDateFromInstantTime(timestamp);
   }
 
+  /**
+   * The same format method as above, but this method will mute ParseException
+   * if the gaven timestamp is invalid and return Date(0), or a corresponding 
Date if these timestamp provided
+   *  {@link 
org.apache.hudi.common.table.timeline.HoodieTimeline#INIT_INSTANT_TS},
+   *  {@link 
org.apache.hudi.common.table.timeline.HoodieTimeline#METADATA_BOOTSTRAP_INSTANT_TS},
+   *  {@link 
org.apache.hudi.common.table.timeline.HoodieTimeline#FULL_BOOTSTRAP_INSTANT_TS}.
+   * This method is useful when parse timestamp for metrics
+   *
+   * @param timestamp a timestamp String which follow pattern as
+   *  {@link 
org.apache.hudi.common.table.timeline.HoodieInstantTimeGenerator#SECS_INSTANT_TIMESTAMP_FORMAT}.
+   * @return Date of instant timestamp
+   */
+  public static Date parseDateFromInstantTimeSafely(String timestamp) {
+    Date parsedDate;
+    try {
+      parsedDate = 
HoodieInstantTimeGenerator.parseDateFromInstantTime(timestamp);
+    } catch (ParseException e) {
+      LOG.warn("Failed to parse timestamp " + timestamp + " because of " + 
e.getMessage());
+      if (NOT_PARSABLE_TIMESTAMPS.contains(timestamp)) {
+        parsedDate = new Date(Integer.parseInt(timestamp));
+      } else {
+        parsedDate = new Date(0);

Review Comment:
   It's the old logic in `HoodieInstantTimeGenerator.parseDateFromInstantTime`, 
if it catch the error and the timestamp is all zero, it will return `Date(0)`, 
so I keep it.
   
   ```java
         // Special handling for all zero timestamp which is not parsable by 
DateTimeFormatter
         if (timestamp.equals(ALL_ZERO_TIMESTAMP)) {
           return new Date(0);
         }
         throw e;
   ```
   
   but I agree with you, it will return a dirty value, which is bad for the 
code where it uses this method. `parseDateFromInstantTimeSafely` should return 
an optional value, then the code who use this method can decide how to deal 
with Option.empty.



-- 
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