flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tillrohrmann <...@git.apache.org>
Subject [GitHub] flink pull request #2108: [FLINK-4027] Flush FlinkKafkaProducer on checkpoin...
Date Wed, 22 Jun 2016 16:30:34 GMT
Github user tillrohrmann commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2108#discussion_r68087265
  
    --- Diff: flink-streaming-connectors/flink-connector-kafka-base/src/test/java/org/apache/flink/streaming/connectors/kafka/AtLeastOnceProducerTest.java
---
    @@ -0,0 +1,206 @@
    +/*
    + * 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.java.tuple.Tuple1;
    +import org.apache.flink.configuration.Configuration;
    +import org.apache.flink.streaming.connectors.kafka.testutils.MockRuntimeContext;
    +import org.apache.flink.streaming.util.serialization.KeyedSerializationSchema;
    +import org.apache.flink.streaming.util.serialization.KeyedSerializationSchemaWrapper;
    +import org.apache.flink.streaming.util.serialization.SimpleStringSchema;
    +import org.apache.kafka.clients.producer.Callback;
    +import org.apache.kafka.clients.producer.KafkaProducer;
    +import org.apache.kafka.clients.producer.Producer;
    +import org.apache.kafka.clients.producer.ProducerRecord;
    +import org.apache.kafka.clients.producer.RecordMetadata;
    +import org.apache.kafka.common.Metric;
    +import org.apache.kafka.common.MetricName;
    +import org.apache.kafka.common.PartitionInfo;
    +import org.apache.kafka.common.serialization.ByteArraySerializer;
    +import org.junit.Assert;
    +import org.junit.Test;
    +
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Properties;
    +import java.util.concurrent.Future;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +/**
    + * Test ensuring that the producer is not dropping buffered records
    + */
    +@SuppressWarnings("unchecked")
    +public class AtLeastOnceProducerTest {
    +
    +	@Test
    +	public void testAtLeastOnceProducer() throws Exception {
    +		runTest(true);
    +	}
    +
    +	// This test ensures that the actual test fails if the flushing is disabled
    +	@Test(expected = AssertionError.class)
    +	public void ensureTestFails() throws Exception {
    +		runTest(false);
    +	}
    +
    +	private void runTest(boolean flushOnCheckpoint) throws Exception {
    +		Properties props = new Properties();
    +		final TestingKafkaProducer<String> producer = new TestingKafkaProducer<>("someTopic",
new KeyedSerializationSchemaWrapper<>(new SimpleStringSchema()), props);
    +		producer.setFlushOnCheckpoint(flushOnCheckpoint);
    +		producer.setRuntimeContext(new MockRuntimeContext(0, 1));
    +
    +		producer.open(new Configuration());
    +
    +		for(int i = 0; i < 100; i++) {
    +			producer.invoke("msg-" + i);
    +		}
    +		// start a thread confirming all pending records
    +		final Tuple1<Throwable> runnableError = new Tuple1<>(null);
    +		final AtomicBoolean markOne = new AtomicBoolean(false);
    +		Runnable confirmer = new Runnable() {
    +			@Override
    +			public void run() {
    +				try {
    +					MockProducer mp = producer.getProducerInstance();
    +					List<Callback> pending = mp.getPending();
    +
    +					// we ensure thread A is locked and didn't reach markOne
    +					// give thread A some time to really reach the snapshot state
    +					Thread.sleep(500);
    --- End diff --
    
    Tests based on `Thread.sleep` are bound to become flakey on Travis. Usually it is better
to synchronize on futures or locks.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message