pnowojski commented on code in PR #27071:
URL: https://github.com/apache/flink/pull/27071#discussion_r2420461409
##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/sequencedmultisetstate/SequencedMultiSetStateTest.java:
##########
@@ -0,0 +1,443 @@
+package org.apache.flink.table.runtime.sequencedmultisetstate;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.api.common.functions.SerializerFactory;
+import org.apache.flink.api.common.serialization.SerializerConfigImpl;
+import org.apache.flink.api.common.state.KeyedStateStore;
+import org.apache.flink.api.common.state.StateTtlConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.metrics.groups.UnregisteredMetricsGroup;
+import org.apache.flink.runtime.externalresource.ExternalResourceInfoProvider;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder;
+import org.apache.flink.runtime.query.KvStateRegistry;
+import org.apache.flink.runtime.state.AbstractKeyedStateBackend;
+import org.apache.flink.runtime.state.DefaultKeyedStateStore;
+import org.apache.flink.runtime.state.InternalKeyContext;
+import org.apache.flink.runtime.state.KeyGroupRange;
+import org.apache.flink.runtime.state.KeyedStateBackend;
+import org.apache.flink.runtime.state.KeyedStateBackendParametersImpl;
+import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
+import org.apache.flink.runtime.state.ttl.TtlTimeProvider;
+import org.apache.flink.streaming.api.TimeDomain;
+import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.generated.GeneratedHashFunction;
+import org.apache.flink.table.runtime.generated.GeneratedRecordEqualiser;
+import org.apache.flink.table.runtime.generated.HashFunction;
+import org.apache.flink.table.runtime.generated.RecordEqualiser;
+import
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType;
+import
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.SizeChangeInfo;
+import
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.Strategy;
+import org.apache.flink.table.runtime.typeutils.RowDataSerializer;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.testutils.junit.extensions.parameterized.Parameter;
+import
org.apache.flink.testutils.junit.extensions.parameterized.ParameterizedTestExtension;
+import org.apache.flink.testutils.junit.extensions.parameterized.Parameters;
+import org.apache.flink.util.function.BiConsumerWithException;
+import org.apache.flink.util.function.ThrowingConsumer;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.ExtendWith;
+import
org.testcontainers.shaded.org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Function;
+
+import static
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType.ALL_REMOVED;
+import static
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType.NOTHING_REMOVED;
+import static
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType.REMOVED_LAST_ADDED;
+import static
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType.REMOVED_OTHER;
+import static org.apache.flink.table.runtime.util.StreamRecordUtils.row;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+/** Test for various implementations of {@link SequencedMultiSetState}. */
+@SuppressWarnings({"SameParameterValue", "unused"})
+@ExtendWith(ParameterizedTestExtension.class)
+public class SequencedMultiSetStateTest {
+
+ @Parameter(0)
+ private Strategy strategy;
+
+ @Parameter(1)
+ private long adaptiveLowThresholdOverride;
+
+ @Parameter(2)
+ private long adaptiveHighThresholdOverride;
+
+ @Parameters(name = "strategy={0}, lowThreshold={1}, highThreshold={2}")
+ public static Object[][] parameters() {
+ return new Object[][] {
+ new Object[] {Strategy.VALUE_STATE, -1, -1},
+ new Object[] {Strategy.MAP_STATE, -1, -1},
+ new Object[] {Strategy.ADAPTIVE, 0, 1},
+ new Object[] {Strategy.ADAPTIVE, 1, 2},
+ new Object[] {Strategy.ADAPTIVE, 0, 10},
+ new Object[] {Strategy.ADAPTIVE, 9, 10},
+ };
+ }
+
+ // for simplicity, all tests use string type only, with row key being the
1st column
+ private static final LogicalType VARCHAR =
DataTypes.VARCHAR(50).getLogicalType();
+ public static final int KEY_POS = 0;
+
+ @TestTemplate
+ public void testBasicFlow() throws Exception {
+ runTest(
+ (state, keyContext) -> {
+ keyContext.setCurrentKey("sk1");
+ assertTrue(state.isEmpty());
+
+ state.add(row("key", "value"), 1L);
+ assertFalse(state.isEmpty());
+
+ keyContext.setCurrentKey("sk2");
+ assertTrue(state.isEmpty());
+
+ keyContext.setCurrentKey("sk1");
+ state.clear();
+ assertTrue(state.isEmpty());
Review Comment:
apart just `isEmpty` assert also `get` (`iterate`?) via
`assertStateContents(..., [])`?
##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/sequencedmultisetstate/SequencedMultiSetStateTest.java:
##########
@@ -0,0 +1,443 @@
+package org.apache.flink.table.runtime.sequencedmultisetstate;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.api.common.functions.SerializerFactory;
+import org.apache.flink.api.common.serialization.SerializerConfigImpl;
+import org.apache.flink.api.common.state.KeyedStateStore;
+import org.apache.flink.api.common.state.StateTtlConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.base.StringSerializer;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.metrics.groups.UnregisteredMetricsGroup;
+import org.apache.flink.runtime.externalresource.ExternalResourceInfoProvider;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder;
+import org.apache.flink.runtime.query.KvStateRegistry;
+import org.apache.flink.runtime.state.AbstractKeyedStateBackend;
+import org.apache.flink.runtime.state.DefaultKeyedStateStore;
+import org.apache.flink.runtime.state.InternalKeyContext;
+import org.apache.flink.runtime.state.KeyGroupRange;
+import org.apache.flink.runtime.state.KeyedStateBackend;
+import org.apache.flink.runtime.state.KeyedStateBackendParametersImpl;
+import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
+import org.apache.flink.runtime.state.ttl.TtlTimeProvider;
+import org.apache.flink.streaming.api.TimeDomain;
+import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.generated.GeneratedHashFunction;
+import org.apache.flink.table.runtime.generated.GeneratedRecordEqualiser;
+import org.apache.flink.table.runtime.generated.HashFunction;
+import org.apache.flink.table.runtime.generated.RecordEqualiser;
+import
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType;
+import
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.SizeChangeInfo;
+import
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.Strategy;
+import org.apache.flink.table.runtime.typeutils.RowDataSerializer;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.testutils.junit.extensions.parameterized.Parameter;
+import
org.apache.flink.testutils.junit.extensions.parameterized.ParameterizedTestExtension;
+import org.apache.flink.testutils.junit.extensions.parameterized.Parameters;
+import org.apache.flink.util.function.BiConsumerWithException;
+import org.apache.flink.util.function.ThrowingConsumer;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.ExtendWith;
+import
org.testcontainers.shaded.org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Function;
+
+import static
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType.ALL_REMOVED;
+import static
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType.NOTHING_REMOVED;
+import static
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType.REMOVED_LAST_ADDED;
+import static
org.apache.flink.table.runtime.sequencedmultisetstate.SequencedMultiSetState.RemovalResultType.REMOVED_OTHER;
+import static org.apache.flink.table.runtime.util.StreamRecordUtils.row;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+/** Test for various implementations of {@link SequencedMultiSetState}. */
+@SuppressWarnings({"SameParameterValue", "unused"})
+@ExtendWith(ParameterizedTestExtension.class)
+public class SequencedMultiSetStateTest {
+
+ @Parameter(0)
+ private Strategy strategy;
+
+ @Parameter(1)
+ private long adaptiveLowThresholdOverride;
+
+ @Parameter(2)
+ private long adaptiveHighThresholdOverride;
+
+ @Parameters(name = "strategy={0}, lowThreshold={1}, highThreshold={2}")
+ public static Object[][] parameters() {
+ return new Object[][] {
+ new Object[] {Strategy.VALUE_STATE, -1, -1},
+ new Object[] {Strategy.MAP_STATE, -1, -1},
+ new Object[] {Strategy.ADAPTIVE, 0, 1},
+ new Object[] {Strategy.ADAPTIVE, 1, 2},
+ new Object[] {Strategy.ADAPTIVE, 0, 10},
+ new Object[] {Strategy.ADAPTIVE, 9, 10},
+ };
+ }
+
+ // for simplicity, all tests use string type only, with row key being the
1st column
+ private static final LogicalType VARCHAR =
DataTypes.VARCHAR(50).getLogicalType();
+ public static final int KEY_POS = 0;
+
+ @TestTemplate
+ public void testBasicFlow() throws Exception {
+ runTest(
+ (state, keyContext) -> {
+ keyContext.setCurrentKey("sk1");
+ assertTrue(state.isEmpty());
+
+ state.add(row("key", "value"), 1L);
+ assertFalse(state.isEmpty());
+
+ keyContext.setCurrentKey("sk2");
+ assertTrue(state.isEmpty());
+
+ keyContext.setCurrentKey("sk1");
+ state.clear();
+ assertTrue(state.isEmpty());
+ });
+ }
+
+ @TestTemplate
+ public void testAppend() throws Exception {
+ runTest(
+ state -> {
+ // should always keep appending
+ state.append(row("k1", "x"), 777L);
+ assertStateContents(state, Tuple2.of(row("k1", "x"),
777L));
+
+ state.append(row("k1", "x"), 778L);
+ assertStateContents(
+ state,
+ Tuple2.of(row("k1", "x"), 777L),
+ Tuple2.of(row("k1", "x"), 778L));
+
+ state.append(row("k2", "y"), 779L);
+ assertStateContents(
+ state,
+ Tuple2.of(row("k1", "x"), 777L),
+ Tuple2.of(row("k1", "x"), 778L),
+ Tuple2.of(row("k2", "y"), 779L));
+
+ state.append(row("k1", "x"), 777L);
+ assertStateContents(
+ state,
+ Tuple2.of(row("k1", "x"), 777L),
+ Tuple2.of(row("k1", "x"), 778L),
+ Tuple2.of(row("k2", "y"), 779L),
+ Tuple2.of(row("k1", "x"), 777L));
+ });
+ }
+
+ @TestTemplate
+ public void testAdd() throws Exception {
+ runTest(
+ state -> {
+ state.add(row("k1", "x"), 777L);
+ assertStateContents(state, row("k1", "x"), 777L);
+
+ state.add(row("k1", "y"), 778L);
+ assertStateContents(state, row("k1", "y"), 778L);
+
+ state.add(row("k2", "y"), 778L);
+ assertStateContents(
+ state,
+ Tuple2.of(row("k1", "y"), 778L),
+ Tuple2.of(row("k2", "y"), 778L));
+ });
Review Comment:
nit `add` another `k2`? And maybe mix order:
add(k1, x)
add(k2, x)
add(k2, y)
add(k1, x)
?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]