@@ -6,20 +6,20 @@ module Kafka.Consumer.Callbacks
6
6
)
7
7
where
8
8
9
- import Control.Arrow ((&&&) )
10
- import Control.Concurrent ( threadDelay )
11
- import Control.Monad ( forM_ , void )
12
- import Data.Monoid ( (<>) )
13
- import qualified Data.Text as Text
14
- import Foreign.ForeignPtr ( newForeignPtr_ )
15
- import Foreign.Ptr ( nullPtr )
16
- import Kafka.Callbacks as X
17
- import Kafka.Consumer.Convert ( fromNativeTopicPartitionList' , fromNativeTopicPartitionList'' , toNativeTopicPartitionList )
18
- import Kafka.Consumer.Types (KafkaConsumer (.. ), RebalanceEvent (.. ), TopicPartition (.. ))
19
- import Kafka.Internal.RdKafka
20
- import Kafka.Internal.Setup ( HasKafka (.. ), HasKafkaConf (.. ), Kafka (.. ), KafkaConf ( .. ), getRdMsgQueue )
21
- import Kafka.Internal.Shared ( kafkaErrorToMaybe )
22
- import Kafka.Types ( KafkaError ( .. ), PartitionId ( .. ), TopicName ( .. ))
9
+ import Control.Arrow ((&&&) )
10
+ import Control.Monad ( forM_ , void )
11
+ import Data.Monoid ( (<>) )
12
+ import Foreign.ForeignPtr ( newForeignPtr_ )
13
+ import Foreign.Ptr ( nullPtr )
14
+ import Kafka.Callbacks as X
15
+ import Kafka.Consumer.Convert ( fromNativeTopicPartitionList' , fromNativeTopicPartitionList'' , toNativeTopicPartitionList )
16
+ import Kafka.Consumer.Types ( KafkaConsumer ( .. ), RebalanceEvent ( .. ), TopicPartition ( .. ))
17
+ import Kafka.Internal.RdKafka
18
+ import Kafka.Internal.Setup (HasKafka (.. ), HasKafkaConf (.. ), Kafka (.. ), KafkaConf ( .. ), getRdMsgQueue )
19
+ import Kafka.Internal.Shared ( kafkaErrorToMaybe )
20
+ import Kafka.Types ( KafkaError (.. ), PartitionId (.. ), TopicName (.. ))
21
+
22
+ import qualified Data.Text as Text
23
23
24
24
-- | Sets a callback that is called when rebalance is needed.
25
25
--
@@ -81,22 +81,24 @@ setRebalanceCallback f k e pls = do
81
81
case mbq of
82
82
Nothing -> pure ()
83
83
Just mq -> do
84
+ let (Kafka kptr) = getKafka k
85
+ f k (RebalanceBeforeAssign assignment)
86
+ -- Magnus Edenhill:
87
+ -- If you redirect after assign() it means some messages may be forwarded to the single consumer queue,
88
+ -- so either do it before assign() or do: assign(); pause(); redirect; resume()
89
+ void $ rdKafkaAssign kptr pls
90
+ void $ rdKafkaPausePartitions kptr pls
84
91
forM_ ps (\ tp -> redirectPartitionQueue (getKafka k) (tpTopicName tp) (tpPartition tp) mq)
85
- -- sleep for 1 second.
86
- -- it looks like without it there is not enough time for redirect to happen
87
- -- or something similarly strange. I don't understand it.
88
- -- If you know WTH is going on PLEASE let me know because the current "fix" is ugly
89
- -- and is completely unreasonable :(
90
- threadDelay 1000000
91
- f k (RebalanceBeforeAssign assignment)
92
- void $ assign' k pls -- pass as pointer to avoid possible serialisation issues
92
+ void $ rdKafkaResumePartitions kptr pls
93
+ -- void $ assign' k pls -- pass as pointer to avoid possible serialisation issues
93
94
f k (RebalanceAssign assignment)
94
95
KafkaResponseError RdKafkaRespErrRevokePartitions -> do
95
96
f k (RebalanceBeforeRevoke assignment)
96
97
void $ assign k []
97
98
f k (RebalanceRevoke assignment)
98
99
x -> error $ " Rebalance: UNKNOWN response: " <> show x
99
100
101
+
100
102
-- | Assigns specified partitions to a current consumer.
101
103
-- Assigning an empty list means unassigning from all partitions that are currently assigned.
102
104
assign :: KafkaConsumer -> [TopicPartition ] -> IO (Maybe KafkaError )
@@ -107,9 +109,13 @@ assign (KafkaConsumer (Kafka k) _) ps =
107
109
er = KafkaResponseError <$> (pl >>= rdKafkaAssign k)
108
110
in kafkaErrorToMaybe <$> er
109
111
110
- -- | Assigns specified partitions to a current consumer.
111
- -- Assigning an empty list means unassigning from all partitions that are currently assigned.
112
- assign' :: KafkaConsumer -> RdKafkaTopicPartitionListTPtr -> IO (Maybe KafkaError )
113
- assign' (KafkaConsumer (Kafka k) _) pls =
114
- (kafkaErrorToMaybe . KafkaResponseError ) <$> rdKafkaAssign k pls
112
+ -- -- | Assigns specified partitions to a current consumer.
113
+ -- -- Assigning an empty list means unassigning from all partitions that are currently assigned.
114
+ -- assign' :: KafkaConsumer -> RdKafkaTopicPartitionListTPtr -> IO (Maybe KafkaError)
115
+ -- assign' (KafkaConsumer (Kafka k) _) pls = do
116
+
117
+
118
+
119
+ -- where
120
+ -- asExcept f = ExceptT $ rdKafkaErrorToEither <$> f
115
121
0 commit comments