AHeise commented on a change in pull request #13351: URL: https://github.com/apache/flink/pull/13351#discussion_r496578188
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/channel/SequentialChannelStateReaderImplTest.java ########## @@ -0,0 +1,321 @@ +/* + * 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.channel; + +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.core.memory.MemorySegmentFactory; +import org.apache.flink.runtime.checkpoint.OperatorSubtaskState; +import org.apache.flink.runtime.checkpoint.StateObjectCollection; +import org.apache.flink.runtime.checkpoint.TaskStateSnapshot; +import org.apache.flink.runtime.io.network.buffer.Buffer; +import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler; +import org.apache.flink.runtime.io.network.buffer.NetworkBuffer; +import org.apache.flink.runtime.io.network.buffer.NetworkBufferPool; +import org.apache.flink.runtime.io.network.partition.ResultPartition; +import org.apache.flink.runtime.io.network.partition.ResultPartitionBuilder; +import org.apache.flink.runtime.io.network.partition.ResultSubpartition.BufferAndBacklog; +import org.apache.flink.runtime.io.network.partition.ResultSubpartitionView; +import org.apache.flink.runtime.io.network.partition.consumer.BufferOrEvent; +import org.apache.flink.runtime.io.network.partition.consumer.InputGate; +import org.apache.flink.runtime.io.network.partition.consumer.SingleInputGate; +import org.apache.flink.runtime.io.network.partition.consumer.SingleInputGateBuilder; +import org.apache.flink.runtime.jobgraph.OperatorID; +import org.apache.flink.runtime.state.InputChannelStateHandle; +import org.apache.flink.runtime.state.ResultSubpartitionStateHandle; +import org.apache.flink.runtime.state.memory.ByteStreamStateHandle; +import org.apache.flink.util.function.ThrowingConsumer; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Random; +import java.util.function.BiFunction; +import java.util.function.Function; + +import static java.util.function.Function.identity; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; +import static java.util.stream.IntStream.range; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * {@link SequentialChannelStateReaderImpl} Test. + */ +@RunWith(Parameterized.class) +public class SequentialChannelStateReaderImplTest { + + @Parameterized.Parameters(name = "{0}: stateParLevel={1}, statePartsPerChannel={1}, stateBytesPerPart={2}, parLevel={4}, bufferSize={5}") + public static Object[][] parameters() { + return new Object[][]{ + {"NoStateAndNoChannels", 0, 0, 0, 0, 0}, + {"NoState", 0, 10, 10, 10, 10}, + {"ReadPermutedStateWithEqualBuffer", 10, 10, 10, 10, 10}, + {"ReadPermutedStateWithReducedBuffer", 10, 10, 10, 20, 10}, + {"ReadPermutedStateWithIncreasedBuffer", 10, 10, 10, 10, 20}, + }; + } + + private final ChannelStateSerializer serializer; + private final Random random; Review comment: The issue is rather that `Random` is always strictly slower than `ThreadLocalRandom` because of lock acquisitions. However, you are right, as long as we not using multiple threads, we do not have contention, so the difference is marginal. ########## File path: flink-streaming-java/src/test/java/org/apache/flink/streaming/util/StreamTaskUtil.java ########## @@ -29,9 +30,10 @@ */ public class StreamTaskUtil { - public static void waitTaskIsRunning(StreamTask<?, ?> task, CompletableFuture<Void> taskInvocation) throws InterruptedException { + public static void waitTaskIsRunning(StreamTask<?, ?> task, CompletableFuture<Void> taskInvocation) throws InterruptedException, ExecutionException { while (!task.isRunning()) { if (taskInvocation.isDone()) { + taskInvocation.get(); Review comment: An alternative way to propagate the exception would be: ``` taskInvocation.exceptionally(e -> { throw new AssertionError("Task has stopped", e); }); ``` It depends if you want the test to fail through assertions or through unexpected exception. ########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/channel/ChannelStateChunkReaderTest.java ########## @@ -0,0 +1,162 @@ +/* + * 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.channel; + +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.core.fs.FSDataInputStream; +import org.apache.flink.core.memory.MemorySegmentFactory; +import org.apache.flink.runtime.io.network.buffer.Buffer; +import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler; +import org.apache.flink.runtime.io.network.buffer.NetworkBuffer; +import org.apache.flink.runtime.state.memory.ByteStreamStateHandle; + +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import static org.apache.flink.util.Preconditions.checkArgument; +import static org.apache.flink.util.Preconditions.checkState; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * {@link ChannelStateChunkReader} test. + */ +public class ChannelStateChunkReaderTest { + + @Test(expected = TestException.class) + public void testBufferRecycledOnFailure() throws IOException { + FailingChannelStateSerializer serializer = new FailingChannelStateSerializer(); + TestRecoveredChannelStateHandler handler = new TestRecoveredChannelStateHandler(); + + try (FSDataInputStream stream = geStream(serializer, 10)) { + new ChannelStateChunkReader(serializer).readChunk(stream, serializer.getHeaderLength(), handler, "channelInfo"); + checkState(serializer.failed); + checkState(!handler.requestedBuffers.isEmpty()); + } finally { + assertTrue(handler.requestedBuffers.stream().allMatch(TestChannelStateByteBuffer::isRecycled)); + } + } + + @Test + public void testBuffersNotRequestedForEmptyStream() throws IOException { + ChannelStateSerializer serializer = new ChannelStateSerializerImpl(); + TestRecoveredChannelStateHandler handler = new TestRecoveredChannelStateHandler(); + + try (FSDataInputStream stream = geStream(serializer, 0)) { + new ChannelStateChunkReader(serializer).readChunk(stream, serializer.getHeaderLength(), handler, "channelInfo"); + } finally { + assertTrue(handler.requestedBuffers.isEmpty()); + } + } + + @Test + public void testNoSeekUnnecessarily() throws IOException { + final int offset = 123; + final FSDataInputStream stream = new FSDataInputStream() { + @Override + public long getPos() { + return offset; + } + + @Override + public void seek(long ignored) { + fail(); + } + + @Override + public int read() { + return 0; + } + }; + + new ChannelStateChunkReader(new ChannelStateSerializerImpl()) + .readChunk(stream, offset, new TestRecoveredChannelStateHandler(), "channelInfo"); + } + + private static class TestRecoveredChannelStateHandler implements RecoveredChannelStateHandler<Object, Object> { + private final List<TestChannelStateByteBuffer> requestedBuffers = new ArrayList<>(); + + @Override + public Tuple2<ChannelStateByteBuffer, Object> getBuffer(Object o) { + TestChannelStateByteBuffer buffer = new TestChannelStateByteBuffer(); + requestedBuffers.add(buffer); + return Tuple2.of(buffer, null); + } + + @Override + public void recover(Object o, Object o2) { + } + + @Override + public void close() throws Exception { + } + Review comment: When looking at formatting, I usually just look for consistency and there are a few classes with this extra new line and many without. But which way is better is probably not worth debating ;). ---------------------------------------------------------------- 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: us...@infra.apache.org