gaoran10 commented on a change in pull request #14709:
URL: https://github.com/apache/pulsar/pull/14709#discussion_r828697952



##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java
##########
@@ -442,4 +450,47 @@ private void checkSnapshotCount(TopicName topicName, 
boolean hasSnapshot,
         reader.close();
     }
 
+
+    @Test
+    public void testTransactionBufferRecoverThrowBrokerMetadataException() 
throws Exception {
+        String topic = NAMESPACE1 + 
"/testTransactionBufferRecoverThrowBrokerMetadataException";
+        @Cleanup
+        Producer<byte[]> producer = pulsarClient
+                .newProducer()
+                .topic(topic)
+                .sendTimeout(0, TimeUnit.SECONDS)
+                .create();
+
+        Transaction txn = pulsarClient.newTransaction()
+                .withTransactionTimeout(5, TimeUnit.SECONDS)
+                .build().get();
+
+        producer.newMessage(txn).value("test".getBytes()).sendAsync();
+        producer.newMessage(txn).value("test".getBytes()).sendAsync();
+        txn.commit().get();
+
+        // take snapshot
+        PersistentTopic originalTopic = (PersistentTopic) 
getPulsarServiceList().get(0)
+                .getBrokerService().getTopic(TopicName.get(topic).toString(), 
false).get().get();
+        TransactionBufferSnapshotService transactionBufferSnapshotService =
+                mock(TransactionBufferSnapshotService.class);
+        SystemTopicClient.Reader<TransactionBufferSnapshot> reader = 
mock(SystemTopicClient.Reader.class);
+        // mock reader can't read snapshot fail
+        doThrow(new 
PulsarClientException.BrokerMetadataException("")).when(reader).hasMoreEvents();
+        
doReturn(CompletableFuture.completedFuture(reader)).when(transactionBufferSnapshotService).createReader(any());
+
+        Field field = 
PulsarService.class.getDeclaredField("transactionBufferSnapshotService");
+        field.setAccessible(true);
+        field.set(getPulsarServiceList().get(0), 
transactionBufferSnapshotService);
+
+        // recover again will throw BrokerMetadataException then close topic
+        new TopicTransactionBuffer(originalTopic);
+        Awaitility.await().untilAsserted(() -> {
+            // isFenced means closed
+            Field close = AbstractTopic.class.getDeclaredField("isFenced");
+            close.setAccessible(true);
+            assertTrue((boolean) close.get(originalTopic));
+        });

Review comment:
       Could we send a transaction message at last? It could make sure the 
topic could recover successfully.

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java
##########
@@ -179,6 +179,9 @@ public void handleTxnEntry(Entry entry) {
 
                     @Override
                     public void recoverExceptionally(Exception e) {
+                        if (e instanceof 
PulsarClientException.BrokerMetadataException) {

Review comment:
       Other exceptions don't need to close the topic, right?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to