@Test public void testCancelUnsentAddPartitionsAndProduceOnAbort() throws InterruptedException { final long pid = 13131L; final short epoch = 1; doInitTransactions(pid, epoch); transactionManager.beginTransaction(); transactionManager.maybeAddPartitionToTransaction(tp0); Future<RecordMetadata> responseFuture = accumulator.append(tp0, time.milliseconds(), "key".getBytes(), "value".getBytes(), Record.EMPTY_HEADERS, null, MAX_BLOCK_TIMEOUT).future; assertFalse(responseFuture.isDone()); TransactionalRequestResult abortResult = transactionManager.beginAbort(); // note since no partitions were added to the transaction, no EndTxn will be sent sender.run(time.milliseconds()); // try to abort assertTrue(abortResult.isCompleted()); assertTrue(abortResult.isSuccessful()); assertTrue(transactionManager.isReady()); // make sure we are ready for a transaction now. try { responseFuture.get(); fail("Expected produce future to raise an exception"); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof KafkaException); } }
@Test public void testAbortResendsAddPartitionErrorIfRetried() throws InterruptedException { final long producerId = 13131L; final short producerEpoch = 1; doInitTransactions(producerId, producerEpoch); transactionManager.beginTransaction(); transactionManager.maybeAddPartitionToTransaction(tp0); prepareAddPartitionsToTxnResponse(Errors.UNKNOWN_TOPIC_OR_PARTITION, tp0, producerEpoch, producerId); Future<RecordMetadata> responseFuture = accumulator.append(tp0, time.milliseconds(), "key".getBytes(), "value".getBytes(), Record.EMPTY_HEADERS, null, MAX_BLOCK_TIMEOUT).future; sender.run(time.milliseconds()); // Send AddPartitions and let it fail assertFalse(responseFuture.isDone()); TransactionalRequestResult abortResult = transactionManager.beginAbort(); // we should resend the AddPartitions prepareAddPartitionsToTxnResponse(Errors.NONE, tp0, producerEpoch, producerId); prepareEndTxnResponse(Errors.NONE, TransactionResult.ABORT, producerId, producerEpoch); sender.run(time.milliseconds()); // Resend AddPartitions sender.run(time.milliseconds()); // Send EndTxn assertTrue(abortResult.isCompleted()); assertTrue(abortResult.isSuccessful()); assertTrue(transactionManager.isReady()); // make sure we are ready for a transaction now. try { responseFuture.get(); fail("Expected produce future to raise an exception"); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof KafkaException); } }
@Test public void shouldNotSendAbortTxnRequestWhenOnlyAddOffsetsRequestFailed() { final long pid = 13131L; final short epoch = 1; doInitTransactions(pid, epoch); transactionManager.beginTransaction(); Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>(); offsets.put(tp1, new OffsetAndMetadata(1)); final String consumerGroupId = "myconsumergroup"; transactionManager.sendOffsetsToTransaction(offsets, consumerGroupId); TransactionalRequestResult abortResult = transactionManager.beginAbort(); prepareAddOffsetsToTxnResponse(Errors.GROUP_AUTHORIZATION_FAILED, consumerGroupId, pid, epoch); sender.run(time.milliseconds()); // Send AddOffsetsToTxnRequest assertFalse(abortResult.isCompleted()); sender.run(time.milliseconds()); assertTrue(transactionManager.isReady()); assertTrue(abortResult.isCompleted()); assertTrue(abortResult.isSuccessful()); }
@Test public void testAllowAbortOnProduceFailure() throws InterruptedException { final long pid = 13131L; final short epoch = 1; doInitTransactions(pid, epoch); transactionManager.beginTransaction(); transactionManager.maybeAddPartitionToTransaction(tp0); Future<RecordMetadata> responseFuture = accumulator.append(tp0, time.milliseconds(), "key".getBytes(), "value".getBytes(), Record.EMPTY_HEADERS, null, MAX_BLOCK_TIMEOUT).future; assertFalse(responseFuture.isDone()); prepareAddPartitionsToTxnResponse(Errors.NONE, tp0, epoch, pid); prepareProduceResponse(Errors.OUT_OF_ORDER_SEQUENCE_NUMBER, pid, epoch); prepareEndTxnResponse(Errors.NONE, TransactionResult.ABORT, pid, epoch); sender.run(time.milliseconds()); // Send AddPartitionsRequest sender.run(time.milliseconds()); // Send Produce Request, returns OutOfOrderSequenceException. TransactionalRequestResult abortResult = transactionManager.beginAbort(); sender.run(time.milliseconds()); // try to abort assertTrue(abortResult.isCompleted()); assertTrue(abortResult.isSuccessful()); assertTrue(transactionManager.isReady()); // make sure we are ready for a transaction now. }
@Test public void testAbortResendsProduceRequestIfRetried() throws Exception { final long producerId = 13131L; final short producerEpoch = 1; doInitTransactions(producerId, producerEpoch); transactionManager.beginTransaction(); transactionManager.maybeAddPartitionToTransaction(tp0); prepareAddPartitionsToTxnResponse(Errors.NONE, tp0, producerEpoch, producerId); prepareProduceResponse(Errors.REQUEST_TIMED_OUT, producerId, producerEpoch); Future<RecordMetadata> responseFuture = accumulator.append(tp0, time.milliseconds(), "key".getBytes(), "value".getBytes(), Record.EMPTY_HEADERS, null, MAX_BLOCK_TIMEOUT).future; sender.run(time.milliseconds()); // Send AddPartitions sender.run(time.milliseconds()); // Send ProduceRequest and let it fail assertFalse(responseFuture.isDone()); TransactionalRequestResult abortResult = transactionManager.beginAbort(); // we should resend the ProduceRequest before aborting prepareProduceResponse(Errors.NONE, producerId, producerEpoch); prepareEndTxnResponse(Errors.NONE, TransactionResult.ABORT, producerId, producerEpoch); sender.run(time.milliseconds()); // Resend ProduceRequest sender.run(time.milliseconds()); // Send EndTxn assertTrue(abortResult.isCompleted()); assertTrue(abortResult.isSuccessful()); assertTrue(transactionManager.isReady()); // make sure we are ready for a transaction now. RecordMetadata recordMetadata = responseFuture.get(); assertEquals(tp0.topic(), recordMetadata.topic()); }
assertTrue(transactionManager.isReady()); // make sure we are ready for a transaction now.
@Test public void testAbortableErrorWhileAbortInProgress() throws InterruptedException { final long pid = 13131L; final short epoch = 1; doInitTransactions(pid, epoch); transactionManager.beginTransaction(); transactionManager.maybeAddPartitionToTransaction(tp0); Future<RecordMetadata> responseFuture = accumulator.append(tp0, time.milliseconds(), "key".getBytes(), "value".getBytes(), Record.EMPTY_HEADERS, null, MAX_BLOCK_TIMEOUT).future; assertFalse(responseFuture.isDone()); prepareAddPartitionsToTxnResponse(Errors.NONE, tp0, epoch, pid); sender.run(time.milliseconds()); // Send AddPartitionsRequest sender.run(time.milliseconds()); // Send Produce Request TransactionalRequestResult abortResult = transactionManager.beginAbort(); assertTrue(transactionManager.isAborting()); assertFalse(transactionManager.hasError()); sendProduceResponse(Errors.OUT_OF_ORDER_SEQUENCE_NUMBER, pid, epoch); prepareEndTxnResponse(Errors.NONE, TransactionResult.ABORT, pid, epoch); sender.run(time.milliseconds()); // receive the produce response // we do not transition to ABORTABLE_ERROR since we were already aborting assertTrue(transactionManager.isAborting()); assertFalse(transactionManager.hasError()); sender.run(time.milliseconds()); // handle the abort assertTrue(abortResult.isCompleted()); assertTrue(abortResult.isSuccessful()); assertTrue(transactionManager.isReady()); // make sure we are ready for a transaction now. }
sender.run(time.milliseconds()); assertFalse(transactionManager.hasInFlightTransactionalRequest()); assertTrue(transactionManager.isReady());
sender.run(time.milliseconds()); assertFalse(transactionManager.hasInFlightTransactionalRequest()); assertTrue(transactionManager.isReady());