forked from didapinchegit/go_rocket_mq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebalance.go
172 lines (146 loc) · 4.3 KB
/
rebalance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package rocketmq
import (
"errors"
"log"
"sort"
"sync"
)
type SubscriptionData struct {
Topic string
SubString string
ClassFilterMode bool
TagsSet []string
CodeSet []string
SubVersion int64
}
type Rebalance struct {
groupName string
messageModel string
topicSubscribeInfoTable map[string][]*MessageQueue
subscriptionInner map[string]*SubscriptionData
mqClient *MqClient
allocateMessageQueueStrategy AllocateMessageQueueStrategy
consumer *DefaultConsumer
processQueueTable map[MessageQueue]int32
mutex sync.Mutex
}
func NewRebalance() *Rebalance {
return &Rebalance{
topicSubscribeInfoTable: make(map[string][]*MessageQueue),
subscriptionInner: make(map[string]*SubscriptionData),
allocateMessageQueueStrategy: new(AllocateMessageQueueAveragely),
messageModel: "CLUSTERING",
processQueueTable: make(map[MessageQueue]int32),
}
}
func (self *Rebalance) doRebalance() {
self.mutex.Lock()
defer self.mutex.Unlock()
for topic, _ := range self.subscriptionInner {
self.rebalanceByTopic(topic)
}
}
type ConsumerIdSorter []string
func (self ConsumerIdSorter) Len() int { return len(self) }
func (self ConsumerIdSorter) Swap(i, j int) { self[i], self[j] = self[j], self[i] }
func (self ConsumerIdSorter) Less(i, j int) bool {
if self[i] < self[j] {
return true
}
return false
}
type AllocateMessageQueueStrategy interface {
allocate(consumerGroup string, currentCID string, mqAll []*MessageQueue, cidAll []string) ([]*MessageQueue, error)
}
type AllocateMessageQueueAveragely struct{}
func (self *AllocateMessageQueueAveragely) allocate(consumerGroup string, currentCID string, mqAll []*MessageQueue, cidAll []string) ([]*MessageQueue, error) {
if currentCID == "" {
return nil, errors.New("currentCID is empty")
}
if mqAll == nil || len(mqAll) == 0 {
return nil, errors.New("mqAll is nil or mqAll empty")
}
if cidAll == nil || len(cidAll) == 0 {
return nil, errors.New("cidAll is nil or cidAll empty")
}
result := make([]*MessageQueue, 0)
for i, cid := range cidAll {
if cid == currentCID {
mqLen := len(mqAll)
cidLen := len(cidAll)
mod := mqLen % cidLen
var averageSize int
if mqLen < cidLen {
averageSize = 1
} else {
if mod > 0 && i < mod {
averageSize = mqLen/cidLen + 1
} else {
averageSize = mqLen / cidLen
}
}
var startIndex int
if mod > 0 && i < mod {
startIndex = i * averageSize
} else {
startIndex = i*averageSize + mod
}
var min int
if averageSize > mqLen-startIndex {
min = mqLen - startIndex
} else {
min = averageSize
}
for j := 0; j < min; j++ {
result = append(result, mqAll[(startIndex+j)%mqLen])
}
return result, nil
}
}
return nil, errors.New("cant't find currentCID")
}
func (self *Rebalance) rebalanceByTopic(topic string) error {
cidAll, err := self.mqClient.findConsumerIdList(topic, self.groupName)
if err != nil {
log.Print(err)
return err
}
mqs, ok := self.topicSubscribeInfoTable[topic]
if ok && len(mqs) > 0 && len(cidAll) > 0 {
var messageQueues MessageQueues = mqs
var consumerIdSorter ConsumerIdSorter = cidAll
sort.Sort(messageQueues)
sort.Sort(consumerIdSorter)
}
allocateResult, err := self.allocateMessageQueueStrategy.allocate(self.groupName, self.mqClient.clientId, mqs, cidAll)
if err != nil {
log.Print(err)
return err
}
log.Printf("rebalance topic[%s]", topic)
self.updateProcessQueueTableInRebalance(topic, allocateResult)
return nil
}
func (self *Rebalance) updateProcessQueueTableInRebalance(topic string, mqSet []*MessageQueue) {
for _, mq := range mqSet {
_, ok := self.processQueueTable[*mq]
if !ok {
pullRequest := new(PullRequest)
pullRequest.consumerGroup = self.groupName
pullRequest.messageQueue = mq
pullRequest.nextOffset = self.computePullFromWhere(mq)
self.mqClient.pullMessageService.pullRequestQueue <- pullRequest
self.processQueueTable[*mq] = 1
}
}
}
func (self *Rebalance) computePullFromWhere(mq *MessageQueue) int64 {
var result int64 = -1
lastOffset := self.consumer.offsetStore.readOffset(mq, READ_FROM_STORE)
if lastOffset >= 0 {
result = lastOffset
} else {
result = 0
}
return result
}