Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Status

Current state: Draft

Discussion threadhttps://lists.apache.org/thread.html/r9d1c89b871792655cd14ff585980bb0ace639d85d9200e239cc0e1cd%40%3Cdev.kafka.apache.org%3E

Voting threadhttps://lists.apache.org/thread.html/rbfe08bfb15e14db14c54d1ca5c86bfcd17dc952084ad0a4dec8255b6%40%3Cdev.kafka.apache.org%3E

...

MirrorMaker2 is currently implemented on Kafka Connect Framework, more specifically the Source Connector / Task, which do not provide exactly-once semantics (EOS) out-of-the-box, as discussed in https://github.com/confluentinc/kafka-connect-jdbc/issues/461,  https://github.com/apache/kafka/pull/5553

Jira
serverASF JIRA
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyKAFKA-6080
 and 
Jira
serverASF JIRA
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyKAFKA-3821
. Therefore MirrorMaker2 currently Therefore current MirrorMaker2 does not provide EOS as well.

This proposal is to provide an option to enable EOS for MirrorMaker 2 if no data loss or duplicate data is preferred. The key idea behind of this proposal is to extend SinkTask with a brand new MirrorSinkTask implementation, which leverages the consumer from WorkerSinkTask and provides which has an option to either deliver messagesmanage the consumer offsets in transactional way (similar high-level idea as HDFS Sink Connector), such that the messages can be delivered across clusters:

(1) Exactly-once: by a transactional producer in MirrorSinkTask and consumer offsets are committed within a transaction by the transactional producer

...

  • as mentioned above, Kafka Connect Source Connector / Task do not provide EOS by nature, mostly because of async and periodic source task offset commit, which in MirorrMaker case, the "offset" is consumer offset. So this is one of the blockers blocker to enable EOS without a lots of changes in WokerSourceTask WorkerSourceTask.
  • Since MirrorSinkTask (that extends SinkTask) is a new implementation, we can fully control how a producer is created (transactional v.s. non-transactional) and handle various Exception cases (especially in transactional mode), purely in the new implementation, rather than changing the existing producer in WorkerSourceTask
  • MirrorSourceTask explicitly avoided using subscribe() and instead handle rebalances and new topic-partitions explicitly. While MirrorSinkTask that extends SinkTask is initiated by WorkerSinkTask. WorkerSinkTask uses consumer.subscribe() in which the benefits of rebalances and auto-detection of new topics/partitions are out-of-the-box. When the consumer or its rebalance handling is upgraded in WorkerSinkTask as part of Kafka Connect, MirrorSinkTask will take the advantages transparently.
  • Since MirrorSinkTask is a new implementation, on the other end we can fully control how a producer is created (transactional v.s. non-transactional) and handle various Exception cases (especially in transactional mode), purely in the new implementation, rather than changing the existing producer in WorkerSourceTask
  • HDFS Sink Connector already achieved EOS, we can correctly implement MirrorSinkTask based on the methods of SinkTask by referring the best practices from HDFS Sink ConnectorMirrorSinkTask can intentionally return an empty result from preCommit() (defined in SinkTask) by overriding it to disable the consumer offset commit done by WorkerSinkTask, so that the transactional producer is able to commit consumer offset within one transaction.

Public Interfaces

New classes and interfaces include:

...

(1) in MirrorMaker case, there are source and target clusters. The Normally the consumer pulls the data and stores its offsets are typically stored in the source cluster, while then the producer takes over the data from the consumer and sends them to target cluster. However Kafka transaction can not happen across clusters out-of-the-box. What If we want EOS across clusters, what modifications need to be done?

A: The short answer is the consumer group offsets , which are supposed to be stored on source cluster, are maintained / committed by are managed, committed by the transactional producer and are stored on the target cluster instead.

But the However the consumer still has to live on the source cluster in order to pull the data and however the , but “source-of-truth” offsets are not no longer stored in source cluster (or stored in the source cluster, not not accurate). We propose to use the following idea to position rewind the consumer correctly while its when data mirroring task restarts or rebalances, while the “source-of-truth” of consumer offsets are stored in the target cluster: (the pseudocode are shown in below)

  • MirrorSinkTask don't rely on Connect's internal offsets tracking or __consumer_offsets on the source cluster.
  • the offsets are only written by transaction producer to the target cluster.
  • Consumer offsets are stored on the target cluster using a
  • "fake"
  • “fake” consumer group, that can be created programmatically as long as we know the name of consumer group. The
  • "fake"
  • “fake” means there would be no actual records being consumed by the group, just offsets being stored in __consumer_offsets topic.
  • all records are written in a transaction.
  • when MirrorSourceTask starts, it loads initial offsets from __
  • However, the __consumer_offsets topic on the target cluster (managed by the “fake” consumer group) is the “source of truth” offsets.

The outcome of the above idea:

    if the transaction succeeds, the
  • With the “fake” consumer group on target cluster, MirrorSinkTask don't rely on Connect's internal offsets tracking or __consumer_offsets
  • topic
  • on the
  • target
  • source cluster
  • is updated
  • .
  • the consumer offsets are only written by the producer evolved in the transaction to the target cluster.
  • all records are written in a transaction, as if in the single cluster
  • when MirrorSinkTask starts or rebalances, it loads initial offsets from if the transaction aborts, all data records are dropped, and the __consumer_offsets topic is not updated.when MirrorSinkTask starts/restarts, it resumes at the last committed offsets, as stored in on the target cluster.

Some items to pay attention in order to make The outcome of the above idea work correctly:

...

  • if the transaction succeeds, the __consumer_offsets topic on the target cluster is updated by following the current protocol of Exactly-Once framework
  • if the transaction aborts, all data records are dropped, and the __consumer_offsets topic on the target cluster is not updated.
  • when MirrorSinkTask starts/restarts, it resumes at the last committed offsets, as stored in the target cluster.

Some items to pay attention in order to make above idea work correctly:

  • If consumer group already exists on source cluster, while the "fake" consumer group (with same Group Id) on the target cluster does not exist or its offsets lower than the high watermark. To avoid duplicate data, it may need to do a one-time offline does not exist or its offsets lower than the high watermark. We need to do a one-time job to sync the offsets from source cluster to target cluster.

(2) Since the offsets of the

...

consumer group

...

on the source cluster is NOT "source of truth"

...

  and not involved in the transaction. Are they still being updated? Do we still need them in some cases?

A: the offsets of the consumer group on the source cluster are still being updated periodically and independently by the logics in WorkerSInkTask. However they may be lagging behind a little, since (1) they are not involved in transaction, (2) they are periodically committed.

However, they may be still useful in some cases: (1) measure the replication lag between the upstream produce on the source cluster and MirrorMaker's consumption. (2) restore the lost "fake" consumer group with small # of duplicate data

(2) The consumer in WorkerSinkTask periodically commits the offsets that are maintained by itself, how to disable this behavior so that transactional producer in MirrorSinkTask can control when and what to commit?

A: The SinkTask provides a preCommit() method let the implementation of SinkTask (here is MirrorSinkTask) to determine what offsets should be committed in WorkerSinkTask. If in transaction mode, we will intentionally return empty Map in preCommit() (see the code below), so that if the return of preCommit() is empty, the consumer of WorkerSinkTask will ignore committing the offsets that are maintained by itself.



The following is the pseudocode illustrates the high-level key implementation:

Code Block
titleMirrorSinkTask
    private boolean isTransactional = config.getTransactionalProducer();
    private boolean transactionInProgress = false;
	protected Map<TopicPartition, OffsetAndMetadata> offsetsMap = new HashMap<>();
    private Set<TopicPartition> taskTopicPartitions;
	private KafkaProducer<byte[], byte[]> producer;
    private String connectConsumerGroup;

    @Override
	public void start(Map<String, String> props) {
		config = new MirrorTaskConfig(props);
	    taskTopicPartitions = config.taskTopicPartitions();
        isTransactional = config.transactionalProducer();
	    producer = initProducer(isTransactional);
        connectConsumerGroup = getSourceConsumerGroupId();
		if (isTransactional) {
			Map<TopicPartition, OffsetAndMetadata> initOffsetsOnTarget = listTargetConsumerGroupOffsets(connectConsumerGroup);
	
			// To rewind consumer to correct offset per <topic, partition> assigned to this task, there are more than one case:        loadContextOffsets();
        }
    }

    @Override
    public void open(Collection<TopicPartition>      // (1) offset exists on target, use target offset as the starting point of consumptionpartitions) {
		if (isTransactional) {
    	    loadContextOffsets();
		}
    }

    private void loadContextOffsets() {
		Map<TopicPartition, OffsetAndMetadata> initOffsetsOnTarget = listTargetConsumerGroupOffsets(connectConsumerGroup);
	
        Set<TopicPartition> assignments //= context.assignment(2);

 offset DOES NOT exist on target, presume a// newonly topic,keep sothe setoffsets offsetof tothe 0
partitions assigned to this task
        Map<TopicPartition, Long> taskOffsetscontextOffsets = taskTopicPartitionsassignments.stream().collect(Collectors.toMap(

        		        		x -> x, x -> initOffsetsOnTarget.containsKey(x) 
            		?
                    initOffsetsOnTarget.getfilter(x).offset() + 1
 -> currentOffsets.containsKey(x))
                       : 0L));
			context.offset(taskOffsets);
        }
    }

	protected KafkaProducer<byte[], byte[]> initProducer(boolean isTransactional) {
        Map<String, Object> producerConfig = config.targetProducerConfig();
        if (isTransactional) {.collect(Collectors.toMap(
        	String	  transactionId = getTransactionId();
        	log.info("use transactional producer with Id: {} ", transactionId);
            producerConfig.put(ProducerConfig.ACKS_CONFIG, "all");
            producerConfig.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true");
 x -> x, x    	producerConfig.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, transactionId);-> currentOffsets.get(x)));

        	producerConfigcontext.put(ProducerConfig.CLIENT_ID_CONFIG, transactionIdoffset(contextOffsets);
        }

	protected KafkaProducer<byte[], byte[]> initProducer(boolean isTransactional) {
        Map<String, Object> producerConfig return= MirrorUtilsconfig.newProducertargetProducerConfig(producerConfig);
	}

    /**
    if * Per some articles, to avoid ProducerFencedException, transaction id is suggested to set application name + hostname
     * Each MirrorSinkTask is also assigned with different set of <topic, partition>. To get unique transaction id,(isTransactional) {
        	String transactionId = getTransactionId();
        	log.info("use transactional producer with Id: {} ", transactionId);
            producerConfig.put(ProducerConfig.ACKS_CONFIG, "all");
     * one way is to append connector name, hostname and string of each <topic, partition> pair
     */
 producerConfig.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true");
        	producerConfig.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, transactionId);
      protected String getTransactionId() {
	producerConfig.put(ProducerConfig.CLIENT_ID_CONFIG, transactionId);
        	return}
 connectorName + "-" + getHostName() + "-" +return getTopicPartitionStrMirrorUtils.newProducer(producerConfig);
	}

    }/**
    
  * Per protected String getTopicPartitionStr() {
    	StringBuffer buf = new StringBuffer();
    	for (TopicPartition tp :  taskTopicPartitions) {
    		buf.append(tp.topic() + "_" + tp.partition() + "-");
    	}
    	return buf.toString().substring(0, buf.length() - 1);
    }

    @Override
	public void put(Collection<SinkRecord> recordssome articles, to avoid ProducerFencedException, transaction id is suggested to set application name + hostname
     * Each MirrorSinkTask is also assigned with different set of <topic, partition>. To get unique transaction id,
     * one way is to append connector name, hostname and string of each <topic, partition> pair
     */
    protected String getTransactionId() {
    	return getHostName() + "-" + getUniquePredictableStr();
    }

    @Override
	public void put(Collection<SinkRecord> records) {
        log.info("receive {} messages from consumer", records.size());
        if (records.size() == 0) {
        	return;
        }
        try {
            sendBatch(records, producer);
        } catch (RebalanceException e) {
            producer.close();
            producer = initProducer(isTransactional);  
        } catch (ResendRecordsException e) {
            abortTransaction(producer);
            //TODO: add limited retry
            sendBatch(e.getRemainingRecords(), producer);
        } catch (Throwable e) {
            log.error(getHostName() + " terminating on exception: {}", e);
            return;
        }
    }

    private void sendBatch(Collection<SinkRecord> records, KafkaProducer<byte[], byte[]> producer) {
        try {
            Map<TopicPartition, List<SinkRecord> remainingRecordsMap = new HashMap<>();
            offsetsMap.clear();
            beginTransaction(producer);
            SinkRecord record;
            for ((record = records.peek()) != null) {
                ProducerRecord<byte[], byte[]> producerRecord = convertToProducerRecord(record);
                offsetsMap.compute(new TopicPartition(record.topic(), record.kafkaPartition()),
                       (tp, curOffsetMetadata) ->
                               (curOffsetMetadata == null || record.kafkaOffset() > curOffsetMetadata.offset())
                                       ?
                                       new OffsetAndMetadata(record.kafkaOffset())
                                       : curOffsetMetadata);
                Future<RecordMetadata> future = producer.send(producerRecord, (recordMetadata, e) -> {
                    if (e != null) {
                        log.error("{} failed to send record to {}: ", MirrorSinkTask.this, producerRecord.topic(), e);
                        log.debug("{} Failed record: {}", MirrorSinkTask.this, producerRecord);
                        throw new KafkaException(e);
                    } else {
                        log.info("{} Wrote record successfully: topic {} partition {} offset {}", //log.trace
                        		MirrorSinkTask.this,
                                recordMetadata.topic(), recordMetadata.partition(),
                                recordMetadata.offset());
                        commitRecord(record, recordMetadata);
                    }
                });
                futures.add(future);
                records.poll();
            }

        } catch (KafkaException e) {
            // Any unsent messages are added to the remaining remainingRecordsMap for re-send
            for (SinkRecord record = records.poll(); record != null; record = records.poll()) {
            	addConsumerRecordToTopicPartitionRecordsMap(record, remainingRecordsMap);
        } finally {  //TODO: may add more exception handling case
            for (Future<RecordMetadata> future : futures) {
                try {
                    future.get();
                } catch (Exception e) {
                    SinkRecord record = futureMap.get(future);
                    // Any record failed to send, add to the remainingRecordsMap
                    addConsumerRecordToTopicPartitionRecordsMap(record, remainingRecordsMap);
                }
            }
         }

         if (isTransactional && remainingRecordsMap.size() == 0) {
             producer.sendOffsetsToTransaction(offsetsMap, consumerGroupId);
             commitTransaction(producer);
         }

        if (remainingRecordsMap.size() > 0) {
            // For transaction case, all records should be put into remainingRecords, as the whole transaction should be redone
            Collection<SinkRecord> recordsToReSend;
            if (isTransactional) {
                // transactional: retry all records, as the transaction will cancel all successful and failed records.
                recordsToReSend = records;
            } else {
                // non-transactional: only retry failed records, others were finished and sent.
                recordsToReSend = remainingRecordsMap;
            }
            throw new ResendRecordsException(recordsToReSend);
        }
    }

    // @Override
This commitRecord() follows the publicsame Map<TopicPartition,logics OffsetAndMetadata>as preCommit(Map<TopicPartitioncommitRecord() in MirrorSourceTask, OffsetAndMetadata>to offsets) {
    	// if transactional, return empty Map intentionally to disable the offset commit by commitOffsets() in WorkerSinkTask.java
public void commitRecord(SinkRecord record, RecordMetadata metadata) {
        try {
         // so that theif transactional producer is able to commit the consumer offsets to target cluster in a transaction at its pace(stopping) {
                return;
    	if (isTransactional) 
    		return new HashMap<TopicPartition, OffsetAndMetadata>(); }
    	else // otherwise, return offsetsMap to let commitOffsets() in "WorkerSinkTask.java" to commit offsets
if (!metadata.hasOffset()) {
     		return offsetsMap;	
    }

    // This commitRecord() follows the same logics as commitRecord() in MirrorSourceTask, to 
    public void commitRecord(SinkRecord record, RecordMetadata metadata) {
log.error("RecordMetadata has no offset -- can't sync offsets for {}.", record.topic());
               try {return;
            if (stopping) {
}
            TopicPartition topicPartition = new  returnTopicPartition(record.topic(), record.kafkaPartition());
            }
long latency = System.currentTimeMillis() - record.timestamp();
       if (!metadata.hasOffset()) {
     metrics.countRecord(topicPartition);
            metrics.replicationLatency(topicPartition, latency);
      log.error("RecordMetadata has no offset -- can't syncTopicPartition offsetssourceTopicPartition for= {}.", MirrorUtils.unwrapPartition(record.topicsourcePartition());
            long upstreamOffset   return= MirrorUtils.unwrapOffset(record.sourceOffset());
            }
long downstreamOffset           TopicPartition topicPartition = new TopicPartition(record.topic(), record.kafkaPartition()= metadata.offset();
            long latency = System.currentTimeMillis() - record.timestamp(maybeSyncOffsets(sourceTopicPartition, upstreamOffset, downstreamOffset);
        } catch (Throwable  metrics.countRecord(topicPartition);e) {
            metricslog.replicationLatency(topicPartitionwarn("Failure committing record.", latencye);
        }
    TopicPartition}

 sourceTopicPartition = MirrorUtils.unwrapPartition(record.sourcePartition());
       private void beginTransaction(KafkaProducer<byte[], byte[]> producer) {
      long upstreamOffset =if MirrorUtils.unwrapOffset(record.sourceOffset());(isTransactional) {
            long downstreamOffset = metadata.offsetproducer.beginTransaction();
            maybeSyncOffsets(sourceTopicPartition, upstreamOffset, downstreamOffset)transactionInProgress = true;
        }
 catch (Throwable e) {
   }
	
    private void initTransactions(KafkaProducer<byte[], byte[]> producer) {
        if (isTransactional) {
          log.warn("Failure committing record.", eproducer.initTransactions();
        }
    }
    
    private void beginTransactioncommitTransaction(KafkaProducer<byte[], byte[]> producer) {
        if (isTransactional) {
            producer.beginTransactioncommitTransaction();
            transactionInProgress = truefalse;
        }
    }
	    
    private void initTransactionsabortTransaction(KafkaProducer<byte[], byte[]> producer) {
        if (isTransactional && transactionInProgress) {
            producer.initTransactionsabortTransaction();
            transactionInProgress = false;
        }
    }

    public static class ResendRecordsException extends Exception {
    private  void commitTransaction(KafkaProducer<byte[], byte[]> producer) { private Collection<SinkRecord> remainingRecords;

        ifpublic ResendRecordsException(isTransactionalCollection<SinkRecord> remainingRecords) {
            producer.commitTransactionsuper(cause);
            transactionInProgressthis.remainingRecords = falseremainingRecords;
        }

    }
    
public Collection<SinkRecord>   private void abortTransaction(KafkaProducer<byte[], byte[]> producer) getRemainingRecords() {
        if (isTransactional && transactionInProgress) {
            producer.abortTransaction();
            transactionInProgress = falsereturn remainingRecords;
        }
    }

    public static class ResendRecordsException extends Exception {
        private Collection<SinkRecord> remainingRecords;

        public ResendRecordsException(Collection<SinkRecord> remainingRecords) {
            super(cause);
            this.remainingRecords = remainingRecords;
        }

        public Collection<SinkRecord> getRemainingRecords() {
            return remainingRecords;
        }
    }

MirrorSinkConnector

As SinkTask can only be created by SinkConnector, MirrorSinkConnector will be implemented and follow the most same logics as current MirrorSourceConnector. To minimize the duplicate code, a new class, e.g. "MirrorCommonConnector", may be proposed to host the common code as a separate code change merged before this KIP.

WorkerSinkTask

MirrorSinkConnector

As SinkTask can only be created by SinkConnector, MirrorSinkConnector will be implemented and follow the most same logics as current MirrorSourceConnector. To minimize the duplicate code, a new class, e.g. "MirrorCommonConnector", may be proposed to host the common code as a separate code change merged before Due to the fact that group offsets from target cluster is single-source-of-truth, each time when MirrorSInkTask restarts, it loads group offsets from target cluster and supply them to the consumer in WorkerSinkTask so that the consumer will rewind to the last committed offsets without duplicating or missing data. However in our test, we ran into 

Jira
serverASF JIRA
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyKAFKA-10370
 so resolving it is a prerequisite of this KIP.

Migration from MirrorSourceConnector to MirrorSinkConnector /w EOS

...