[
https://issues.apache.org/jira/browse/FLINK-41640?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
ASF GitHub Bot updated FLINK-41640:
-----------------------------------
Labels: pull-request-available (was: )
> Synchronous MapState.isEmpty() reports non-empty when all entries have
> expired under NeverReturnExpired
> -------------------------------------------------------------------------------------------------------
>
> Key: FLINK-41640
> URL: https://issues.apache.org/jira/browse/FLINK-41640
> Project: Flink
> Issue Type: Bug
> Components: Runtime / State Backends
> Affects Versions: 1.20.3
> Reporter: YifanWu
> Priority: Major
> Labels: pull-request-available
>
> With {{NeverReturnExpired}}, synchronous {{MapState.isEmpty()}} reports that
> a keyed map still contains entries after its only mapping has expired. The
> result changes if the application reads the expired key first. This also
> affects {{MapState.is_empty()}} in PyFlink DataStream thread mode.
> h3. Steps to reproduce
> On Apache Flink 1.20.3, add the following JUnit class alongside
> {{HeapAsyncSnapshotTtlStateTest}} in {{flink-runtime}} and run
> {{SyncMapStateTtlTest#isEmptyIgnoresExpiredMapping}}. The test uses the real
> heap keyed-state backend and its controllable TTL clock. Disabling background
> cleanup keeps the expired entry in storage until it is read.
> {code:java}
> package org.apache.flink.runtime.state.ttl;
> import org.apache.flink.api.common.state.MapState;
> import org.apache.flink.api.common.state.MapStateDescriptor;
> import org.apache.flink.api.common.state.StateTtlConfig;
> import org.apache.flink.api.common.typeutils.base.StringSerializer;
> import org.junit.jupiter.api.Test;
> import java.time.Duration;
> import static org.assertj.core.api.Assertions.assertThat;
> class SyncMapStateTtlTest extends HeapAsyncSnapshotTtlStateTest {
> @Test
> void isEmptyIgnoresExpiredMapping() throws Exception {
> sbetc.createAndRestoreKeyedStateBackend(null);
> sbetc.setCurrentKey("partition");
> MapStateDescriptor<String, String> descriptor =
> new MapStateDescriptor<>("map", StringSerializer.INSTANCE,
> StringSerializer.INSTANCE);
> descriptor.enableTimeToLive(
> StateTtlConfig.newBuilder(Duration.ofSeconds(2))
>
> .setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
> .disableCleanupInBackground()
> .build());
> MapState<String, String> state = sbetc.createState(descriptor, "ns");
> timeProvider.setCurrentTimestamp(0);
> state.put("k", "value");
> timeProvider.setCurrentTimestamp(4000);
> boolean emptyBeforeGet = state.isEmpty();
> String value = state.get("k");
> boolean emptyAfterGet = state.isEmpty();
> assertThat(value).isNull();
> assertThat(emptyAfterGet).isTrue();
> assertThat(emptyBeforeGet).as("first isEmpty() after
> expiry").isTrue();
> }
> }
> {code}
> *Expected* (first {{isEmpty()}}, {{get("k")}}, second {{isEmpty()}}): {{true,
> null, true}}. Under
> [NeverReturnExpired|https://nightlies.apache.org/flink/flink-docs-release-1.20/docs/dev/datastream/fault-tolerance/state/#state-time-to-live-ttl],
> an expired mapping should be invisible to the application even while
> physical cleanup is pending.
> *Observed* on Apache Flink 1.20.3: {{false, null, true}}. Only the assertion
> on {{emptyBeforeGet}} fails. The corresponding PyFlink thread-mode sequence
> is {{state.is_empty(), state.get("k"), state.is_empty()}}, yielding {{(False,
> None, True)}}.
> The synchronous
> [TtlMapState.isEmpty()|https://github.com/apache/flink/blob/release-1.20.3/flink-runtime/src/main/java/org/apache/flink/runtime/state/ttl/TtlMapState.java#L130]
> delegates to the underlying map. Reading the expired key checks its TTL and
> removes the entry, which explains why the second empty check succeeds.
> FLINK-34625 covers the PyFlink process-mode state cache and its TTL-aware
> empty check. FLINK-41417 covers ForSt asynchronous state iterators. The test
> above exercises the synchronous state API directly.
> _This report was prepared with AI assistance. The Java test was run against
> the Apache Flink 1.20.3 release artifacts._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)