forked from didapinchegit/go_rocket_mq
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrebalance.go
182 lines (156 loc) · 4.6 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
package rocketmq
import (
"errors"
"fmt"
"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
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)
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 (r *Rebalance) rebalanceByTopic(topic string) error {
cidAll, err := r.mqClient.findConsumerIdList(topic, r.groupName)
if err != nil {
fmt.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)
if err != nil {
fmt.Println(err)
return err
}
fmt.Printf("rebalance topic[%s]\n", topic)
r.updateProcessQueueTableInRebalance(topic, allocateResult)
return nil
}
func (r *Rebalance) updateProcessQueueTableInRebalance(topic string, mqSet []*MessageQueue) {
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
}