forked from sevennt/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebalance.go
197 lines (171 loc) · 5.12 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package rocketmq
import (
"errors"
"sort"
"sync"
"fmt"
"strings"
)
type SubscriptionData struct {
Topic string `json:"topic"`
SubString string `json:"subString"`
ClassFilterMode bool `json:"classFilterMode"`
TagsSet []string `json:"tagsSet"`
CodeSet []string `json:"codeSet"`
SubVersion int64 `json:"subVersion"`
}
type Rebalance struct {
groupName string
messageModel string
topicSubscribeInfoTable map[string][]*MessageQueue
topicSubscribeInfoTableLock sync.RWMutex
subscriptionInner map[string]*SubscriptionData
subscriptionInnerLock sync.RWMutex
mqClient *MqClient
allocateMessageQueueStrategy AllocateMessageQueueStrategy
consumer *DefaultConsumer
producer *DefaultProducer
processQueueTable map[MessageQueue]int32
processQueueTableLock sync.RWMutex
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 (r *Rebalance) doRebalance() {
r.mutex.Lock()
defer r.mutex.Unlock()
for topic, _ := range r.subscriptionInner {
r.rebalanceByTopic(topic)
}
}
type ConsumerIdSorter []string
func (r ConsumerIdSorter) Len() int { return len(r) }
func (r ConsumerIdSorter) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ConsumerIdSorter) Less(i, j int) bool {
if r[i] < r[j] {
return true
}
return false
}
type AllocateMessageQueueStrategy interface {
allocate(consumerGroup string, currentCID string, mqAll []*MessageQueue, cidAll []string) ([]*MessageQueue, error)
}
type AllocateMessageQueueAveragely struct{}
func (r *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)
tmpStr := make([]string,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])
tmpStr = append(tmpStr,fmt.Sprintf("%+v ",mqAll[(startIndex+j)%mqLen]))
}
Info.Printf("Rebalance allocate Result: %s",strings.Join(tmpStr,","))
return result, nil
}
}
return nil, nil
}
func (r *Rebalance) rebalanceByTopic(topic string) error {
cidAll, err := r.mqClient.findConsumerIdList(topic, r.groupName)
if err != nil {
Error.Println(err)
return err
}
r.topicSubscribeInfoTableLock.RLock()
mqs, ok := r.topicSubscribeInfoTable[topic]
r.topicSubscribeInfoTableLock.RUnlock()
if ok && len(mqs) > 0 && len(cidAll) > 0 {
var messageQueues MessageQueues = mqs
var consumerIdSorter ConsumerIdSorter = cidAll
sort.Sort(messageQueues)
sort.Sort(consumerIdSorter)
}
allocateResult, err := r.allocateMessageQueueStrategy.allocate(r.groupName, r.mqClient.clientId, mqs, cidAll)
r.updateProcessQueueTableInRebalance(topic, allocateResult)
return nil
}
func (r *Rebalance) updateProcessQueueTableInRebalance(topic string, mqSet []*MessageQueue) {
if mqSet ==nil{
mqSet = make([]*MessageQueue, 0)
}
r.processQueueTableLock.Lock()
for pgmq,_ := range r.processQueueTable{
exist := false
for _,mq := range mqSet {
if(mq.brokerName == pgmq.brokerName && mq.queueId == pgmq.queueId){
exist=true
}
}
if !exist {
delete(r.processQueueTable,pgmq)
}
}
r.processQueueTableLock.Unlock()
for _, mq := range mqSet {
r.processQueueTableLock.RLock()
_, ok := r.processQueueTable[*mq]
r.processQueueTableLock.RUnlock()
if !ok {
pullRequest := new(PullRequest)
pullRequest.consumerGroup = r.groupName
pullRequest.messageQueue = mq
pullRequest.nextOffset = r.computePullFromWhere(mq)
r.mqClient.pullMessageService.pullRequestQueue <- pullRequest
r.processQueueTableLock.Lock()
r.processQueueTable[*mq] = 1
r.processQueueTableLock.Unlock()
}
}
}
func (r *Rebalance) computePullFromWhere(mq *MessageQueue) int64 {
var result int64 = -1
lastOffset := r.consumer.offsetStore.readOffset(mq, ReadFromStore)
if lastOffset >= 0 {
result = lastOffset
} else {
result = 0
}
return result
}