@@ -28,9 +28,14 @@ import org.apache.kafka.common.requests.{RequestHeader, TransactionResult, Write |
28 | 28 | import org.apache.kafka.common.utils.MockTime |
29 | 29 | import org.apache.kafka.common.utils.internals.LogContext |
30 | 30 | import org.apache.kafka.common.metrics.Metrics |
31 | | -import org.apache.kafka.common.{Node, TopicPartition} |
| 31 | +import org.apache.kafka.common.{Node, TopicPartition, Uuid} |
| 32 | +import org.apache.kafka.common.metadata.{PartitionChangeRecord, PartitionRecord, RegisterBrokerRecord, TopicRecord} |
| 33 | +import org.apache.kafka.common.metadata.RegisterBrokerRecord.{BrokerEndpoint, BrokerEndpointCollection} |
| 34 | +import org.apache.kafka.common.network.ListenerName |
| 35 | +import org.apache.kafka.common.protocol.ApiMessage |
| 36 | +import org.apache.kafka.common.security.auth.SecurityProtocol |
32 | 37 | import org.apache.kafka.coordinator.transaction.{CoordinatorEpochAndTxnMetadata, TransactionMetadata, TransactionState} |
33 | | -import org.apache.kafka.metadata.MetadataCache |
| 38 | +import org.apache.kafka.metadata.{LeaderConstants, MetadataCache, MetadataCacheFixtures} |
34 | 39 | import org.apache.kafka.server.common.{MetadataVersion, TransactionVersion} |
35 | 40 | import org.apache.kafka.server.metrics.{KafkaMetricsGroup, KafkaYammerMetrics} |
36 | 41 | import org.apache.kafka.server.util.{InterBrokerSendThread, RequestAndCompletionHandler} |
@@ -401,6 +406,94 @@ class TransactionMarkerChannelManagerTest { |
401 | 406 | assertEquals(Map(broker1 -> expectedBroker1Request), secondDrainedRequests) |
402 | 407 | } |
403 | 408 |
|
| 409 | + @ParameterizedTest |
| 410 | + @EnumSource(classOf[TransactionResult]) |
| 411 | + def shouldRetryMarkersUntilLeaderEndpointIsAvailable(result: TransactionResult): Unit = { |
| 412 | + mockCache() |
| 413 | + txnMetadata2.state(if (result == TransactionResult.COMMIT) TransactionState.PREPARE_COMMIT else TransactionState.PREPARE_ABORT) |
| 414 | + val expectedTransition = txnMetadata2.prepareComplete(time.milliseconds()) |
| 415 | + val cache = MetadataCacheFixtures.createCache() |
| 416 | + val topicId = Uuid.randomUuid() |
| 417 | + val listenerName = ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT) |
| 418 | + MetadataCacheFixtures.updateCache(cache, util.List.of[ApiMessage]( |
| 419 | + new TopicRecord().setName(partition1.topic).setTopicId(topicId), |
| 420 | + new PartitionRecord() |
| 421 | + .setTopicId(topicId) |
| 422 | + .setPartitionId(partition1.partition) |
| 423 | + .setLeader(LeaderConstants.NO_LEADER) |
| 424 | + .setReplicas(util.List.of[Integer](broker1.id)) |
| 425 | + .setIsr(util.List.of[Integer]()) |
| 426 | + )) |
| 427 | + |
| 428 | + val manager = new TransactionMarkerChannelManager( |
| 429 | + KafkaConfig.fromProps(TestUtils.createBrokerConfig(1)), cache, networkClient, txnStateManager, time) |
| 430 | + try { |
| 431 | + manager.addTxnMarkersToSend(coordinatorEpoch, result, txnMetadata2, expectedTransition) |
| 432 | + |
| 433 | + // A known partition without a leader must retain its marker and remain pending. |
| 434 | + assertTrue(manager.generateRequests().isEmpty) |
| 435 | + assertEquals(1, manager.queueForUnknownBroker.totalNumMarkers) |
| 436 | + assertEquals(1, manager.numTxnsWithPendingMarkers) |
| 437 | + assertTrue(txnMetadata2.topicPartitions.contains(partition1)) |
| 438 | + verify(txnStateManager, times(0)).appendTransactionToLog(any(), any(), any(), any(), any(), any()) |
| 439 | + |
| 440 | + MetadataCacheFixtures.updateCache(cache, util.List.of[ApiMessage]( |
| 441 | + new PartitionChangeRecord() |
| 442 | + .setTopicId(topicId) |
| 443 | + .setPartitionId(partition1.partition) |
| 444 | + .setLeader(broker1.id) |
| 445 | + .setIsr(util.List.of[Integer](broker1.id)) |
| 446 | + )) |
| 447 | + |
| 448 | + // Knowing the leader ID is insufficient while its broker registration is missing. |
| 449 | + assertTrue(manager.generateRequests().isEmpty) |
| 450 | + assertEquals(1, manager.queueForUnknownBroker.totalNumMarkers) |
| 451 | + assertEquals(1, manager.numTxnsWithPendingMarkers) |
| 452 | + assertTrue(txnMetadata2.topicPartitions.contains(partition1)) |
| 453 | + verify(txnStateManager, times(0)).appendTransactionToLog(any(), any(), any(), any(), any(), any()) |
| 454 | + |
| 455 | + MetadataCacheFixtures.updateCache(cache, util.List.of[ApiMessage]( |
| 456 | + new RegisterBrokerRecord() |
| 457 | + .setBrokerId(broker1.id) |
| 458 | + .setRack("rack1") |
| 459 | + .setFenced(false) |
| 460 | + .setEndPoints(new BrokerEndpointCollection(util.List.of( |
| 461 | + new BrokerEndpoint() |
| 462 | + .setHost(broker1.host) |
| 463 | + .setPort(broker1.port) |
| 464 | + .setSecurityProtocol(SecurityProtocol.PLAINTEXT.id) |
| 465 | + .setName(listenerName.value) |
| 466 | + ))) |
| 467 | + )) |
| 468 | + |
| 469 | + val requests = manager.generateRequests().asScala.toList |
| 470 | + assertEquals(1, requests.size) |
| 471 | + assertEquals(new Node(broker1.id, broker1.host, broker1.port, "rack1"), requests.head.destination) |
| 472 | + val request = requests.head.request.asInstanceOf[WriteTxnMarkersRequest.Builder].build() |
| 473 | + assertEquals(util.List.of(new WriteTxnMarkersRequest.TxnMarkerEntry( |
| 474 | + producerId2, producerEpoch, coordinatorEpoch, result, util.List.of(partition1), TransactionVersion.TV_2.featureLevel() |
| 475 | + )), request.markers()) |
| 476 | + assertEquals(0, manager.queueForUnknownBroker.totalNumMarkers) |
| 477 | + assertEquals(1, manager.numTxnsWithPendingMarkers) |
| 478 | + verify(txnStateManager, times(0)).appendTransactionToLog(any(), any(), any(), any(), any(), any()) |
| 479 | + |
| 480 | + val response = new WriteTxnMarkersResponse( |
| 481 | + util.Map.of(producerId2: java.lang.Long, util.Map.of(partition1, Errors.NONE))) |
| 482 | + requests.head.handler.onComplete(new ClientResponse( |
| 483 | + new RequestHeader(ApiKeys.WRITE_TXN_MARKERS, 0, "client", 1), null, null, |
| 484 | + time.milliseconds(), time.milliseconds(), false, null, null, response)) |
| 485 | + |
| 486 | + assertTrue(txnMetadata2.topicPartitions.isEmpty) |
| 487 | + assertEquals(0, manager.numTxnsWithPendingMarkers) |
| 488 | + verify(txnStateManager).appendTransactionToLog( |
| 489 | + ArgumentMatchers.eq(transactionalId2), ArgumentMatchers.eq(coordinatorEpoch), |
| 490 | + ArgumentMatchers.eq(expectedTransition), any(), any(), any()) |
| 491 | + assertTrue(manager.generateRequests().isEmpty) |
| 492 | + } finally { |
| 493 | + manager.shutdown() |
| 494 | + } |
| 495 | + } |
| 496 | + |
404 | 497 | @Test |
405 | 498 | def shouldRemoveMarkersForTxnPartitionWhenPartitionEmigrated(): Unit = { |
406 | 499 | mockCache() |
|