@@ -1344,6 +1344,121 @@ public void testPartitionedTopicDistribution() throws Exception { |
1344 | 1344 | } |
1345 | 1345 | } |
1346 | 1346 |
|
| 1347 | + // With pooled messages, the client owns a message until it hands it to the application. A message that the |
| 1348 | + // partition's consumer hands over after the multi-topics consumer has closed never reaches the application, so |
| 1349 | + // the client must release it. |
| 1350 | + @Test(timeOut = testTimeout) |
| 1351 | + public void testCloseReleasesPooledMessagesHandedOverAfterTheClose() throws Exception { |
| 1352 | + final String topic = TopicName.get(newTopicName()).toString(); |
| 1353 | + admin.topics().createPartitionedTopic(topic, 1); |
| 1354 | + final String partition = TopicName.get(topic).getPartition(0).toString(); |
| 1355 | + // The payload of each message the partition's consumer handles. The test takes a reference of its own to it: |
| 1356 | + // a buffer that is still referenced cannot be reused, so its reference count stays readable. |
| 1357 | + List<ByteBuf> payloads = new CopyOnWriteArrayList<>(); |
| 1358 | + Consumer<Integer> consumer = pulsarClient.newConsumer(Schema.INT32) |
| 1359 | + .topic(topic) |
| 1360 | + .subscriptionName("sub") |
| 1361 | + .poolMessages(true) |
| 1362 | + .intercept(new ConsumerInterceptor<Integer>() { |
| 1363 | + @Override |
| 1364 | + public Message<Integer> onArrival(Consumer<Integer> consumer, Message<Integer> message) { |
| 1365 | + payloads.add(((MessageImpl<Integer>) message).getDataBuffer().retain()); |
| 1366 | + return message; |
| 1367 | + } |
| 1368 | + |
| 1369 | + @Override |
| 1370 | + public Message<Integer> beforeConsume(Consumer<Integer> consumer, Message<Integer> message) { |
| 1371 | + return message; |
| 1372 | + } |
| 1373 | + |
| 1374 | + @Override |
| 1375 | + public void onAcknowledge(Consumer<Integer> consumer, MessageId messageId, Throwable exception) { |
| 1376 | + } |
| 1377 | + |
| 1378 | + @Override |
| 1379 | + public void onAcknowledgeCumulative(Consumer<Integer> consumer, MessageId messageId, |
| 1380 | + Throwable exception) { |
| 1381 | + } |
| 1382 | + |
| 1383 | + @Override |
| 1384 | + public void onNegativeAcksSend(Consumer<Integer> consumer, Set<MessageId> messageIds) { |
| 1385 | + } |
| 1386 | + |
| 1387 | + @Override |
| 1388 | + public void onAckTimeoutSend(Consumer<Integer> consumer, Set<MessageId> messageIds) { |
| 1389 | + } |
| 1390 | + |
| 1391 | + @Override |
| 1392 | + public void close() { |
| 1393 | + } |
| 1394 | + }) |
| 1395 | + .subscribe(); |
| 1396 | + ConsumerImpl<Integer> partitionConsumer = |
| 1397 | + ((MultiTopicsConsumerImpl<Integer>) consumer).getConsumers().get(0); |
| 1398 | + CountDownLatch closeConfirmed = new CountDownLatch(1); |
| 1399 | + CompletableFuture<Void> closeFuture = null; |
| 1400 | + try { |
| 1401 | + ClientCnx cnx = partitionConsumer.getClientCnx(); |
| 1402 | + Awaitility.await().untilAsserted(() -> assertThat(partitionConsumer.hasNextPendingReceive()) |
| 1403 | + .as("the multi-topics consumer waits in a receive of the partition's consumer") |
| 1404 | + .isTrue()); |
| 1405 | + |
| 1406 | + // Hold the partition consumer's tasks back until it has handled the confirmation of its close, so that |
| 1407 | + // it hands the message over after the close. |
| 1408 | + partitionConsumer.internalPinnedExecutor.execute(() -> { |
| 1409 | + try { |
| 1410 | + closeConfirmed.await(testTimeout, TimeUnit.MILLISECONDS); |
| 1411 | + } catch (InterruptedException e) { |
| 1412 | + Thread.currentThread().interrupt(); |
| 1413 | + } |
| 1414 | + }); |
| 1415 | + // A client of its own, whose executors the test does not hold |
| 1416 | + @Cleanup |
| 1417 | + PulsarClient producerClient = newPulsarClient(); |
| 1418 | + @Cleanup |
| 1419 | + Producer<Integer> producer = producerClient.newProducer(Schema.INT32) |
| 1420 | + .topic(topic) |
| 1421 | + .enableBatching(false) |
| 1422 | + .compressionType(CompressionType.LZ4) |
| 1423 | + .compressionMinMsgBodySize(0) |
| 1424 | + .create(); |
| 1425 | + producer.send(0); |
| 1426 | + // The broker sent the message before the close, so the consumer receives it before the close's |
| 1427 | + // confirmation |
| 1428 | + Awaitility.await().untilAsserted(() -> assertThat( |
| 1429 | + admin.topics().getStats(partition).getSubscriptions().get("sub").getMsgOutCounter()) |
| 1430 | + .as("messages the broker dispatched to the subscription") |
| 1431 | + .isEqualTo(1)); |
| 1432 | + |
| 1433 | + closeFuture = consumer.closeAsync(); |
| 1434 | + Awaitility.await().untilAsserted(() -> assertThat(partitionConsumer.getState()) |
| 1435 | + .as("state of the partition's consumer") |
| 1436 | + .isEqualTo(HandlerState.State.Closed)); |
| 1437 | + // The connection's thread handles the confirmation. It has queued the last task of the close on the |
| 1438 | + // partition consumer's executor once it gets to this task. |
| 1439 | + cnx.ctx().channel().eventLoop().submit(() -> { }).get(10, TimeUnit.SECONDS); |
| 1440 | + closeConfirmed.countDown(); |
| 1441 | + closeFuture.get(10, TimeUnit.SECONDS); |
| 1442 | + |
| 1443 | + Awaitility.await().untilAsserted(() -> assertThat(payloads) |
| 1444 | + .as("payload reference count of the pooled message handed over after the close: only the test's" |
| 1445 | + + " own reference must be left") |
| 1446 | + .extracting(ByteBuf::refCnt) |
| 1447 | + .containsExactly(1)); |
| 1448 | + } finally { |
| 1449 | + // Let the partition consumer's executor go when the test fails before the close: the close needs it |
| 1450 | + closeConfirmed.countDown(); |
| 1451 | + try { |
| 1452 | + // The consumer has closed and its executor has run the tasks it had, so that no message arrives |
| 1453 | + // after the test has given its references back. Closing again would not wait for the first close. |
| 1454 | + (closeFuture != null ? closeFuture : consumer.closeAsync()).get(10, TimeUnit.SECONDS); |
| 1455 | + partitionConsumer.internalPinnedExecutor.submit(() -> { }).get(10, TimeUnit.SECONDS); |
| 1456 | + } finally { |
| 1457 | + payloads.forEach(ByteBuf::release); |
| 1458 | + } |
| 1459 | + } |
| 1460 | + } |
| 1461 | + |
1347 | 1462 | // A seek of a partition completes when the broker confirms it. The messages that arrived before the confirmation |
1348 | 1463 | // may still be on their way through the partition's consumer then, and must not reach the application. |
1349 | 1464 | @Test(timeOut = testTimeout) |
|