Myasuka commented on a change in pull request #11491: 
[FLINK-16513][checkpointing] Unaligned checkpoints: checkpoint metadata
URL: https://github.com/apache/flink/pull/11491#discussion_r399976452
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/ChannelStateNoRescalingPartitionerTest.java
 ##########
 @@ -0,0 +1,129 @@
+/*
+ * 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.checkpoint;
+
+import org.apache.flink.runtime.checkpoint.channel.InputChannelInfo;
+import org.apache.flink.runtime.checkpoint.channel.ResultSubpartitionInfo;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.state.AbstractChannelStateHandle;
+import org.apache.flink.runtime.state.InputChannelStateHandle;
+import org.apache.flink.runtime.state.ResultSubpartitionStateHandle;
+import org.apache.flink.runtime.state.testutils.EmptyStreamStateHandle;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.function.Function;
+
+import static java.util.Collections.singletonList;
+import static 
org.apache.flink.runtime.checkpoint.StateAssignmentOperation.channelStateNonRescalingRepartitioner;
+import static org.apache.flink.runtime.checkpoint.StateObjectCollection.empty;
+import static 
org.apache.flink.runtime.checkpoint.StateObjectCollection.singleton;
+import static org.junit.Assert.fail;
+
+/**
+ * Simple test for
+ * {@link 
org.apache.flink.runtime.checkpoint.StateAssignmentOperation#channelStateNonRescalingRepartitioner
 channelStateNonRescalingRepartitioner}.
+ * It checks whether partitioner fails or not using property-based approach.
+ */
+@RunWith(Parameterized.class)
+public class ChannelStateNoRescalingPartitionerTest {
+
+       @Parameters
+       public static Collection<Object[]> parameters() {
+               List<Object[]> params = new ArrayList<>();
+               int[] parLevels = {1, 2};
+               int[] offsetSizes = {0, 1, 2};
+               final List<Function<OperatorSubtaskState, ?>> extractors = 
Arrays.asList(
+                       OperatorSubtaskState::getInputChannelState,
+                       OperatorSubtaskState::getResultSubpartitionState
+               );
+               for (int oldParallelism : parLevels) {
+                       for (int newParallelism : parLevels) {
+                               for (int offsetSize : offsetSizes) {
+                                       for (Function<OperatorSubtaskState, ?> 
stateExtractor : extractors) {
+                                               params.add(new 
Object[]{oldParallelism, newParallelism, offsetSize, stateExtractor});
+                                       }
+                               }
+                       }
+               }
+               return params;
+       }
+
+       private static final OperatorID OPERATOR_ID = new OperatorID();
+
+       private final int oldParallelism;
+       private final int newParallelism;
+       private final int offsetsSize;
+       private final Function<OperatorSubtaskState, ? extends 
StateObjectCollection<?>> extractState;
+
+       public ChannelStateNoRescalingPartitionerTest(int oldParallelism, int 
newParallelism, int offsetsSize, Function<OperatorSubtaskState, ? extends 
StateObjectCollection<?>> extractState) {
+               this.oldParallelism = oldParallelism;
+               this.newParallelism = newParallelism;
+               this.offsetsSize = offsetsSize;
+               this.extractState = extractState;
+       }
+
+       @Test
+       public <T extends AbstractChannelStateHandle<?>> void testNoRescaling() 
{
+               OperatorState state = new OperatorState(OPERATOR_ID, 
oldParallelism, oldParallelism);
+               state.putState(0, new OperatorSubtaskState(
+                       empty(),
+                       empty(),
+                       empty(),
+                       empty(),
+                       singleton(new InputChannelStateHandle(new 
InputChannelInfo(0, 0), new EmptyStreamStateHandle(), getOffset())),
+                       singleton(new ResultSubpartitionStateHandle(new 
ResultSubpartitionInfo(0, 0), new EmptyStreamStateHandle(), getOffset()))));
+               try {
+                       // noinspection unchecked
+                       
StateAssignmentOperation.reDistributePartitionableStates(
+                               singletonList(state),
+                               newParallelism,
+                               singletonList(OPERATOR_ID),
+                               (Function<OperatorSubtaskState, 
StateObjectCollection<T>>) this.extractState,
+                               channelStateNonRescalingRepartitioner("test"));
+               } catch (IllegalArgumentException e) {
+                       if (!shouldFail()) {
+                               throw e;
+                       } else {
+                               return;
+                       }
+               }
+               if (shouldFail()) {
+                       fail("expected to fail for: oldParallelism=" + 
oldParallelism + ", newParallelism=" + newParallelism + ", offsetsSize=" + 
offsetsSize + ", extractState=" + extractState);
+               }
+       }
 
 Review comment:
   I think we could change the verify logic to 
   ~~~java
        @Test
        public <T extends AbstractChannelStateHandle<?>> void testNoRescaling() 
{
                OperatorState state = new OperatorState(OPERATOR_ID, 
oldParallelism, oldParallelism);
                state.putState(0, new OperatorSubtaskState(
                        empty(),
                        empty(),
                        empty(),
                        empty(),
                        singleton(new InputChannelStateHandle(new 
InputChannelInfo(0, 0), new EmptyStreamStateHandle(), getOffset())),
                        singleton(new ResultSubpartitionStateHandle(new 
ResultSubpartitionInfo(0, 0), new EmptyStreamStateHandle(), getOffset()))));
                verifyNoRescalingChannelState(state, 
OperatorSubtaskState::getInputChannelState);
                verifyNoRescalingChannelState(state, 
OperatorSubtaskState::getResultSubpartitionState);
        }
   
        private <T extends AbstractChannelStateHandle<?>> void 
verifyNoRescalingChannelState(OperatorState state, 
Function<OperatorSubtaskState, StateObjectCollection<T>> stateExtractor) {
                try {
                        // noinspection unchecked
                        
StateAssignmentOperation.reDistributePartitionableStates(
                                singletonList(state),
                                newParallelism,
                                singletonList(OPERATOR_ID),
                                stateExtractor,
                                channelStateNonRescalingRepartitioner("test"));
                } catch (IllegalArgumentException e) {
                        if (!shouldFail()) {
                                throw e;
                        } else {
                                return;
                        }
                }
                if (shouldFail()) {
                        fail("expected to fail for: oldParallelism=" + 
oldParallelism + ", newParallelism=" + newParallelism + ", offsetsSize=" + 
offsetsSize + ", stateExtractor=" + stateExtractor);
                }
        }
   ~~~

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to