fredia commented on code in PR #24614: URL: https://github.com/apache/flink/pull/24614#discussion_r1554814874
########## flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/ContextStateFutureImplTest.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.runtime.asyncprocessing; + +import org.apache.flink.core.state.StateFutureUtils; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link ContextStateFutureImpl}. */ +public class ContextStateFutureImplTest { + + @Test + public void testThenApply() { + SingleStepRunner runner = new SingleStepRunner(); + KeyAccountingUnit<String, String> keyAccountingUnit = new KeyAccountingUnit<>(); + RecordContext<String, String> recordContext = + new RecordContext<>(keyAccountingUnit, "a", "b"); + + // validate + ContextStateFutureImpl<Void> future = + new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(1); + future.thenApply((v) -> 1L); + future.complete(null); + assertThat(runner.runThrough()).isTrue(); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + + // validate completion before callback + future = new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(1); + future.complete(null); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + future.thenApply((v) -> 1L); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + assertThat(runner.runThrough()).isFalse(); + } + + @Test + public void testThenAccept() { + SingleStepRunner runner = new SingleStepRunner(); + KeyAccountingUnit<String, String> keyAccountingUnit = new KeyAccountingUnit<>(); + RecordContext<String, String> recordContext = + new RecordContext<>(keyAccountingUnit, "a", "b"); + + // validate + ContextStateFutureImpl<Void> future = + new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(1); + future.thenAccept((v) -> {}); + future.complete(null); + assertThat(runner.runThrough()).isTrue(); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + + // validate completion before callback + future = new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(1); + future.complete(null); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + future.thenAccept((v) -> {}); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + assertThat(runner.runThrough()).isFalse(); + } + + @Test + public void testThenCompose() { + SingleStepRunner runner = new SingleStepRunner(); + KeyAccountingUnit<String, String> keyAccountingUnit = new KeyAccountingUnit<>(); + RecordContext<String, String> recordContext = + new RecordContext<>(keyAccountingUnit, "a", "b"); + + // validate + ContextStateFutureImpl<Void> future = + new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(1); + future.thenCompose((v) -> StateFutureUtils.completedFuture(1L)); + future.complete(null); + assertThat(runner.runThrough()).isTrue(); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + + // validate completion before callback + future = new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(1); + future.complete(null); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + future.thenCompose((v) -> StateFutureUtils.completedFuture(1L)); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + assertThat(runner.runThrough()).isFalse(); + } + + @Test + public void testThenCombine() { + SingleStepRunner runner = new SingleStepRunner(); + KeyAccountingUnit<String, String> keyAccountingUnit = new KeyAccountingUnit<>(); + RecordContext<String, String> recordContext = + new RecordContext<>(keyAccountingUnit, "a", "b"); + + // validate + ContextStateFutureImpl<Void> future1 = + new ContextStateFutureImpl<>(runner::submit, recordContext); + ContextStateFutureImpl<Void> future2 = + new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(2); + future1.thenCombine(future2, (v1, v2) -> 1L); + future1.complete(null); + future2.complete(null); + assertThat(runner.runThrough()).isTrue(); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + + // validate future1 completion before callback + future1 = new ContextStateFutureImpl<>(runner::submit, recordContext); + future2 = new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(2); + future1.complete(null); + future1.thenCombine(future2, (v1, v2) -> 1L); + assertThat(recordContext.getReferenceCount()).isGreaterThan(1); + future2.complete(null); + assertThat(runner.runThrough()).isTrue(); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + + // validate future2 completion before callback + future1 = new ContextStateFutureImpl<>(runner::submit, recordContext); + future2 = new ContextStateFutureImpl<>(runner::submit, recordContext); + assertThat(recordContext.getReferenceCount()).isEqualTo(2); + future2.complete(null); + future1.thenCombine(future2, (v1, v2) -> 1L); + assertThat(recordContext.getReferenceCount()).isGreaterThan(1); + future1.complete(null); + assertThat(runner.runThrough()).isTrue(); + assertThat(recordContext.getReferenceCount()).isEqualTo(0); + } + + @Test + public void testComplex() { + SingleStepRunner runner = new SingleStepRunner(); + KeyAccountingUnit<String, String> keyAccountingUnit = new KeyAccountingUnit<>(); + RecordContext<String, String> recordContext = + new RecordContext<>(keyAccountingUnit, "a", "b"); + + for (int i = 0; i < 32; i++) { // 2^5 for completion status combination Review Comment: Add some descriptions to explain what the bits in `i` represent? ########## flink-runtime/src/test/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionControllerTest.java: ########## @@ -0,0 +1,292 @@ +/* + * 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.asyncprocessing; + +import org.apache.flink.api.common.operators.MailboxExecutor; +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 org.apache.flink.runtime.asyncprocessing.StateRequest.RequestType; +import org.apache.flink.runtime.mailbox.SyncMailboxExecutor; +import org.apache.flink.util.Preconditions; + +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +/** Test for {@link AsyncExecutionController}. */ +class AsyncExecutionControllerTest { + + // TODO: this test is not well completed, cause buffering in AEC is not implemented. + // Yet, just for illustrating the interaction between AEC and Async state API. + @Test + void testBasicRun() { + TestAsyncExecutionController<String, String> aec = + new TestAsyncExecutionController<>( + new SyncMailboxExecutor(), new TestStateExecutor()); + TestUnderlyingState underlyingState = new TestUnderlyingState(); + TestValueState valueState = new TestValueState(aec, underlyingState); + AtomicInteger output = new AtomicInteger(); + Runnable userCode = + () -> { + valueState + .asyncValue() + .thenCompose( + val -> { + int updated = (val == null ? 1 : (val + 1)); + return valueState + .asyncUpdate(updated) + .thenCompose( + o -> + StateFutureUtils.completedFuture( + updated)); + }) + .thenAccept(val -> output.set(val)); + }; + + // ============================ element1 ============================ + String record1 = "key1-r1"; + String key1 = "key1"; + // Simulate the wrapping in {@link RecordProcessorUtils#getRecordProcessor()}, wrapping the + // record and key with RecordContext. + RecordContext<String, String> recordContext1 = aec.buildContext(record1, key1); + aec.setCurrentContext(recordContext1); + // execute user code + userCode.run(); + + // single-step run + assertThat(aec.activeBuffer.size()).isEqualTo(1); + assertThat(aec.keyAccountingUnit.occupiedCount()).isEqualTo(1); + aec.triggerIfNeeded(true); + assertThat(aec.activeBuffer.size()).isEqualTo(1); Review Comment: Add some descriptions here to explain what the contents of each step in active buffer are? -- 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