|
84 | 84 | import java.nio.ByteBuffer; |
85 | 85 | import java.util.Collections; |
86 | 86 | import java.util.HashMap; |
| 87 | +import java.util.HashSet; |
87 | 88 | import java.util.List; |
88 | 89 | import java.util.Map; |
89 | 90 | import java.util.Objects; |
@@ -160,7 +161,12 @@ public void handleExit() { |
160 | 161 | LOGGER.info( |
161 | 162 | "Subscription: remove consumer config {} when handling exit", |
162 | 163 | consumerConfigThreadLocal.get()); |
| 164 | + // we should not close the consumer here because it might reuse the previous consumption |
| 165 | + // progress to continue consuming |
163 | 166 | // closeConsumer(consumerConfig); |
| 167 | + // when handling exit, unsubscribe from topics that have already been completed as much as |
| 168 | + // possible to release some resources (such as the underlying pipe) in a timely manner |
| 169 | + unsubscribeCompleteTopics(consumerConfig); |
164 | 170 | consumerConfigThreadLocal.remove(); |
165 | 171 | } |
166 | 172 | } |
@@ -309,6 +315,8 @@ private TPipeSubscribeResp handlePipeSubscribeHeartbeatInternal( |
309 | 315 | // fetch topics should be unsubscribed |
310 | 316 | final List<String> topicNamesToUnsubscribe = |
311 | 317 | SubscriptionAgent.broker().fetchTopicNamesToUnsubscribe(consumerConfig, topics.keySet()); |
| 318 | + // here we did not immediately unsubscribe from topics in order to allow the client to perceive |
| 319 | + // completed topics |
312 | 320 |
|
313 | 321 | return PipeSubscribeHeartbeatResp.toTPipeSubscribeResp( |
314 | 322 | RpcUtils.SUCCESS_STATUS, topics, endPoints, topicNamesToUnsubscribe); |
@@ -682,6 +690,26 @@ private void closeConsumer(final ConsumerConfig consumerConfig) { |
682 | 690 | LOGGER.info("Subscription: consumer {} close successfully", consumerConfig); |
683 | 691 | } |
684 | 692 |
|
| 693 | + private void unsubscribeCompleteTopics(final ConsumerConfig consumerConfig) { |
| 694 | + // fetch subscribed topics |
| 695 | + final Map<String, TopicConfig> topics = |
| 696 | + SubscriptionAgent.topic() |
| 697 | + .getTopicConfigs( |
| 698 | + SubscriptionAgent.consumer() |
| 699 | + .getTopicNamesSubscribedByConsumer( |
| 700 | + consumerConfig.getConsumerGroupId(), consumerConfig.getConsumerId())); |
| 701 | + |
| 702 | + // fetch topics should be unsubscribed |
| 703 | + final List<String> topicNamesToUnsubscribe = |
| 704 | + SubscriptionAgent.broker().fetchTopicNamesToUnsubscribe(consumerConfig, topics.keySet()); |
| 705 | + |
| 706 | + unsubscribe(consumerConfig, new HashSet<>(topicNamesToUnsubscribe)); |
| 707 | + LOGGER.info( |
| 708 | + "Subscription: consumer {} unsubscribe {} (completed topics) successfully", |
| 709 | + consumerConfig, |
| 710 | + topicNamesToUnsubscribe); |
| 711 | + } |
| 712 | + |
685 | 713 | //////////////////////////// consumer operations //////////////////////////// |
686 | 714 |
|
687 | 715 | private void createConsumer(final ConsumerConfig consumerConfig) throws SubscriptionException { |
|
0 commit comments