You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by bl...@apache.org on 2019/06/27 21:10:07 UTC

[incubator-iceberg] branch master updated: Fix thread safety of date formatting in BaseTableScan (#237)

This is an automated email from the ASF dual-hosted git repository.

blue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-iceberg.git


The following commit(s) were added to refs/heads/master by this push:
     new 5aa9542  Fix thread safety of date formatting in BaseTableScan (#237)
5aa9542 is described below

commit 5aa9542eb87ba74f955e392e3a81a145e105ed55
Author: David Phillips <da...@acz.org>
AuthorDate: Thu Jun 27 14:10:03 2019 -0700

    Fix thread safety of date formatting in BaseTableScan (#237)
    
    SimpleDateFormat is not thread safe.
---
 core/src/main/java/org/apache/iceberg/BaseTableScan.java | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/iceberg/BaseTableScan.java b/core/src/main/java/org/apache/iceberg/BaseTableScan.java
index 50364ee..abbd4c7 100644
--- a/core/src/main/java/org/apache/iceberg/BaseTableScan.java
+++ b/core/src/main/java/org/apache/iceberg/BaseTableScan.java
@@ -27,10 +27,12 @@ import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
-import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Date;
 import java.util.List;
 import java.util.Set;
 import java.util.function.Function;
@@ -56,7 +58,7 @@ import org.slf4j.LoggerFactory;
 class BaseTableScan implements TableScan {
   private static final Logger LOG = LoggerFactory.getLogger(TableScan.class);
 
-  private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+  private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
   private static final List<String> SCAN_COLUMNS = ImmutableList.of(
       "snapshot_id", "file_path", "file_ordinal", "file_format", "block_size_in_bytes",
       "file_size_in_bytes", "record_count", "partition"
@@ -128,7 +130,7 @@ class BaseTableScan implements TableScan {
     // the snapshot ID could be null if no entries were older than the requested time. in that case,
     // there is no valid snapshot to read.
     Preconditions.checkArgument(lastSnapshotId != null,
-        "Cannot find a snapshot older than %s", DATE_FORMAT.format(new Date(timestampMillis)));
+        "Cannot find a snapshot older than %s", formatTimestampMillis(timestampMillis));
 
     return useSnapshot(lastSnapshotId);
   }
@@ -173,7 +175,7 @@ class BaseTableScan implements TableScan {
 
     if (snapshot != null) {
       LOG.info("Scanning table {} snapshot {} created at {} with filter {}", table,
-          snapshot.snapshotId(), DATE_FORMAT.format(new Date(snapshot.timestampMillis())),
+          snapshot.snapshotId(), formatTimestampMillis(snapshot.timestampMillis()),
           rowFilter);
 
       Listeners.notifyAll(
@@ -286,4 +288,8 @@ class BaseTableScan implements TableScan {
 
     return schema;
   }
+
+  private static String formatTimestampMillis(long millis) {
+    return DATE_FORMAT.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault()));
+  }
 }