pnowojski commented on a change in pull request #7677: [FLINK-11249][kafka] Add migration tests for FlinkKafkaProdcuer and FlinkKafkaProducer011 URL: https://github.com/apache/flink/pull/7677#discussion_r276132947
########## File path: flink-connectors/flink-connector-kafka-base/src/test/java/org/apache/flink/streaming/connectors/kafka/KafkaMigrationTestBase.java ########## @@ -0,0 +1,171 @@ +/* + * 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.streaming.connectors.kafka; + +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.serialization.TypeInformationSerializationSchema; +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; +import org.apache.flink.runtime.checkpoint.OperatorSubtaskState; +import org.apache.flink.streaming.connectors.kafka.internals.KeyedSerializationSchemaWrapper; +import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness; +import org.apache.flink.streaming.util.OperatorSnapshotUtil; +import org.apache.flink.streaming.util.serialization.KeyedSerializationSchema; +import org.apache.flink.testutils.migration.MigrationVersion; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.Optional; +import java.util.Properties; + +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * The base class with migration tests for the Kafka Exactly-Once Producer. + */ +@SuppressWarnings("serial") +public abstract class KafkaMigrationTestBase extends KafkaTestBase { + + protected static final Logger LOG = LoggerFactory.getLogger(KafkaMigrationTestBase.class); + protected static final String TOPIC = "flink-kafka-producer-migration-test"; + + protected final MigrationVersion testMigrateVersion; + protected final TypeInformationSerializationSchema<Integer> integerSerializationSchema = + new TypeInformationSerializationSchema<>(BasicTypeInfo.INT_TYPE_INFO, new ExecutionConfig()); + protected final KeyedSerializationSchema<Integer> integerKeyedSerializationSchema = + new KeyedSerializationSchemaWrapper<>(integerSerializationSchema); + + /** + * TODO change this to the corresponding savepoint version to be written (e.g. {@link MigrationVersion#v1_3} for 1.3) + * TODO and remove all @Ignore annotations on write*Snapshot() methods to generate savepoints + * TODO Note: You should generate the savepoint based on the release branch instead of the master. + */ + protected final Optional<MigrationVersion> flinkGenerateSavepointVersion = Optional.empty(); + + public KafkaMigrationTestBase(MigrationVersion testMigrateVersion) { + this.testMigrateVersion = checkNotNull(testMigrateVersion); + } + + public String getOperatorSnapshotPath() { + return getOperatorSnapshotPath(testMigrateVersion); + } + + public String getOperatorSnapshotPath(MigrationVersion version) { + return "src/test/resources/kafka-migration-kafka-producer-flink-" + version + "-snapshot"; + } + + /** + * Override {@link KafkaTestBase}. Kafka Migration Tests are starting up Kafka/ZooKeeper cluster manually + */ + @BeforeClass + public static void prepare() throws Exception { + } + + /** + * Override {@link KafkaTestBase}. Kafka Migration Tests are starting up Kafka/ZooKeeper cluster manually + */ + @AfterClass + public static void shutDownServices() throws Exception { + } + + /** + * Manually run this to write binary snapshot data. + */ + @Ignore + @Test + public void writeSnapshot() throws Exception { + try { + checkState(flinkGenerateSavepointVersion.isPresent()); + startClusters(); + + OperatorSubtaskState snapshot = initializeTestState(); + OperatorSnapshotUtil.writeStateHandle(snapshot, getOperatorSnapshotPath(flinkGenerateSavepointVersion.get())); + } + finally { + shutdownClusters(); + } + } + + private OperatorSubtaskState initializeTestState() throws Exception { + try (OneInputStreamOperatorTestHarness testHarness = createTestHarness()) { + testHarness.setup(); + testHarness.open(); + + // Create a committed transaction + testHarness.processElement(42, 0L); + + // TODO: when stop with savepoint is available, replace this code with it (with stop with savepoint + // there won't be any pending transactions) + OperatorSubtaskState snapshot = testHarness.snapshot(0L, 1L); + // We kind of simulate stop with savepoint by making sure that notifyOfCompletedCheckpoint is called + testHarness.notifyOfCompletedCheckpoint(0L); + + // Create a Pending transaction + testHarness.processElement(43, 2L); + return snapshot; + } + } + + @SuppressWarnings("warning") + @Test + public void testRestoreProducer() throws Exception { + try { + startClusters(); + + initializeTestState(); + + try (OneInputStreamOperatorTestHarness testHarness = createTestHarness()) { Review comment: Good question. I'm not sure if we can change this behaviour with altering production code. However... ... regardless if (1) or (2) has happened here, from the perspective of this use case/scenario, the result is correct in both cases. However there is a different test that focus on (1) and (2) and differentiates between them `FlinkKafkaProducer011ITCase#testFailBeforeNotifyAndResumeWorkAfterwards`. I guess for completeness it would be best to add more test cases here, but it would prolong already very length tests and it depends what are we planning to support? So far in the long term we were aiming to "officially" support kafka migration stories only with "stop with savepoint" feature (in progress). Ideally we would want "stop with savepoint without side-effects" (no ongoing transactions) but work on this hasn't yet started. ---------------------------------------------------------------- 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
