[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-17877: Only call once maybeSendResponseCallback for each marker #17619

Merged
merged 3 commits into from
Nov 5, 2024
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
38 changes: 21 additions & 17 deletions core/src/main/scala/kafka/server/KafkaApis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2431,8 +2431,12 @@ class KafkaApis(val requestChannel: RequestChannel,
}

val markerResults = new ConcurrentHashMap[TopicPartition, Errors]()
def maybeComplete(): Unit = {
if (partitionsWithCompatibleMessageFormat.size == markerResults.size) {
val numPartitions = new AtomicInteger(partitionsWithCompatibleMessageFormat.size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To understand the first case:

we call replicaManager#appendRecords (with empty records) while simultaneously appending via the new coordinator. This incorrectly decrements the number of appends counter and thus sends the premature response.

The similarity in both cases is that we prematurely decrement this numAppends counter.

Is my understanding correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. It is common if the new coordinator returns an error for the partition(NOT_LEADER_OR_FOLLOWER) fast enough to get ahead of the callback of the replicaManager#appendRecords

def addResultAndMaybeComplete(partition: TopicPartition, error: Errors): Unit = {
markerResults.put(partition, error)
// We should only call maybeSendResponseCallback once per marker. Otherwise, it causes sending the response
// prematurely.
if (numPartitions.decrementAndGet() == 0) {
maybeSendResponseCallback(producerId, marker.transactionResult, markerResults)
}
}
Expand Down Expand Up @@ -2462,8 +2466,7 @@ class KafkaApis(val requestChannel: RequestChannel,
error
}
}
markerResults.put(partition, error)
maybeComplete()
addResultAndMaybeComplete(partition, error)
}
} else {
// Otherwise, the regular appendRecords path is used for all the non __consumer_offsets
Expand All @@ -2476,20 +2479,21 @@ class KafkaApis(val requestChannel: RequestChannel,
}
}

replicaManager.appendRecords(
timeout = config.requestTimeoutMs.toLong,
requiredAcks = -1,
internalTopicsAllowed = true,
origin = AppendOrigin.COORDINATOR,
entriesPerPartition = controlRecords,
requestLocal = requestLocal,
responseCallback = errors => {
errors.foreachEntry { (tp, partitionResponse) =>
markerResults.put(tp, partitionResponse.error)
if (controlRecords.nonEmpty) {
replicaManager.appendRecords(
timeout = config.requestTimeoutMs.toLong,
requiredAcks = -1,
internalTopicsAllowed = true,
origin = AppendOrigin.COORDINATOR,
entriesPerPartition = controlRecords,
requestLocal = requestLocal,
responseCallback = errors => {
errors.foreachEntry { (tp, partitionResponse) =>
addResultAndMaybeComplete(tp, partitionResponse.error)
}
}
maybeComplete()
}
)
)
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions core/src/test/scala/unit/kafka/server/KafkaApisTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3068,6 +3068,44 @@ class KafkaApisTest extends Logging {
assertEquals(expectedErrors, markersResponse.errorsByProducerId.get(1L))
}

@Test
def testWriteTxnMarkersShouldAllBeIncludedInTheResponse(): Unit = {
// This test verifies the response will not be sent prematurely because of calling replicaManager append
// with no records.
val topicPartition = new TopicPartition(Topic.GROUP_METADATA_TOPIC_NAME, 0)
val writeTxnMarkersRequest = new WriteTxnMarkersRequest.Builder(ApiKeys.WRITE_TXN_MARKERS.latestVersion(),
asList(
new TxnMarkerEntry(1, 1.toShort, 0, TransactionResult.COMMIT, asList(topicPartition)),
new TxnMarkerEntry(2, 1.toShort, 0, TransactionResult.COMMIT, asList(topicPartition)),
)).build()
val request = buildRequest(writeTxnMarkersRequest)
val capturedResponse: ArgumentCaptor[WriteTxnMarkersResponse] = ArgumentCaptor.forClass(classOf[WriteTxnMarkersResponse])

when(replicaManager.getMagic(any()))
.thenReturn(Some(RecordBatch.MAGIC_VALUE_V2))
when(groupCoordinator.isNewGroupCoordinator)
.thenReturn(true)
when(groupCoordinator.completeTransaction(
ArgumentMatchers.eq(topicPartition),
any(),
ArgumentMatchers.eq(1.toShort),
ArgumentMatchers.eq(0),
ArgumentMatchers.eq(TransactionResult.COMMIT),
any()
)).thenReturn(CompletableFuture.completedFuture[Void](null))

kafkaApis = createKafkaApis()
kafkaApis.handleWriteTxnMarkersRequest(request, RequestLocal.withThreadConfinedCaching)

verify(requestChannel).sendResponse(
ArgumentMatchers.eq(request),
capturedResponse.capture(),
ArgumentMatchers.eq(None)
)
val markersResponse = capturedResponse.getValue
assertEquals(2, markersResponse.errorsByProducerId.size())
}

@Test
def shouldRespondWithUnsupportedMessageFormatForBadPartitionAndNoErrorsForGoodPartition(): Unit = {
val tp1 = new TopicPartition("t", 0)
Expand Down