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 4ee1bcaf5b [core] Use elapsed time for consumer expiration (#11284)
4ee1bcaf5b is described below

commit 4ee1bcaf5bc9ec3d8369aee6dc929f8ea31bef6b
Author: YueZhang <[email protected]>
AuthorDate: Thu Nov 19 23:14:07 2026 -0500

    [core] Use elapsed time for consumer expiration (#11284)
---
 .../apache/paimon/consumer/ConsumerManager.java    |  16 +++-
 .../apache/paimon/table/sink/TableCommitImpl.java  |   4 +-
 .../paimon/table/ConsumerExpirationTimeTest.java   | 103 +++++++++++++++++++++
 3 files changed, 118 insertions(+), 5 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/consumer/ConsumerManager.java 
b/paimon-core/src/main/java/org/apache/paimon/consumer/ConsumerManager.java
index c6ed06f5fe..e21d956c80 100644
--- a/paimon-core/src/main/java/org/apache/paimon/consumer/ConsumerManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/consumer/ConsumerManager.java
@@ -32,6 +32,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.OptionalLong;
+import java.util.function.LongPredicate;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
@@ -93,13 +94,22 @@ public class ConsumerManager implements Serializable {
     }
 
     public void expire(LocalDateTime expireDateTime) {
+        expire(
+                modificationTime ->
+                        
expireDateTime.isAfter(DateTimeUtils.toLocalDateTime(modificationTime)));
+    }
+
+    /** Expires consumers last modified before the given epoch-millisecond 
cutoff. */
+    public void expire(long expireTimeMillis) {
+        expire(modificationTime -> modificationTime < expireTimeMillis);
+    }
+
+    private void expire(LongPredicate expired) {
         try {
             listVersionedFileStatus(fileIO, consumerDirectory(), 
CONSUMER_PREFIX)
                     .forEach(
                             status -> {
-                                LocalDateTime modificationTime =
-                                        
DateTimeUtils.toLocalDateTime(status.getModificationTime());
-                                if (expireDateTime.isAfter(modificationTime)) {
+                                if 
(expired.test(status.getModificationTime())) {
                                     fileIO.deleteQuietly(status.getPath());
                                 }
                             });
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/sink/TableCommitImpl.java 
b/paimon-core/src/main/java/org/apache/paimon/table/sink/TableCommitImpl.java
index ce21a999ce..accf6e184b 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/sink/TableCommitImpl.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/sink/TableCommitImpl.java
@@ -54,7 +54,7 @@ import javax.annotation.Nullable;
 import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.time.Duration;
-import java.time.LocalDateTime;
+import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
@@ -450,7 +450,7 @@ public class TableCommitImpl implements InnerTableCommit {
 
         // expire consumer first to avoid preventing snapshot expiration
         if (doExpire && consumerExpireTime != null) {
-            
consumerManager.expire(LocalDateTime.now().minus(consumerExpireTime));
+            
consumerManager.expire(Instant.now().minus(consumerExpireTime).toEpochMilli());
         }
 
         // snapshot expiration usually take the most time, so put it at the end
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/table/ConsumerExpirationTimeTest.java
 
b/paimon-core/src/test/java/org/apache/paimon/table/ConsumerExpirationTimeTest.java
new file mode 100644
index 0000000000..caa401cf93
--- /dev/null
+++ 
b/paimon-core/src/test/java/org/apache/paimon/table/ConsumerExpirationTimeTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.paimon.table;
+
+import org.apache.paimon.CoreOptions;
+import org.apache.paimon.consumer.Consumer;
+import org.apache.paimon.consumer.ConsumerManager;
+import org.apache.paimon.data.GenericRow;
+import org.apache.paimon.schema.Schema;
+import org.apache.paimon.types.DataTypes;
+
+import org.junit.jupiter.api.parallel.ResourceLock;
+import org.junit.jupiter.api.parallel.Resources;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.mockito.MockedStatic;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.attribute.FileTime;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.temporal.ChronoUnit;
+import java.util.TimeZone;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mockStatic;
+
+/** Tests that consumer lifetime protects snapshots for the configured elapsed 
duration. */
+@ResourceLock(Resources.TIME_ZONE)
+class ConsumerExpirationTimeTest extends TableTestBase {
+
+    @ParameterizedTest
+    @CsvSource({
+        "America/New_York,2026-03-08T07:30:00Z,45,false",
+        "America/New_York,2026-03-08T07:30:00Z,75,true",
+        "America/New_York,2026-11-01T06:30:00Z,45,false",
+        "America/New_York,2026-11-01T06:30:00Z,75,true",
+        "America/New_York,2026-11-02T07:30:00Z,45,false",
+        "America/New_York,2026-11-02T07:30:00Z,75,true",
+        "UTC,2026-03-08T07:30:00Z,60,false",
+        "UTC,2026-03-08T07:30:00Z,61,true"
+    })
+    void testConsumerLifetimeUsesElapsedTime(
+            String zoneName, String nowText, int ageMinutes, boolean expired) 
throws Exception {
+        catalog.createTable(
+                identifier(),
+                Schema.newBuilder()
+                        .column("id", DataTypes.INT())
+                        .option(CoreOptions.CONSUMER_EXPIRATION_TIME.key(), "1 
h")
+                        .option(CoreOptions.SNAPSHOT_NUM_RETAINED_MIN.key(), 
"1")
+                        .option(CoreOptions.SNAPSHOT_NUM_RETAINED_MAX.key(), 
"1")
+                        
.option(CoreOptions.SNAPSHOT_EXPIRE_EXECUTION_MODE.key(), "sync")
+                        .build(),
+                false);
+        FileStoreTable table = (FileStoreTable) catalog.getTable(identifier());
+        write(table, GenericRow.of(1));
+        long protectedSnapshot = table.latestSnapshot().get().id();
+        ConsumerManager consumers = new ConsumerManager(table.fileIO(), 
table.location());
+        consumers.resetConsumer("reader", new Consumer(protectedSnapshot));
+
+        ZoneId zone = ZoneId.of(zoneName);
+        Instant now = Instant.parse(nowText);
+        Instant lastActive = now.minus(ageMinutes, ChronoUnit.MINUTES);
+        Files.setLastModifiedTime(
+                Paths.get(table.location().toUri().getPath(), "consumer", 
"consumer-reader"),
+                FileTime.from(lastActive));
+        LocalDateTime localNow = LocalDateTime.ofInstant(now, zone);
+        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(localNow);
+            instants.when(Instant::now).thenReturn(now);
+            write(table, GenericRow.of(2));
+        } finally {
+            TimeZone.setDefault(originalZone);
+        }
+
+        
assertThat(consumers.consumer("reader").isPresent()).isEqualTo(!expired);
+        
assertThat(table.snapshotManager().snapshotExists(protectedSnapshot)).isEqualTo(!expired);
+        
assertThat(table.latestSnapshot().get().id()).isGreaterThan(protectedSnapshot);
+    }
+}

Reply via email to