Zakelly commented on code in PR #25233: URL: https://github.com/apache/flink/pull/25233#discussion_r1734500935
########## flink-runtime/src/test/java/org/apache/flink/runtime/state/v2/AsyncKeyedStateBackendAdaptorTest.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.runtime.state.v2; + +import org.apache.flink.api.common.state.State; +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue; +import org.apache.flink.runtime.state.Keyed; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateFunction; +import org.apache.flink.runtime.state.PriorityComparable; +import org.apache.flink.runtime.state.StateSnapshotTransformer; +import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement; +import org.apache.flink.runtime.state.v2.adaptor.AsyncKeyedStateBackendAdaptor; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** Tests for {@link AsyncKeyedStateBackendAdaptor}. */ +public class AsyncKeyedStateBackendAdaptorTest { + + @Test + public void testAdapt() throws Exception { + KeyedStateBackend<Integer> keyedStateBackend = new TestKeyedStateBackend(); + AsyncKeyedStateBackendAdaptor<Integer> adaptor = + new AsyncKeyedStateBackendAdaptor(keyedStateBackend); + StateDescriptor descriptor = Review Comment: ```suggestion StateDescriptor<Integer> descriptor = ``` ########## flink-runtime/src/test/java/org/apache/flink/runtime/state/v2/AsyncKeyedStateBackendAdaptorTest.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.runtime.state.v2; + +import org.apache.flink.api.common.state.State; +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue; +import org.apache.flink.runtime.state.Keyed; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateFunction; +import org.apache.flink.runtime.state.PriorityComparable; +import org.apache.flink.runtime.state.StateSnapshotTransformer; +import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement; +import org.apache.flink.runtime.state.v2.adaptor.AsyncKeyedStateBackendAdaptor; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** Tests for {@link AsyncKeyedStateBackendAdaptor}. */ +public class AsyncKeyedStateBackendAdaptorTest { + + @Test + public void testAdapt() throws Exception { + KeyedStateBackend<Integer> keyedStateBackend = new TestKeyedStateBackend(); + AsyncKeyedStateBackendAdaptor<Integer> adaptor = + new AsyncKeyedStateBackendAdaptor(keyedStateBackend); + StateDescriptor descriptor = + new ValueStateDescriptor<>("testState", BasicTypeInfo.INT_TYPE_INFO); + + org.apache.flink.api.common.state.v2.ValueState<Integer> valueState = + (org.apache.flink.api.common.state.v2.ValueState<Integer>) + adaptor.createState(null, null, descriptor); + + // test synchronous interfaces. + valueState.clear(); + valueState.update(10); + assertEquals(10, valueState.value().intValue()); Review Comment: We should use Junit5 `assertThat(xxx).isEqualTo()` clause. ########## flink-runtime/src/test/java/org/apache/flink/runtime/state/v2/AsyncKeyedStateBackendAdaptorTest.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.runtime.state.v2; + +import org.apache.flink.api.common.state.State; +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue; +import org.apache.flink.runtime.state.Keyed; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateFunction; +import org.apache.flink.runtime.state.PriorityComparable; +import org.apache.flink.runtime.state.StateSnapshotTransformer; +import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement; +import org.apache.flink.runtime.state.v2.adaptor.AsyncKeyedStateBackendAdaptor; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** Tests for {@link AsyncKeyedStateBackendAdaptor}. */ +public class AsyncKeyedStateBackendAdaptorTest { + + @Test + public void testAdapt() throws Exception { + KeyedStateBackend<Integer> keyedStateBackend = new TestKeyedStateBackend(); + AsyncKeyedStateBackendAdaptor<Integer> adaptor = + new AsyncKeyedStateBackendAdaptor(keyedStateBackend); + StateDescriptor descriptor = + new ValueStateDescriptor<>("testState", BasicTypeInfo.INT_TYPE_INFO); + + org.apache.flink.api.common.state.v2.ValueState<Integer> valueState = + (org.apache.flink.api.common.state.v2.ValueState<Integer>) + adaptor.createState(null, null, descriptor); Review Comment: ```suggestion adaptor.createState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, descriptor); ``` ########## flink-runtime/src/main/java/org/apache/flink/runtime/state/v2/adaptor/ValueStateWrapper.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.runtime.state.v2.adaptor; + +import org.apache.flink.api.common.state.v2.StateFuture; +import org.apache.flink.api.common.state.v2.ValueState; +import org.apache.flink.core.state.StateFutureUtils; + +import java.io.IOException; + +/** + * A wrapper that transforms {@link org.apache.flink.api.common.state.ValueState} into {@link + * ValueState}. + * + * @param <V> Type of the value in the state. + */ +public class ValueStateWrapper<V> implements ValueState<V> { + private final org.apache.flink.api.common.state.ValueState<V> valueState; + + public ValueStateWrapper(org.apache.flink.api.common.state.ValueState<V> valueState) { + this.valueState = valueState; + } + + public StateFuture<V> asyncValue() { + try { + return StateFutureUtils.completedFuture(valueState.value()); + } catch (Exception e) { + throw new RuntimeException("Error while getting value from raw ValueState", e); + } + } + + public StateFuture<Void> asyncUpdate(V value) { + try { + valueState.update(value); + } catch (IOException e) { + throw new RuntimeException("Error while updating value from raw ValueState", e); + } + return StateFutureUtils.completedFuture(null); + } + + public V value() { + V value; + try { + value = valueState.value(); + } catch (Exception e) { + throw new RuntimeException("Error while getting value from raw ValueState", e); + } + return value; Review Comment: ```suggestion try { return valueState.value(); } catch (Exception e) { throw new RuntimeException("Error while getting value from raw ValueState", e); } ``` ########## flink-runtime/src/main/java/org/apache/flink/runtime/state/v2/adaptor/ValueStateWrapper.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.runtime.state.v2.adaptor; + +import org.apache.flink.api.common.state.v2.StateFuture; +import org.apache.flink.api.common.state.v2.ValueState; +import org.apache.flink.core.state.StateFutureUtils; + +import java.io.IOException; + +/** + * A wrapper that transforms {@link org.apache.flink.api.common.state.ValueState} into {@link + * ValueState}. + * + * @param <V> Type of the value in the state. + */ +public class ValueStateWrapper<V> implements ValueState<V> { + private final org.apache.flink.api.common.state.ValueState<V> valueState; + + public ValueStateWrapper(org.apache.flink.api.common.state.ValueState<V> valueState) { + this.valueState = valueState; + } + + public StateFuture<V> asyncValue() { + try { + return StateFutureUtils.completedFuture(valueState.value()); + } catch (Exception e) { + throw new RuntimeException("Error while getting value from raw ValueState", e); + } + } + + public StateFuture<Void> asyncUpdate(V value) { + try { + valueState.update(value); + } catch (IOException e) { + throw new RuntimeException("Error while updating value from raw ValueState", e); + } + return StateFutureUtils.completedFuture(null); Review Comment: ```suggestion return StateFutureUtils. completedVoidFuture(); ``` ########## flink-runtime/src/test/java/org/apache/flink/runtime/state/v2/AsyncKeyedStateBackendAdaptorTest.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.runtime.state.v2; + +import org.apache.flink.api.common.state.State; +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue; +import org.apache.flink.runtime.state.Keyed; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateFunction; +import org.apache.flink.runtime.state.PriorityComparable; +import org.apache.flink.runtime.state.StateSnapshotTransformer; +import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement; +import org.apache.flink.runtime.state.v2.adaptor.AsyncKeyedStateBackendAdaptor; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** Tests for {@link AsyncKeyedStateBackendAdaptor}. */ +public class AsyncKeyedStateBackendAdaptorTest { + + @Test + public void testAdapt() throws Exception { + KeyedStateBackend<Integer> keyedStateBackend = new TestKeyedStateBackend(); + AsyncKeyedStateBackendAdaptor<Integer> adaptor = + new AsyncKeyedStateBackendAdaptor(keyedStateBackend); + StateDescriptor descriptor = + new ValueStateDescriptor<>("testState", BasicTypeInfo.INT_TYPE_INFO); + + org.apache.flink.api.common.state.v2.ValueState<Integer> valueState = + (org.apache.flink.api.common.state.v2.ValueState<Integer>) Review Comment: Remove this line if applied `StateDescriptor<Integer>` in line 51 ########## flink-runtime/src/test/java/org/apache/flink/runtime/state/v2/AsyncKeyedStateBackendAdaptorTest.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.runtime.state.v2; + +import org.apache.flink.api.common.state.State; +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue; +import org.apache.flink.runtime.state.Keyed; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateFunction; +import org.apache.flink.runtime.state.PriorityComparable; +import org.apache.flink.runtime.state.StateSnapshotTransformer; +import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement; +import org.apache.flink.runtime.state.v2.adaptor.AsyncKeyedStateBackendAdaptor; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** Tests for {@link AsyncKeyedStateBackendAdaptor}. */ +public class AsyncKeyedStateBackendAdaptorTest { + + @Test + public void testAdapt() throws Exception { + KeyedStateBackend<Integer> keyedStateBackend = new TestKeyedStateBackend(); + AsyncKeyedStateBackendAdaptor<Integer> adaptor = + new AsyncKeyedStateBackendAdaptor(keyedStateBackend); Review Comment: ```suggestion new AsyncKeyedStateBackendAdaptor<>(keyedStateBackend); ``` ########## flink-runtime/src/main/java/org/apache/flink/runtime/state/v2/adaptor/ValueStateWrapper.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.runtime.state.v2.adaptor; + +import org.apache.flink.api.common.state.v2.StateFuture; +import org.apache.flink.api.common.state.v2.ValueState; +import org.apache.flink.core.state.StateFutureUtils; + +import java.io.IOException; + +/** + * A wrapper that transforms {@link org.apache.flink.api.common.state.ValueState} into {@link + * ValueState}. + * + * @param <V> Type of the value in the state. + */ +public class ValueStateWrapper<V> implements ValueState<V> { + private final org.apache.flink.api.common.state.ValueState<V> valueState; + + public ValueStateWrapper(org.apache.flink.api.common.state.ValueState<V> valueState) { + this.valueState = valueState; + } + + public StateFuture<V> asyncValue() { + try { + return StateFutureUtils.completedFuture(valueState.value()); + } catch (Exception e) { + throw new RuntimeException("Error while getting value from raw ValueState", e); + } + } + + public StateFuture<Void> asyncUpdate(V value) { + try { + valueState.update(value); + } catch (IOException e) { + throw new RuntimeException("Error while updating value from raw ValueState", e); + } + return StateFutureUtils.completedFuture(null); + } + + public V value() { + V value; + try { + value = valueState.value(); + } catch (Exception e) { + throw new RuntimeException("Error while getting value from raw ValueState", e); + } + return value; + } + + public void update(V value) { + try { + valueState.update(value); + } catch (IOException e) { + throw new RuntimeException("Error while updating value from raw ValueState", e); + } + } + + public StateFuture<Void> asyncClear() { + valueState.clear(); + return StateFutureUtils.completedFuture(null); Review Comment: ```suggestion return StateFutureUtils.completedVoidFuture(); ``` ########## flink-runtime/src/main/java/org/apache/flink/runtime/state/v2/adaptor/AsyncKeyedStateBackendAdaptor.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.runtime.state.v2.adaptor; + +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.state.v2.State; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.runtime.asyncprocessing.StateExecutor; +import org.apache.flink.runtime.asyncprocessing.StateRequestHandler; +import org.apache.flink.runtime.state.AsyncKeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.v2.StateDescriptor; +import org.apache.flink.runtime.state.v2.StateDescriptorUtils; + +import javax.annotation.Nonnull; + +import java.io.IOException; + +/** + * A adaptor that transforms {@link KeyedStateBackend} into {@link AsyncKeyedStateBackend}. + * + * @param <K> The key by which state is keyed. + */ +public class AsyncKeyedStateBackendAdaptor<K> implements AsyncKeyedStateBackend { + private final KeyedStateBackend<K> keyedStateBackend; + + public AsyncKeyedStateBackendAdaptor(KeyedStateBackend<K> keyedStateBackend) { + this.keyedStateBackend = keyedStateBackend; + } + + @Override + public void setup(@Nonnull StateRequestHandler stateRequestHandler) {} + + @Nonnull + @Override + public <N, S extends State, SV> S createState( + @Nonnull N defaultNamespace, + @Nonnull TypeSerializer<N> namespaceSerializer, + @Nonnull StateDescriptor<SV> stateDesc) + throws Exception { + org.apache.flink.api.common.state.StateDescriptor rawStateDesc = + StateDescriptorUtils.transformFromV2ToV1(stateDesc); + org.apache.flink.api.common.state.State rawState = + keyedStateBackend.getOrCreateKeyedState(namespaceSerializer, rawStateDesc); + switch (rawStateDesc.getType()) { + case VALUE: + { + return (S) new ValueStateWrapper((ValueState) rawState); + } + default: + throw new UnsupportedOperationException( + String.format("Unsupported state type: %s", stateDesc.getType())); Review Comment: I'd not suggest the mix usage of `rawStateDesc.getType()` and `stateDesc.getType()` ########## flink-runtime/src/test/java/org/apache/flink/runtime/state/v2/AsyncKeyedStateBackendAdaptorTest.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.runtime.state.v2; + +import org.apache.flink.api.common.state.State; +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue; +import org.apache.flink.runtime.state.Keyed; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateFunction; +import org.apache.flink.runtime.state.PriorityComparable; +import org.apache.flink.runtime.state.StateSnapshotTransformer; +import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement; +import org.apache.flink.runtime.state.v2.adaptor.AsyncKeyedStateBackendAdaptor; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** Tests for {@link AsyncKeyedStateBackendAdaptor}. */ +public class AsyncKeyedStateBackendAdaptorTest { + + @Test + public void testAdapt() throws Exception { + KeyedStateBackend<Integer> keyedStateBackend = new TestKeyedStateBackend(); + AsyncKeyedStateBackendAdaptor<Integer> adaptor = + new AsyncKeyedStateBackendAdaptor(keyedStateBackend); + StateDescriptor descriptor = + new ValueStateDescriptor<>("testState", BasicTypeInfo.INT_TYPE_INFO); + + org.apache.flink.api.common.state.v2.ValueState<Integer> valueState = + (org.apache.flink.api.common.state.v2.ValueState<Integer>) + adaptor.createState(null, null, descriptor); + + // test synchronous interfaces. + valueState.clear(); + valueState.update(10); + assertEquals(10, valueState.value().intValue()); + + // test asynchronous interfaces. + valueState + .asyncClear() + .thenAccept( + clear -> { + valueState + .asyncUpdate(20) + .thenAccept( + update -> { + assertEquals(20, valueState.value().intValue()); Review Comment: I'd suggest `thenCompose` a `valueState.asyncValue()` here ########## flink-runtime/src/main/java/org/apache/flink/runtime/state/v2/adaptor/AsyncKeyedStateBackendAdaptor.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.runtime.state.v2.adaptor; + +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.state.v2.State; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.runtime.asyncprocessing.StateExecutor; +import org.apache.flink.runtime.asyncprocessing.StateRequestHandler; +import org.apache.flink.runtime.state.AsyncKeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.v2.StateDescriptor; +import org.apache.flink.runtime.state.v2.StateDescriptorUtils; + +import javax.annotation.Nonnull; + +import java.io.IOException; + +/** + * A adaptor that transforms {@link KeyedStateBackend} into {@link AsyncKeyedStateBackend}. + * + * @param <K> The key by which state is keyed. + */ +public class AsyncKeyedStateBackendAdaptor<K> implements AsyncKeyedStateBackend { + private final KeyedStateBackend<K> keyedStateBackend; + + public AsyncKeyedStateBackendAdaptor(KeyedStateBackend<K> keyedStateBackend) { + this.keyedStateBackend = keyedStateBackend; + } + + @Override + public void setup(@Nonnull StateRequestHandler stateRequestHandler) {} + + @Nonnull + @Override + public <N, S extends State, SV> S createState( + @Nonnull N defaultNamespace, + @Nonnull TypeSerializer<N> namespaceSerializer, + @Nonnull StateDescriptor<SV> stateDesc) + throws Exception { + org.apache.flink.api.common.state.StateDescriptor rawStateDesc = + StateDescriptorUtils.transformFromV2ToV1(stateDesc); + org.apache.flink.api.common.state.State rawState = + keyedStateBackend.getOrCreateKeyedState(namespaceSerializer, rawStateDesc); + switch (rawStateDesc.getType()) { + case VALUE: + { Review Comment: how about removing the braces? ########## flink-runtime/src/main/java/org/apache/flink/runtime/state/v2/StateDescriptorUtils.java: ########## @@ -0,0 +1,50 @@ +/* + * 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.runtime.state.v2; +/** + * Utilities for transforming {@link StateDescriptor} to {@link + * org.apache.flink.api.common.state.StateDescriptor}. + */ +public class StateDescriptorUtils { + private StateDescriptorUtils() {} + + public static org.apache.flink.api.common.state.StateDescriptor transformFromV2ToV1( + StateDescriptor stateDescriptor) { + switch (stateDescriptor.getType()) { + case VALUE: + return new org.apache.flink.api.common.state.ValueStateDescriptor( + stateDescriptor.getStateId(), stateDescriptor.getSerializer()); + case MAP: + return new org.apache.flink.api.common.state.MapStateDescriptor( + stateDescriptor.getStateId(), stateDescriptor.getSerializer(), null); Review Comment: This is not correct. You should convert `stateDescriptor` to `MapStateDescriptor` and `getUserKeySerializer`. Other branches need reconsideration as well. ########## flink-runtime/src/test/java/org/apache/flink/runtime/state/v2/AsyncKeyedStateBackendAdaptorTest.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.runtime.state.v2; + +import org.apache.flink.api.common.state.State; +import org.apache.flink.api.common.state.ValueState; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue; +import org.apache.flink.runtime.state.Keyed; +import org.apache.flink.runtime.state.KeyedStateBackend; +import org.apache.flink.runtime.state.KeyedStateFunction; +import org.apache.flink.runtime.state.PriorityComparable; +import org.apache.flink.runtime.state.StateSnapshotTransformer; +import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement; +import org.apache.flink.runtime.state.v2.adaptor.AsyncKeyedStateBackendAdaptor; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** Tests for {@link AsyncKeyedStateBackendAdaptor}. */ +public class AsyncKeyedStateBackendAdaptorTest { + + @Test + public void testAdapt() throws Exception { + KeyedStateBackend<Integer> keyedStateBackend = new TestKeyedStateBackend(); + AsyncKeyedStateBackendAdaptor<Integer> adaptor = + new AsyncKeyedStateBackendAdaptor(keyedStateBackend); + StateDescriptor descriptor = + new ValueStateDescriptor<>("testState", BasicTypeInfo.INT_TYPE_INFO); + + org.apache.flink.api.common.state.v2.ValueState<Integer> valueState = + (org.apache.flink.api.common.state.v2.ValueState<Integer>) + adaptor.createState(null, null, descriptor); + + // test synchronous interfaces. + valueState.clear(); + valueState.update(10); + assertEquals(10, valueState.value().intValue()); + + // test asynchronous interfaces. + valueState + .asyncClear() + .thenAccept( + clear -> { + valueState + .asyncUpdate(20) + .thenAccept( + update -> { Review Comment: ```suggestion empty -> { ``` -- 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-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org