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