You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "mayuehappy (via GitHub)" <gi...@apache.org> on 2025/04/11 09:00:58 UTC

[PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

mayuehappy opened a new pull request, #26440:
URL: https://github.com/apache/flink/pull/26440

   
   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend
   
   *(For example: This pull request makes task deployment go through the blob server, rather than through RPC. That way we avoid re-transferring them on each deployment (during recovery).)*
   
   This PR is mainly designed to facilitate compatibility verification after the upcoming feature of switching between ForStSyncKeyeStateBackend and ForSyAsynchronous KeyeStateBackend.
   Due to the current inability to switch between ForStSyncKeydStateBackend and ForSyAsynchronous KeydStateBackend, an Exception will be caught in UT first, and once it is supported later, the correctness of the State data will continue to be verified.
   
   ## Verifying this change
   
   
   This change is a trivial rework 
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes / no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #26440:
URL: https://github.com/apache/flink/pull/26440#discussion_r2039177627


##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */

Review Comment:
   ```suggestion
   /** Compatibility test for {@link ForStKeyedStateBackend} and {@link ForStSyncKeyedStateBackend}. */
   ```



##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;

Review Comment:
   ```suggestion
   import static org.junit.jupiter.api.Assertions.fail;
   ```



##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =
+                setUpAsyncKeyedStateBackend(Collections.emptyList());
+        MapStateDescriptor<Integer, String> descriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+
+        MapState<Integer, String> asyncMapState =
+                keyedBackend.createState(1, IntSerializer.INSTANCE, descriptor);
+
+        context = aec.buildContext("testRecord", "testKey");
+        context.retain();
+        aec.setCurrentContext(context);
+        asyncMapState.asyncPut(1, "1");
+        context.release();
+        aec.drainInflightRecords(0);
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+        ForStSyncKeyedStateBackend<String> syncKeyedStateBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend,
+                        env,
+                        StringSerializer.INSTANCE,
+                        Collections.singletonList(stateHandle));
+
+        try {
+            org.apache.flink.api.common.state.MapState<Integer, String> syncMapState =
+                    syncKeyedStateBackend.getOrCreateKeyedState(
+                            IntSerializer.INSTANCE,
+                            StateDescriptorUtils.transformFromV2ToV1(descriptor));
+            fail();
+
+            syncKeyedStateBackend.setCurrentKey("testKey");
+            ((InternalKvState) syncKeyedStateBackend).setCurrentNamespace(1);
+            Assert.assertEquals(syncMapState.get(1), "1");
+        } catch (Exception e) {
+            // Currently, ForStStateBackend does not support switching from Async to Sync, so this
+            // exception will be caught here
+            assertEquals(
+                    e.getMessage()
+                            .contains(
+                                    "org.apache.flink.runtime.state.v2.RegisteredKeyAndUserKeyValueStateBackendMetaInfo cannot be cast to class org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo"),
+                    true);
+        } finally {
+            IOUtils.closeQuietly(syncKeyedStateBackend);
+        }
+    }
+
+    @Test
+    void testForStTransFromSyncToAsync() throws Exception {
+        ForStSyncKeyedStateBackend<String> keyedBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend, env, StringSerializer.INSTANCE, Collections.emptyList());
+        org.apache.flink.api.common.state.MapStateDescriptor<Integer, String> descriptor =
+                new org.apache.flink.api.common.state.MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+        org.apache.flink.api.common.state.MapState<Integer, String> mapState =
+                keyedBackend.getOrCreateKeyedState(IntSerializer.INSTANCE, descriptor);
+        keyedBackend.setCurrentKey("testKey");
+        ((InternalKvState) mapState).setCurrentNamespace(1);
+        mapState.put(1, "1");
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+
+        ForStKeyedStateBackend<String> asyncKeyedStateBackend =
+                setUpAsyncKeyedStateBackend(Collections.singletonList(stateHandle));
+
+        MapStateDescriptor<Integer, String> newStateDescriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+        try {
+            MapState<Integer, String> asyncMapState =
+                    asyncKeyedStateBackend.createState(
+                            1, IntSerializer.INSTANCE, newStateDescriptor);
+            fail();
+
+            context = aec.buildContext("testRecord", "testKey");
+            context.retain();
+            aec.setCurrentContext(context);
+            asyncMapState
+                    .asyncGet(1)
+                    .thenAccept(
+                            value -> {
+                                Assert.assertEquals(value, "1");

Review Comment:
   ```suggestion
                                 assertThat(value).isEqualTo("1");
   ```



##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {

Review Comment:
   We can remove `public` from junit5 test.



##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =
+                setUpAsyncKeyedStateBackend(Collections.emptyList());
+        MapStateDescriptor<Integer, String> descriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+
+        MapState<Integer, String> asyncMapState =
+                keyedBackend.createState(1, IntSerializer.INSTANCE, descriptor);
+
+        context = aec.buildContext("testRecord", "testKey");
+        context.retain();
+        aec.setCurrentContext(context);
+        asyncMapState.asyncPut(1, "1");
+        context.release();
+        aec.drainInflightRecords(0);
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+        ForStSyncKeyedStateBackend<String> syncKeyedStateBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend,
+                        env,
+                        StringSerializer.INSTANCE,
+                        Collections.singletonList(stateHandle));
+
+        try {
+            org.apache.flink.api.common.state.MapState<Integer, String> syncMapState =
+                    syncKeyedStateBackend.getOrCreateKeyedState(
+                            IntSerializer.INSTANCE,
+                            StateDescriptorUtils.transformFromV2ToV1(descriptor));
+            fail();
+
+            syncKeyedStateBackend.setCurrentKey("testKey");
+            ((InternalKvState) syncKeyedStateBackend).setCurrentNamespace(1);
+            Assert.assertEquals(syncMapState.get(1), "1");
+        } catch (Exception e) {
+            // Currently, ForStStateBackend does not support switching from Async to Sync, so this
+            // exception will be caught here
+            assertEquals(

Review Comment:
   It's better to use assertThat.
   ```suggestion
              assertThat(e).isInstanceOf(ClassCastException.class);
               assertThat(e.getMessage())
                       .contains(
                               "org.apache.flink.runtime.state.v2.RegisteredKeyAndUserKeyValueStateBackendMetaInfo " 
                                       + "cannot be cast to class org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo");
   ```



##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =
+                setUpAsyncKeyedStateBackend(Collections.emptyList());
+        MapStateDescriptor<Integer, String> descriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+
+        MapState<Integer, String> asyncMapState =
+                keyedBackend.createState(1, IntSerializer.INSTANCE, descriptor);
+
+        context = aec.buildContext("testRecord", "testKey");
+        context.retain();
+        aec.setCurrentContext(context);
+        asyncMapState.asyncPut(1, "1");
+        context.release();
+        aec.drainInflightRecords(0);
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+        ForStSyncKeyedStateBackend<String> syncKeyedStateBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend,
+                        env,
+                        StringSerializer.INSTANCE,
+                        Collections.singletonList(stateHandle));
+
+        try {
+            org.apache.flink.api.common.state.MapState<Integer, String> syncMapState =
+                    syncKeyedStateBackend.getOrCreateKeyedState(
+                            IntSerializer.INSTANCE,
+                            StateDescriptorUtils.transformFromV2ToV1(descriptor));
+            fail();
+
+            syncKeyedStateBackend.setCurrentKey("testKey");
+            ((InternalKvState) syncKeyedStateBackend).setCurrentNamespace(1);
+            Assert.assertEquals(syncMapState.get(1), "1");

Review Comment:
   ```suggestion
               assertThat(syncMapState.get(1)).isEqualTo("1");
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia merged PR #26440:
URL: https://github.com/apache/flink/pull/26440


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #26440:
URL: https://github.com/apache/flink/pull/26440#discussion_r2039523712


##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =
+                setUpAsyncKeyedStateBackend(Collections.emptyList());
+        MapStateDescriptor<Integer, String> descriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+
+        MapState<Integer, String> asyncMapState =
+                keyedBackend.createState(1, IntSerializer.INSTANCE, descriptor);
+
+        context = aec.buildContext("testRecord", "testKey");
+        context.retain();
+        aec.setCurrentContext(context);
+        asyncMapState.asyncPut(1, "1");
+        context.release();
+        aec.drainInflightRecords(0);
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+        ForStSyncKeyedStateBackend<String> syncKeyedStateBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend,
+                        env,
+                        StringSerializer.INSTANCE,
+                        Collections.singletonList(stateHandle));
+
+        try {
+            org.apache.flink.api.common.state.MapState<Integer, String> syncMapState =
+                    syncKeyedStateBackend.getOrCreateKeyedState(
+                            IntSerializer.INSTANCE,
+                            StateDescriptorUtils.transformFromV2ToV1(descriptor));
+            fail();
+
+            syncKeyedStateBackend.setCurrentKey("testKey");
+            ((InternalKvState) syncKeyedStateBackend).setCurrentNamespace(1);
+            Assert.assertEquals(syncMapState.get(1), "1");
+        } catch (Exception e) {
+            // Currently, ForStStateBackend does not support switching from Async to Sync, so this
+            // exception will be caught here
+            assertEquals(
+                    e.getMessage()
+                            .contains(
+                                    "org.apache.flink.runtime.state.v2.RegisteredKeyAndUserKeyValueStateBackendMetaInfo cannot be cast to class org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo"),
+                    true);
+        } finally {
+            IOUtils.closeQuietly(syncKeyedStateBackend);
+        }
+    }
+
+    @Test
+    void testForStTransFromSyncToAsync() throws Exception {
+        ForStSyncKeyedStateBackend<String> keyedBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend, env, StringSerializer.INSTANCE, Collections.emptyList());
+        org.apache.flink.api.common.state.MapStateDescriptor<Integer, String> descriptor =
+                new org.apache.flink.api.common.state.MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+        org.apache.flink.api.common.state.MapState<Integer, String> mapState =
+                keyedBackend.getOrCreateKeyedState(IntSerializer.INSTANCE, descriptor);
+        keyedBackend.setCurrentKey("testKey");
+        ((InternalKvState) mapState).setCurrentNamespace(1);
+        mapState.put(1, "1");
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {

Review Comment:
   I am curious what this line is doing? when will is be true and when will it be false?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "mayuehappy (via GitHub)" <gi...@apache.org>.
mayuehappy commented on code in PR #26440:
URL: https://github.com/apache/flink/pull/26440#discussion_r2041473339


##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =
+                setUpAsyncKeyedStateBackend(Collections.emptyList());
+        MapStateDescriptor<Integer, String> descriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+
+        MapState<Integer, String> asyncMapState =
+                keyedBackend.createState(1, IntSerializer.INSTANCE, descriptor);
+
+        context = aec.buildContext("testRecord", "testKey");
+        context.retain();
+        aec.setCurrentContext(context);
+        asyncMapState.asyncPut(1, "1");
+        context.release();
+        aec.drainInflightRecords(0);
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+        ForStSyncKeyedStateBackend<String> syncKeyedStateBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend,
+                        env,
+                        StringSerializer.INSTANCE,
+                        Collections.singletonList(stateHandle));
+
+        try {
+            org.apache.flink.api.common.state.MapState<Integer, String> syncMapState =
+                    syncKeyedStateBackend.getOrCreateKeyedState(
+                            IntSerializer.INSTANCE,
+                            StateDescriptorUtils.transformFromV2ToV1(descriptor));
+            fail();
+
+            syncKeyedStateBackend.setCurrentKey("testKey");
+            ((InternalKvState) syncKeyedStateBackend).setCurrentNamespace(1);
+            Assert.assertEquals(syncMapState.get(1), "1");
+        } catch (Exception e) {
+            // Currently, ForStStateBackend does not support switching from Async to Sync, so this
+            // exception will be caught here
+            assertEquals(
+                    e.getMessage()
+                            .contains(
+                                    "org.apache.flink.runtime.state.v2.RegisteredKeyAndUserKeyValueStateBackendMetaInfo cannot be cast to class org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo"),
+                    true);
+        } finally {
+            IOUtils.closeQuietly(syncKeyedStateBackend);
+        }
+    }
+
+    @Test
+    void testForStTransFromSyncToAsync() throws Exception {
+        ForStSyncKeyedStateBackend<String> keyedBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend, env, StringSerializer.INSTANCE, Collections.emptyList());
+        org.apache.flink.api.common.state.MapStateDescriptor<Integer, String> descriptor =
+                new org.apache.flink.api.common.state.MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+        org.apache.flink.api.common.state.MapState<Integer, String> mapState =
+                keyedBackend.getOrCreateKeyedState(IntSerializer.INSTANCE, descriptor);
+        keyedBackend.setCurrentKey("testKey");
+        ((InternalKvState) mapState).setCurrentNamespace(1);
+        mapState.put(1, "1");
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {

Review Comment:
    this line is wait for snapshot completing



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #26440:
URL: https://github.com/apache/flink/pull/26440#discussion_r2039520002


##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =
+                setUpAsyncKeyedStateBackend(Collections.emptyList());
+        MapStateDescriptor<Integer, String> descriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+
+        MapState<Integer, String> asyncMapState =
+                keyedBackend.createState(1, IntSerializer.INSTANCE, descriptor);
+
+        context = aec.buildContext("testRecord", "testKey");
+        context.retain();
+        aec.setCurrentContext(context);
+        asyncMapState.asyncPut(1, "1");
+        context.release();
+        aec.drainInflightRecords(0);
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+        ForStSyncKeyedStateBackend<String> syncKeyedStateBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend,
+                        env,
+                        StringSerializer.INSTANCE,
+                        Collections.singletonList(stateHandle));
+
+        try {
+            org.apache.flink.api.common.state.MapState<Integer, String> syncMapState =
+                    syncKeyedStateBackend.getOrCreateKeyedState(
+                            IntSerializer.INSTANCE,
+                            StateDescriptorUtils.transformFromV2ToV1(descriptor));
+            fail();

Review Comment:
   what is this line doing? I suggest a comment to explain - it appears to be in both tests



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "mayuehappy (via GitHub)" <gi...@apache.org>.
mayuehappy commented on PR #26440:
URL: https://github.com/apache/flink/pull/26440#issuecomment-2800572122

   > Thanks for the PR, left some comments about the usage of junit5, and the test logic looks good to me.
   
   @fredia  Thanks for the review , I've updated the PR  , PTAL ~


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #26440:
URL: https://github.com/apache/flink/pull/26440#discussion_r2039520002


##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =
+                setUpAsyncKeyedStateBackend(Collections.emptyList());
+        MapStateDescriptor<Integer, String> descriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+
+        MapState<Integer, String> asyncMapState =
+                keyedBackend.createState(1, IntSerializer.INSTANCE, descriptor);
+
+        context = aec.buildContext("testRecord", "testKey");
+        context.retain();
+        aec.setCurrentContext(context);
+        asyncMapState.asyncPut(1, "1");
+        context.release();
+        aec.drainInflightRecords(0);
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+        ForStSyncKeyedStateBackend<String> syncKeyedStateBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend,
+                        env,
+                        StringSerializer.INSTANCE,
+                        Collections.singletonList(stateHandle));
+
+        try {
+            org.apache.flink.api.common.state.MapState<Integer, String> syncMapState =
+                    syncKeyedStateBackend.getOrCreateKeyedState(
+                            IntSerializer.INSTANCE,
+                            StateDescriptorUtils.transformFromV2ToV1(descriptor));
+            fail();

Review Comment:
   what is this line doing?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on PR #26440:
URL: https://github.com/apache/flink/pull/26440#issuecomment-2796840427

   nit: the title of the pr is missing d's from `ForStKeyeStateBackend `and `ForStSyncKeyeStateBackend
   `
   should be `ForStKeyedStateBackend `and `ForStSyncKeyedStateBackend` I think


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #26440:
URL: https://github.com/apache/flink/pull/26440#issuecomment-2796308879

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "b2c4e2dfb786e4bffe055110b115f33a340cbe2a",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "b2c4e2dfb786e4bffe055110b115f33a340cbe2a",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * b2c4e2dfb786e4bffe055110b115f33a340cbe2a UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #26440:
URL: https://github.com/apache/flink/pull/26440#discussion_r2039528718


##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =
+                setUpAsyncKeyedStateBackend(Collections.emptyList());
+        MapStateDescriptor<Integer, String> descriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+
+        MapState<Integer, String> asyncMapState =
+                keyedBackend.createState(1, IntSerializer.INSTANCE, descriptor);
+
+        context = aec.buildContext("testRecord", "testKey");
+        context.retain();
+        aec.setCurrentContext(context);
+        asyncMapState.asyncPut(1, "1");
+        context.release();
+        aec.drainInflightRecords(0);
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+        ForStSyncKeyedStateBackend<String> syncKeyedStateBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend,
+                        env,
+                        StringSerializer.INSTANCE,
+                        Collections.singletonList(stateHandle));
+
+        try {
+            org.apache.flink.api.common.state.MapState<Integer, String> syncMapState =
+                    syncKeyedStateBackend.getOrCreateKeyedState(
+                            IntSerializer.INSTANCE,
+                            StateDescriptorUtils.transformFromV2ToV1(descriptor));
+            fail();
+
+            syncKeyedStateBackend.setCurrentKey("testKey");
+            ((InternalKvState) syncKeyedStateBackend).setCurrentNamespace(1);
+            Assert.assertEquals(syncMapState.get(1), "1");
+        } catch (Exception e) {
+            // Currently, ForStStateBackend does not support switching from Async to Sync, so this
+            // exception will be caught here
+            assertEquals(
+                    e.getMessage()
+                            .contains(
+                                    "org.apache.flink.runtime.state.v2.RegisteredKeyAndUserKeyValueStateBackendMetaInfo cannot be cast to class org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo"),
+                    true);
+        } finally {
+            IOUtils.closeQuietly(syncKeyedStateBackend);
+        }
+    }
+
+    @Test
+    void testForStTransFromSyncToAsync() throws Exception {
+        ForStSyncKeyedStateBackend<String> keyedBackend =
+                createSyncKeyedStateBackend(
+                        forStStateBackend, env, StringSerializer.INSTANCE, Collections.emptyList());
+        org.apache.flink.api.common.state.MapStateDescriptor<Integer, String> descriptor =
+                new org.apache.flink.api.common.state.MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+        org.apache.flink.api.common.state.MapState<Integer, String> mapState =
+                keyedBackend.getOrCreateKeyedState(IntSerializer.INSTANCE, descriptor);
+        keyedBackend.setCurrentKey("testKey");
+        ((InternalKvState) mapState).setCurrentNamespace(1);
+        mapState.put(1, "1");
+
+        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot =
+                keyedBackend.snapshot(
+                        1L,
+                        System.currentTimeMillis(),
+                        env.getCheckpointStorageAccess()
+                                .resolveCheckpointStorageLocation(
+                                        1L, CheckpointStorageLocationReference.getDefault()),
+                        CheckpointOptions.forCheckpointWithDefaultLocation());
+
+        if (!snapshot.isDone()) {
+            snapshot.run();
+        }
+        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
+        KeyedStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
+        IOUtils.closeQuietly(keyedBackend);
+
+        ForStKeyedStateBackend<String> asyncKeyedStateBackend =
+                setUpAsyncKeyedStateBackend(Collections.singletonList(stateHandle));
+
+        MapStateDescriptor<Integer, String> newStateDescriptor =
+                new MapStateDescriptor<>(
+                        "testState", IntSerializer.INSTANCE, StringSerializer.INSTANCE);
+        try {
+            MapState<Integer, String> asyncMapState =
+                    asyncKeyedStateBackend.createState(
+                            1, IntSerializer.INSTANCE, newStateDescriptor);
+            fail();
+
+            context = aec.buildContext("testRecord", "testKey");
+            context.retain();
+            aec.setCurrentContext(context);
+            asyncMapState
+                    .asyncGet(1)
+                    .thenAccept(
+                            value -> {
+                                Assert.assertEquals(value, "1");
+                            });
+            context.release();
+            aec.drainInflightRecords(0);
+        } catch (Exception e) {

Review Comment:
    could we have a more specific exception and /or message. It is not obvious from the exception text that this means,  ForStStateBackend does not support switching from Sync to Async, so this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-37554] Add unit tests for state compatibility between ForStKeyeStateBackend and ForStSyncKeyeStateBackend [flink]

Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #26440:
URL: https://github.com/apache/flink/pull/26440#discussion_r2039530913


##########
flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStAsyncAndSyncCompatibilityTest.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.flink.state.forst;
+
+import org.apache.flink.api.common.operators.MailboxExecutor;
+import org.apache.flink.api.common.state.v2.MapState;
+import org.apache.flink.api.common.state.v2.MapStateDescriptor;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController;
+import org.apache.flink.runtime.asyncprocessing.RecordContext;
+import org.apache.flink.runtime.asyncprocessing.declare.DeclarationManager;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.state.CheckpointStorageLocationReference;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.SnapshotResult;
+import org.apache.flink.runtime.state.internal.InternalKvState;
+import org.apache.flink.runtime.state.v2.StateDescriptorUtils;
+import org.apache.flink.state.forst.sync.ForStSyncKeyedStateBackend;
+import org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor;
+import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorImpl;
+import org.apache.flink.streaming.runtime.tasks.mailbox.TaskMailboxImpl;
+import org.apache.flink.util.IOUtils;
+
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.RunnableFuture;
+
+import static org.apache.flink.state.forst.ForStStateTestBase.getMockEnvironment;
+import static org.apache.flink.state.forst.ForStTestUtils.createKeyedStateBackend;
+import static org.apache.flink.state.forst.ForStTestUtils.createSyncKeyedStateBackend;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests for {@link ForStListState}. */
+public class ForStAsyncAndSyncCompatibilityTest {
+    protected ForStStateBackend forStStateBackend;
+    protected AsyncExecutionController<String> aec;
+    protected MailboxExecutor mailboxExecutor;
+
+    protected RecordContext<String> context;
+
+    protected MockEnvironment env;
+
+    @BeforeEach
+    public void setup(@TempDir File temporaryFolder) throws IOException {
+        FileSystem.initialize(new Configuration(), null);
+        Configuration configuration = new Configuration();
+        configuration.set(ForStOptions.PRIMARY_DIRECTORY, temporaryFolder.toURI().toString());
+        forStStateBackend = new ForStStateBackend().configure(configuration, null);
+
+        env = getMockEnvironment(temporaryFolder);
+
+        mailboxExecutor =
+                new MailboxExecutorImpl(
+                        new TaskMailboxImpl(), 0, StreamTaskActionExecutor.IMMEDIATE);
+    }
+
+    @Test
+    void testForStTransFromAsyncToSync() throws Exception {
+        ForStKeyedStateBackend<String> keyedBackend =

Review Comment:
   there seems to be duplication of code in these 2 tests. Could we arrange the code so there isn't? Maybe using a parametrized test?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org