[HUDI-1794] Moved static COMMIT_FORMATTER to thread local variable as SimpleDateFormat is not thread safe. (#2819)
This commit is contained in:
@@ -35,14 +35,14 @@ import org.apache.log4j.Logger;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -59,8 +59,6 @@ import java.util.function.Function;
|
||||
*/
|
||||
public class HoodieActiveTimeline extends HoodieDefaultTimeline {
|
||||
|
||||
public static final SimpleDateFormat COMMIT_FORMATTER = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
|
||||
public static final Set<String> VALID_EXTENSIONS_IN_ACTIVE_TIMELINE = new HashSet<>(Arrays.asList(
|
||||
COMMIT_EXTENSION, INFLIGHT_COMMIT_EXTENSION, REQUESTED_COMMIT_EXTENSION,
|
||||
DELTA_COMMIT_EXTENSION, INFLIGHT_DELTA_COMMIT_EXTENSION, REQUESTED_DELTA_COMMIT_EXTENSION,
|
||||
@@ -72,28 +70,44 @@ public class HoodieActiveTimeline extends HoodieDefaultTimeline {
|
||||
REQUESTED_REPLACE_COMMIT_EXTENSION, INFLIGHT_REPLACE_COMMIT_EXTENSION, REPLACE_COMMIT_EXTENSION));
|
||||
private static final Logger LOG = LogManager.getLogger(HoodieActiveTimeline.class);
|
||||
protected HoodieTableMetaClient metaClient;
|
||||
private static AtomicReference<String> lastInstantTime = new AtomicReference<>(String.valueOf(Integer.MIN_VALUE));
|
||||
|
||||
/**
|
||||
* Returns next instant time in the {@link #COMMIT_FORMATTER} format.
|
||||
* Ensures each instant time is atleast 1 second apart since we create instant times at second granularity
|
||||
* Parse the timestamp of an Instant and return a {@code SimpleDateFormat}.
|
||||
*/
|
||||
public static String createNewInstantTime() {
|
||||
return createNewInstantTime(0);
|
||||
public static Date parseInstantTime(String timestamp) throws ParseException {
|
||||
return HoodieInstantTimeGenerator.parseInstantTime(timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns next instant time that adds N milliseconds in the {@link #COMMIT_FORMATTER} format.
|
||||
* Format the java.time.Instant to a String representing the timestamp of a Hoodie Instant.
|
||||
*/
|
||||
public static String formatInstantTime(Instant timestamp) {
|
||||
return HoodieInstantTimeGenerator.formatInstantTime(timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the Date to a String representing the timestamp of a Hoodie Instant.
|
||||
*/
|
||||
public static String formatInstantTime(Date timestamp) {
|
||||
return HoodieInstantTimeGenerator.formatInstantTime(timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns next instant time in the correct format.
|
||||
* Ensures each instant time is atleast 1 second apart since we create instant times at second granularity
|
||||
*/
|
||||
public static String createNewInstantTime() {
|
||||
return HoodieInstantTimeGenerator.createNewInstantTime(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns next instant time that adds N milliseconds to current time.
|
||||
* Ensures each instant time is atleast 1 second apart since we create instant times at second granularity
|
||||
*
|
||||
* @param milliseconds Milliseconds to add to current time while generating the new instant time
|
||||
*/
|
||||
public static String createNewInstantTime(long milliseconds) {
|
||||
return lastInstantTime.updateAndGet((oldVal) -> {
|
||||
String newCommitTime;
|
||||
do {
|
||||
newCommitTime = HoodieActiveTimeline.COMMIT_FORMATTER.format(new Date(System.currentTimeMillis() + milliseconds));
|
||||
} while (HoodieTimeline.compareTimestamps(newCommitTime, LESSER_THAN_OR_EQUALS, oldVal));
|
||||
return newCommitTime;
|
||||
});
|
||||
return HoodieInstantTimeGenerator.createNewInstantTime(milliseconds);
|
||||
}
|
||||
|
||||
protected HoodieActiveTimeline(HoodieTableMetaClient metaClient, Set<String> includedExtensions) {
|
||||
@@ -129,6 +143,7 @@ public class HoodieActiveTimeline extends HoodieDefaultTimeline {
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
public HoodieActiveTimeline() {
|
||||
}
|
||||
|
||||
@@ -137,6 +152,7 @@ public class HoodieActiveTimeline extends HoodieDefaultTimeline {
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
in.defaultReadObject();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.common.table.timeline;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* Utility class to generate and parse timestamps used in Instants.
|
||||
*/
|
||||
public class HoodieInstantTimeGenerator {
|
||||
// Format of the timestamp used for an Instant
|
||||
private static final String INSTANT_TIMESTAMP_FORMAT = "yyyyMMddHHmmss";
|
||||
// Formatter to generate Instant timestamps
|
||||
private static DateTimeFormatter INSTANT_TIME_FORMATTER = DateTimeFormatter.ofPattern(INSTANT_TIMESTAMP_FORMAT);
|
||||
// The last Instant timestamp generated
|
||||
private static AtomicReference<String> lastInstantTime = new AtomicReference<>(String.valueOf(Integer.MIN_VALUE));
|
||||
private static final String ALL_ZERO_TIMESTAMP = "00000000000000";
|
||||
|
||||
/**
|
||||
* Returns next instant time that adds N milliseconds to the current time.
|
||||
* Ensures each instant time is atleast 1 second apart since we create instant times at second granularity
|
||||
*
|
||||
* @param milliseconds Milliseconds to add to current time while generating the new instant time
|
||||
*/
|
||||
public static String createNewInstantTime(long milliseconds) {
|
||||
return lastInstantTime.updateAndGet((oldVal) -> {
|
||||
String newCommitTime;
|
||||
do {
|
||||
Date d = new Date(System.currentTimeMillis() + milliseconds);
|
||||
newCommitTime = INSTANT_TIME_FORMATTER.format(convertDateToTemporalAccessor(d));
|
||||
} while (HoodieTimeline.compareTimestamps(newCommitTime, HoodieActiveTimeline.LESSER_THAN_OR_EQUALS, oldVal));
|
||||
return newCommitTime;
|
||||
});
|
||||
}
|
||||
|
||||
public static Date parseInstantTime(String timestamp) {
|
||||
try {
|
||||
LocalDateTime dt = LocalDateTime.parse(timestamp, INSTANT_TIME_FORMATTER);
|
||||
return Date.from(dt.atZone(ZoneId.systemDefault()).toInstant());
|
||||
} catch (DateTimeParseException e) {
|
||||
// Special handling for all zero timestamp which is not parsable by DateTimeFormatter
|
||||
if (timestamp.equals(ALL_ZERO_TIMESTAMP)) {
|
||||
return new Date(0);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static String formatInstantTime(Instant timestamp) {
|
||||
return INSTANT_TIME_FORMATTER.format(timestamp);
|
||||
}
|
||||
|
||||
public static String formatInstantTime(Date timestamp) {
|
||||
return INSTANT_TIME_FORMATTER.format(convertDateToTemporalAccessor(timestamp));
|
||||
}
|
||||
|
||||
private static TemporalAccessor convertDateToTemporalAccessor(Date d) {
|
||||
return d.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public interface HoodieTableMetadata extends Serializable, AutoCloseable {
|
||||
* {@link org.apache.hudi.common.table.timeline.HoodieTimeline#INIT_INSTANT_TS}, such that the metadata table
|
||||
* can be prepped even before bootstrap is done.
|
||||
*/
|
||||
String SOLO_COMMIT_TIMESTAMP = "0000000000000";
|
||||
String SOLO_COMMIT_TIMESTAMP = "00000000000000";
|
||||
// Key for the record which saves list of all partitions
|
||||
String RECORDKEY_PARTITION_LIST = "__all_partitions__";
|
||||
// The partition name used for non-partitioned tables
|
||||
|
||||
Reference in New Issue
Block a user