Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,43 @@ public KafkaProducer(Properties properties, Serializer<K> keySerializer, Seriali
}
}

// visible for testing
KafkaProducer(ProducerConfig config,
LogContext logContext,
Metrics metrics,
Serializer<K> keySerializer,
Serializer<V> valueSerializer,
ProducerMetadata metadata,
RecordAccumulator accumulator,
TransactionManager transactionManager,
Sender sender,
ProducerInterceptors<K, V> interceptors,
Partitioner partitioner,
Time time,
KafkaThread ioThread) {
this.producerConfig = config;
this.time = time;
this.clientId = config.getString(ProducerConfig.CLIENT_ID_CONFIG);
this.log = logContext.logger(KafkaProducer.class);
this.metrics = metrics;
this.producerMetrics = new KafkaProducerMetrics(metrics);
this.partitioner = partitioner;
this.keySerializer = keySerializer;
this.valueSerializer = valueSerializer;
this.interceptors = interceptors;
this.maxRequestSize = config.getInt(ProducerConfig.MAX_REQUEST_SIZE_CONFIG);
this.totalMemorySize = config.getLong(ProducerConfig.BUFFER_MEMORY_CONFIG);
this.compressionType = CompressionType.forName(config.getString(ProducerConfig.COMPRESSION_TYPE_CONFIG));
this.maxBlockTimeMs = config.getLong(ProducerConfig.MAX_BLOCK_MS_CONFIG);
this.apiVersions = new ApiVersions();
this.transactionManager = transactionManager;
this.accumulator = accumulator;
this.errors = this.metrics.sensor("errors");
this.metadata = metadata;
this.sender = sender;
this.ioThread = ioThread;
}

// visible for testing
Sender newSender(LogContext logContext, KafkaClient kafkaClient, ProducerMetadata metadata) {
int maxInflightRequests = producerConfig.getInt(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION);
Expand Down Expand Up @@ -939,10 +976,6 @@ private Future<RecordMetadata> doSend(ProducerRecord<K, V> record, Callback call
// producer callback will make sure to call both 'callback' and interceptor callback
Callback interceptCallback = new InterceptorCallback<>(callback, this.interceptors, tp);

if (transactionManager != null) {
transactionManager.maybeAddPartition(tp);
}

RecordAccumulator.RecordAppendResult result = accumulator.append(tp, timestamp, serializedKey,
serializedValue, headers, interceptCallback, remainingWaitMs, true, nowMs);

Expand All @@ -961,6 +994,15 @@ private Future<RecordMetadata> doSend(ProducerRecord<K, V> record, Callback call
serializedValue, headers, interceptCallback, remainingWaitMs, false, nowMs);
}

// Add the partition to the transaction (if in progress) after it has been successfully
// appended to the accumulator. We cannot do it before because the initially selected
// partition may be changed when the batch is closed (as indicated by `abortForNewBatch`).
// Note that the `Sender` will refuse to dequeue batches from the accumulator until they
// have been added to the transaction.
if (transactionManager != null) {
transactionManager.maybeAddPartition(tp);
}

if (result.batchIsFull || result.newBatchCreated) {
log.trace("Waking up the sender since topic {} partition {} is either full or getting a new batch", record.topic(), partition);
this.sender.wakeup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
* The accumulator uses a bounded amount of memory and append calls will block when that memory is exhausted, unless
* this behavior is explicitly disabled.
*/
public final class RecordAccumulator {
public class RecordAccumulator {

private final Logger log;
private volatile boolean closed;
Expand Down
Loading