muralibasani commented on code in PR #23879: URL: https://github.com/apache/kafka/pull/23879#discussion_r4341422966
########## clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/producer/TransactionsBounceTest.java: ########## @@ -0,0 +1,242 @@ +/* + * 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.kafka.clients.producer; + +import org.apache.kafka.clients.ClientsTestUtils; +import org.apache.kafka.clients.consumer.Consumer; +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.clients.consumer.GroupProtocol; +import org.apache.kafka.clients.consumer.OffsetAndMetadata; +import org.apache.kafka.clients.producer.internals.ErrorLoggingCallback; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.config.TopicConfig; +import org.apache.kafka.common.test.ClusterInstance; +import org.apache.kafka.common.test.api.ClusterConfigProperty; +import org.apache.kafka.common.test.api.ClusterTest; +import org.apache.kafka.common.test.api.ClusterTestDefaults; +import org.apache.kafka.common.test.api.Type; +import org.apache.kafka.coordinator.group.GroupCoordinatorConfig; +import org.apache.kafka.coordinator.transaction.TransactionLogConfig; +import org.apache.kafka.server.config.ReplicationConfigs; +import org.apache.kafka.server.config.ServerConfigs; +import org.apache.kafka.server.config.ServerLogConfigs; +import org.apache.kafka.server.util.ShutdownableThread; +import org.apache.kafka.test.TestUtils; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +@ClusterTestDefaults( + types = {Type.KRAFT}, + brokers = 4, + serverProperties = { + @ClusterConfigProperty(key = ServerLogConfigs.AUTO_CREATE_TOPICS_ENABLE_CONFIG, value = "false"), + @ClusterConfigProperty(key = ServerConfigs.MESSAGE_MAX_BYTES_CONFIG, value = "32768"), + @ClusterConfigProperty(key = ServerConfigs.CONTROLLED_SHUTDOWN_ENABLE_CONFIG, value = "true"), + @ClusterConfigProperty(key = ReplicationConfigs.AUTO_LEADER_REBALANCE_ENABLE_CONFIG, value = "false"), + // Use fewer partitions for __consumer_offsets so topic creation and leader assignment do not take too long. + @ClusterConfigProperty(key = GroupCoordinatorConfig.OFFSETS_TOPIC_PARTITIONS_CONFIG, value = "1"), + @ClusterConfigProperty(key = TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, value = "2"), + // Allow a small session timeout. + @ClusterConfigProperty(key = GroupCoordinatorConfig.GROUP_MIN_SESSION_TIMEOUT_MS_CONFIG, value = "10"), + @ClusterConfigProperty(key = GroupCoordinatorConfig.GROUP_INITIAL_REBALANCE_DELAY_MS_CONFIG, value = "0"), + @ClusterConfigProperty(key = TransactionLogConfig.TRANSACTIONS_TOPIC_PARTITIONS_CONFIG, value = "1"), + } +) +public class TransactionsBounceTest { + private static final int CONSUME_RECORD_TIMEOUT_MS = 30000; + private static final int NUM_PARTITIONS = 3; + private static final String OUTPUT_TOPIC = "output-topic"; + private static final String INPUT_TOPIC = "input-topic"; + private static final String TRANSACTION_STATUS_HEADER = "transactionStatus"; + + private final ClusterInstance cluster; + + TransactionsBounceTest(ClusterInstance cluster) { + this.cluster = cluster; + } + + @ClusterTest + public void testWithGroupMetadataClassic() throws Exception { + testBrokerFailure(GroupProtocol.CLASSIC); + } + + @ClusterTest + public void testWithGroupMetadataConsumer() throws Exception { + testBrokerFailure(GroupProtocol.CONSUMER); + } + + private void testBrokerFailure(GroupProtocol groupProtocol) throws Exception { + // Seed a topic with 10000 records and copy it transactionally while constantly bouncing brokers. + int numInputRecords = 10000; + Map<String, String> topicConfigs = Map.of(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "2"); + cluster.createTopic(INPUT_TOPIC, NUM_PARTITIONS, (short) 3, topicConfigs); + cluster.createTopic(OUTPUT_TOPIC, NUM_PARTITIONS, (short) 3, topicConfigs); + + try (Producer<byte[], byte[]> producer = cluster.producer()) { + for (int i = 0; i < numInputRecords; i++) { + byte[] value = Integer.toString(i).getBytes(StandardCharsets.UTF_8); + producer.send(new ProducerRecord<>(INPUT_TOPIC, value, value)); + } + producer.flush(); + } + + try (Consumer<byte[], byte[]> consumer = createConsumerAndSubscribe("myGroup", INPUT_TOPIC, groupProtocol, false); + Producer<byte[], byte[]> producer = cluster.producer(Map.of( + ProducerConfig.BATCH_SIZE_CONFIG, 512, + ProducerConfig.TRANSACTIONAL_ID_CONFIG, "test-txn" + ))) { + producer.initTransactions(); + BounceScheduler scheduler = new BounceScheduler(); + scheduler.start(); + try { + int numMessagesProcessed = 0; + int iteration = 0; + while (numMessagesProcessed < numInputRecords) { + int toRead = Math.min(200, numInputRecords - numMessagesProcessed); + List<ConsumerRecord<byte[], byte[]>> records = ClientsTestUtils.consumeRecords( + consumer, toRead, Integer.MAX_VALUE, CONSUME_RECORD_TIMEOUT_MS); + + producer.beginTransaction(); + boolean shouldAbort = iteration % 3 == 0; + for (ConsumerRecord<byte[], byte[]> record : records) { + ProducerRecord<byte[], byte[]> outputRecord = new ProducerRecord<>(OUTPUT_TOPIC, record.key(), record.value()); + // Mark the intended outcome so the verifying consumer can detect any aborted records it receives. + outputRecord.headers().add(TRANSACTION_STATUS_HEADER, + (shouldAbort ? "aborted" : "committed").getBytes(StandardCharsets.UTF_8)); + producer.send(outputRecord, new ErrorLoggingCallback(OUTPUT_TOPIC, record.key(), record.value(), true)); + } + + Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>(); + for (TopicPartition partition : consumer.assignment()) { + offsets.put(partition, new OffsetAndMetadata(consumer.position(partition))); + } + producer.sendOffsetsToTransaction(offsets, consumer.groupMetadata()); + + if (shouldAbort) { + producer.abortTransaction(); + resetToCommittedPositions(consumer); + } else { + producer.commitTransaction(); + numMessagesProcessed += records.size(); + } + iteration++; + } + } finally { + scheduler.shutdown(); + } + assertNull(scheduler.failure, "Broker bouncer failed"); + } + + try (Consumer<byte[], byte[]> consumer = createConsumerAndSubscribe("randomGroup", OUTPUT_TOPIC, groupProtocol, true)) { + Map<TopicPartition, List<Integer>> recordsByPartition = new HashMap<>(); + List<ConsumerRecord<byte[], byte[]>> records = ClientsTestUtils.consumeRecords( + consumer, numInputRecords, Integer.MAX_VALUE, CONSUME_RECORD_TIMEOUT_MS); + for (ConsumerRecord<byte[], byte[]> record : records) { + var header = record.headers().lastHeader(TRANSACTION_STATUS_HEADER); + assertNotNull(header, "Expected a transaction status header"); + assertEquals("committed", new String(header.value(), StandardCharsets.UTF_8)); + TopicPartition partition = new TopicPartition(record.topic(), record.partition()); + recordsByPartition.computeIfAbsent(partition, __ -> new ArrayList<>()) + .add(Integer.parseInt(new String(record.value(), StandardCharsets.UTF_8))); + } + + Set<Integer> recordSet = new HashSet<>(); + for (List<Integer> partitionValues : recordsByPartition.values()) { + assertEquals(partitionValues.stream().sorted().collect(Collectors.toList()), partitionValues, + "Out of order messages detected"); + recordSet.addAll(partitionValues); + } + assertEquals(numInputRecords, records.size(), "Unexpected number of committed records"); + assertEquals(numInputRecords, recordSet.size()); + assertEquals(IntStream.range(0, numInputRecords).boxed().collect(Collectors.toSet()), recordSet); + } + } + + private Consumer<byte[], byte[]> createConsumerAndSubscribe( + String groupId, + String topic, + GroupProtocol groupProtocol, + boolean readCommitted + ) { + Consumer<byte[], byte[]> consumer = cluster.consumer(Map.of( + ConsumerConfig.GROUP_ID_CONFIG, groupId, + ConsumerConfig.GROUP_PROTOCOL_CONFIG, groupProtocol.name(), + ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false, + ConsumerConfig.ISOLATION_LEVEL_CONFIG, readCommitted ? "read_committed" : "read_uncommitted" + )); + consumer.subscribe(List.of(topic)); + return consumer; + } + + private void resetToCommittedPositions(Consumer<byte[], byte[]> consumer) { + Map<TopicPartition, OffsetAndMetadata> committed = consumer.committed(consumer.assignment()); + for (TopicPartition partition : consumer.assignment()) { + OffsetAndMetadata offset = committed.get(partition); + if (offset == null) { + consumer.seekToBeginning(List.of(partition)); + } else { + consumer.seek(partition, offset.offset()); + } + } + } + + private class BounceScheduler extends ShutdownableThread { + private volatile Throwable failure; + + BounceScheduler() { + super("daemon-broker-bouncer", false); + } + + @Override + public void doWork() { + try { + for (int brokerId : cluster.brokerIds()) { + log.trace("Shutting down broker {}", brokerId); + cluster.shutdownBroker(brokerId); + cluster.brokers().get(brokerId).awaitShutdown(); + TimeUnit.MILLISECONDS.sleep(500); + cluster.startBroker(brokerId); + log.trace("Restarted broker {}", brokerId); + TimeUnit.MILLISECONDS.sleep(500); + } + for (int partition = 0; partition < NUM_PARTITIONS; partition++) { + TopicPartition topicPartition = new TopicPartition(OUTPUT_TOPIC, partition); + TestUtils.waitForCondition(() -> cluster.getLeaderBrokerId(topicPartition) >= 0, Review Comment: In this step, cluster.getLeaderBrokerId calls tp.leader().id() and it would throw null pointer, if partition has no leader. So waitForCondition can keep retrying and fails with generic assertion and "No leader elected for " never appears ? -- 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]
