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

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 6ba6e6725c [core] Use elapsed time for historical clustering idle 
checks (#11232)
6ba6e6725c is described below

commit 6ba6e6725cd60401de8f42304e255583a22fc572
Author: YueZhang <[email protected]>
AuthorDate: Thu Nov 19 02:42:59 2026 -0500

    [core] Use elapsed time for historical clustering idle checks (#11232)
---
 .../append/cluster/HistoryPartitionCluster.java    | 27 +++++--
 .../cluster/HistoryPartitionClusterTest.java       | 83 +++++++++++++++++++++-
 2 files changed, 102 insertions(+), 8 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/append/cluster/HistoryPartitionCluster.java
 
b/paimon-core/src/main/java/org/apache/paimon/append/cluster/HistoryPartitionCluster.java
index 4850d2bed3..0398776a6f 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/append/cluster/HistoryPartitionCluster.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/append/cluster/HistoryPartitionCluster.java
@@ -36,6 +36,7 @@ import org.slf4j.LoggerFactory;
 import javax.annotation.Nullable;
 
 import java.time.Duration;
+import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.util.ArrayList;
@@ -129,18 +130,16 @@ public class HistoryPartitionCluster {
 
     @VisibleForTesting
     public Map<BinaryRow, List<LevelSortedRun>> 
constructLevelsForHistoryPartitions() {
-        long historyMilli =
-                LocalDateTime.now()
-                        .minus(historyPartitionIdleTime)
-                        .atZone(ZoneId.systemDefault())
-                        .toInstant()
-                        .toEpochMilli();
+        long historyMilli = 
Instant.now().minus(historyPartitionIdleTime).toEpochMilli();
 
         List<BinaryRow> historyPartitions =
                 table.newSnapshotReader().withSnapshot(snapshot)
                         .withLevelMinMaxFilter((min, max) -> min < maxLevel)
                         .withLevelFilter(level -> level < 
maxLevel).partitionEntries().stream()
-                        .filter(entry -> entry.lastFileCreationTime() < 
historyMilli)
+                        .filter(
+                                entry ->
+                                        
latestCreationMillis(entry.lastFileCreationTime())
+                                                < historyMilli)
                         
.sorted(Comparator.comparingLong(PartitionEntry::lastFileCreationTime))
                         .map(PartitionEntry::partition)
                         .collect(Collectors.toList());
@@ -171,6 +170,20 @@ public class HistoryPartitionCluster {
                                 entry -> 
constructPartitionLevels(entry.getValue())));
     }
 
+    /**
+     * File creation times are persisted as local date-times, and {@link
+     * DataFileMeta#creationTimeEpochMillis()} resolves a repeated DST hour to 
its earlier
+     * occurrence. Re-resolve to the later one so a partition is never treated 
as idle early.
+     */
+    private static long latestCreationMillis(long creationMillis) {
+        ZoneId zone = ZoneId.systemDefault();
+        return LocalDateTime.ofInstant(Instant.ofEpochMilli(creationMillis), 
zone)
+                .atZone(zone)
+                .withLaterOffsetAtOverlap()
+                .toInstant()
+                .toEpochMilli();
+    }
+
     private Map<BinaryRow, List<DataFileMeta>> filterPartitions(
             Map<BinaryRow, List<DataFileMeta>> partitionFiles) {
         Map<BinaryRow, List<DataFileMeta>> result = new HashMap<>();
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/append/cluster/HistoryPartitionClusterTest.java
 
b/paimon-core/src/test/java/org/apache/paimon/append/cluster/HistoryPartitionClusterTest.java
index c23c949dbd..481d7ca001 100644
--- 
a/paimon-core/src/test/java/org/apache/paimon/append/cluster/HistoryPartitionClusterTest.java
+++ 
b/paimon-core/src/test/java/org/apache/paimon/append/cluster/HistoryPartitionClusterTest.java
@@ -37,20 +37,101 @@ import org.apache.paimon.types.RowType;
 import org.assertj.core.util.Lists;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
-
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.mockito.MockedStatic;
+
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.temporal.ChronoUnit;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.TimeZone;
 
 import static 
org.apache.paimon.append.cluster.IncrementalClusterManagerTest.writeOnce;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mockStatic;
 
 /** Test for {@link HistoryPartitionCluster}. */
 public class HistoryPartitionClusterTest {
 
     @TempDir java.nio.file.Path tempDir;
 
+    @ParameterizedTest
+    @CsvSource({
+        "America/New_York,2026-03-08T07:30:00Z,90,false",
+        "America/New_York,2026-11-01T07:30:00Z,165,true",
+        "America/New_York,2026-11-01T07:45:00Z,90,false",
+        "America/New_York,2026-11-02T07:30:00Z,90,false",
+        "America/New_York,2026-11-02T07:30:00Z,165,true",
+        "UTC,2026-03-08T07:30:00Z,90,false",
+        "UTC,2026-03-08T07:30:00Z,165,true",
+        "UTC,2026-03-08T07:30:00Z,120,false",
+        "UTC,2026-03-08T07:30:00Z,121,true"
+    })
+    void testHistoryPartitionIdleTimeUsesElapsedDuration(
+            String zoneName, String nowText, int ageMinutes, boolean eligible) 
throws Exception {
+        ZoneId zone = ZoneId.of(zoneName);
+        Instant now = Instant.parse(nowText);
+        Instant created = now.minus(ageMinutes, ChronoUnit.MINUTES);
+        LocalDateTime creationTime = LocalDateTime.ofInstant(created, zone);
+        LocalDateTime currentTime = LocalDateTime.ofInstant(now, zone);
+        Map<String, String> options = new HashMap<>();
+        options.put(CoreOptions.CLUSTERING_HISTORY_PARTITION_IDLE_TIME.key(), 
"2h");
+        options.put(CoreOptions.ASYNC_FILE_WRITE.key(), "false");
+        FileStoreTable table = createTable(options, 
Collections.singletonList("f2"));
+        TimeZone originalZone = TimeZone.getDefault();
+        TimeZone.setDefault(TimeZone.getTimeZone(zone));
+        try (MockedStatic<LocalDateTime> localTimes =
+                        mockStatic(LocalDateTime.class, CALLS_REAL_METHODS);
+                MockedStatic<Instant> instants = mockStatic(Instant.class, 
CALLS_REAL_METHODS)) {
+            localTimes.when(LocalDateTime::now).thenReturn(creationTime);
+            instants.when(Instant::now).thenReturn(created);
+            writeOnce(
+                    table,
+                    GenericRow.of(
+                            1,
+                            1,
+                            BinaryString.fromString("requested"),
+                            BinaryString.fromString("v")),
+                    GenericRow.of(
+                            1,
+                            1,
+                            BinaryString.fromString("history"),
+                            BinaryString.fromString("v")));
+            // The persisted local time is ambiguous when created in a 
repeated DST hour.
+            assertThat(table.store().newScan().plan().files())
+                    .hasSize(2)
+                    .allSatisfy(
+                            entry ->
+                                    
assertThat(entry.file().creationTime().toLocalDateTime())
+                                            .isEqualTo(creationTime));
+
+            localTimes.when(LocalDateTime::now).thenReturn(currentTime);
+            instants.when(Instant::now).thenReturn(now);
+            BinaryRow requested = BinaryRow.singleColumn("requested");
+            BinaryRow history = BinaryRow.singleColumn("history");
+            IncrementalClusterManager manager =
+                    new IncrementalClusterManager(
+                            table,
+                            PartitionPredicate.fromMultiple(
+                                    RowType.of(DataTypes.STRING()),
+                                    Collections.singletonList(requested)));
+            if (eligible) {
+                assertThat(manager.createCompactUnits(true).keySet())
+                        .containsExactlyInAnyOrder(requested, history);
+            } else {
+                
assertThat(manager.createCompactUnits(true).keySet()).containsExactly(requested);
+            }
+        } finally {
+            TimeZone.setDefault(originalZone);
+        }
+    }
+
     @Test
     public void testHistoryPartitionAutoClustering() throws Exception {
         FileStoreTable table = createTable(Collections.emptyMap(), 
Collections.singletonList("f2"));

Reply via email to