@@ -16,7 +16,6 @@ package memory
1616
1717import (
1818 "fmt"
19- "log"
2019 "sync"
2120 "sync/atomic"
2221 "time"
@@ -105,16 +104,13 @@ func (c *ConsumerGroup) eventHandler(name string, event *Event) {
105104 var err error
106105 err = c .reportOffset (data .Records )
107106 data .ErrChan <- err
108- log .Printf ("消费者%s上报offset成功" , name )
109107 case RejoinAckEvent :
110108 // consumer响应重平衡信号返回的数据,返回的是当前所有分区的偏移量
111109 records , _ := event .Data .([]PartitionRecord )
112110 // 不管上报成不成功
113111 _ = c .reportOffset (records )
114- log .Printf ("消费者%s成功接受到重平衡信号,并上报offset" , name )
115112 c .balanceCh <- struct {}{}
116113 case PartitionNotifyAckEvent :
117- log .Printf ("消费者%s 成功设置分区信息" , name )
118114 c .balanceCh <- struct {}{}
119115 }
120116}
@@ -128,12 +124,9 @@ func (c *ConsumerGroup) exitGroup(name string, closeCh chan struct{}) {
128124 time .Sleep (defaultSleepTime )
129125 continue
130126 }
131- log .Printf ("消费者 %s 准备退出消费组" , name )
132127 c .consumers .Delete (name )
133128 c .reBalance ()
134- log .Printf ("给消费者 %s 发送退出确认信号" , name )
135129 close (closeCh )
136- log .Printf ("消费者 %s 成功退出消费组" , name )
137130 if ! atomic .CompareAndSwapInt32 (& c .status , StatusBalancing , StatusStable ) {
138131 atomic .CompareAndSwapInt32 (& c .status , StatusStopping , StatusStop )
139132 }
@@ -167,7 +160,7 @@ func (c *ConsumerGroup) Close() {
167160}
168161
169162func (c * ConsumerGroup ) close () {
170- c .consumers .Range (func (key string , value * Consumer ) bool {
163+ c .consumers .Range (func (_ string , value * Consumer ) bool {
171164 ch := make (chan struct {})
172165 value .receiveCh <- & Event {
173166 Type : CloseEvent ,
@@ -180,39 +173,30 @@ func (c *ConsumerGroup) close() {
180173
181174// reBalance 单独使用该方法是并发不安全的
182175func (c * ConsumerGroup ) reBalance () {
183- log .Println ("开始重平衡" )
184176 // 通知每一个消费者进行偏移量的上报
185177 length := 0
186178 consumers := make ([]string , 0 , consumerCap )
187- log .Println ("开始给每个消费者,重平衡信号" )
188179 c .consumers .Range (func (key string , value * Consumer ) bool {
189- log .Printf ("开始通知消费者%s" , key )
190180 value .receiveCh <- & Event {
191181 Type : RejoinEvent ,
192182 }
193183 consumers = append (consumers , key )
194184 length ++
195- log .Printf ("通知消费者%s成功" , key )
196185 return true
197186 })
198187 number := 0
199- log .Println ("xxxxxxxxxx长度" , length )
200188 // 等待所有消费者都接收到信号,并上报自己offset
201189 for length > 0 {
202190 <- c .balanceCh
203191 number ++
204192 if number != length {
205- log .Println ("xxxxxxxxxx number" , number )
206193 continue
207194 }
208195 // 接收到所有信号
209- log .Println ("所有消费者已经接受到重平衡请求,并上报了消费进度" )
210196 consumerMap := c .consumerPartitionAssigner .AssignPartition (consumers , len (c .partitions ))
211197 // 通知所有消费者分配
212- log .Println ("开始分配分区" )
213198 for consumerName , partitions := range consumerMap {
214199 // 查找消费者所属的channel
215- log .Printf ("消费者 %s 消费 %v 分区" , consumerName , partitions )
216200 consumer , ok := c .consumers .Load (consumerName )
217201 if ok {
218202 // 往每个消费者的receive_channel发送partition的信息
@@ -232,7 +216,6 @@ func (c *ConsumerGroup) reBalance() {
232216
233217 }
234218 }
235- log .Println ("重平衡结束" )
236219 return
237220 }
238221}
@@ -250,7 +233,7 @@ func (c *ConsumerGroup) JoinGroup() (*Consumer, error) {
250233 }
251234
252235 var length int
253- c .consumers .Range (func (key string , value * Consumer ) bool {
236+ c .consumers .Range (func (_ string , _ * Consumer ) bool {
254237 length ++
255238 return true
256239 })
@@ -269,7 +252,6 @@ func (c *ConsumerGroup) JoinGroup() (*Consumer, error) {
269252 c .consumers .Store (name , consumer )
270253 go c .consumerEventsHandler (name , reportCh )
271254 go consumer .eventLoop ()
272- log .Printf ("新建消费者 %s" , name )
273255 // 重平衡分配分区
274256 c .reBalance ()
275257 atomic .CompareAndSwapInt32 (& c .status , StatusBalancing , StatusStable )
@@ -287,10 +269,3 @@ func (c *ConsumerGroup) consumerEventsHandler(name string, reportCh chan *Event)
287269 }
288270 }
289271}
290-
291- func min (i , j int ) int {
292- if i < j {
293- return i
294- }
295- return j
296- }
0 commit comments