/
strategy.go
227 lines (201 loc) · 6 KB
/
strategy.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.dreaminglwj.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package consumer
import (
"github.com/dreaminglwj/rocketmq-client-go/internal/utils"
"github.com/dreaminglwj/rocketmq-client-go/primitive"
"github.com/dreaminglwj/rocketmq-client-go/rlog"
"stathat.com/c/consistent"
"strings"
)
// Strategy Algorithm for message allocating between consumers
// An allocate strategy proxy for based on machine room nearside priority. An actual allocate strategy can be
// specified.
//
// If any consumer is alive in a machine room, the message queue of the broker which is deployed in the same machine
// should only be allocated to those. Otherwise, those message queues can be shared along all consumers since there are
// no alive consumer to monopolize them.
//
// Average Hashing queue algorithm
// Cycle average Hashing queue algorithm
// Use Message QueueID specified
// Computer room Hashing queue algorithm, such as Alipay logic room
// Consistent Hashing queue algorithm
type AllocateStrategy func(string, string, []*primitive.MessageQueue, []string) []*primitive.MessageQueue
func AllocateByAveragely(consumerGroup, currentCID string, mqAll []*primitive.MessageQueue,
cidAll []string) []*primitive.MessageQueue {
if currentCID == "" || len(mqAll) == 0 || len(cidAll) == 0 {
return nil
}
var (
find bool
index int
)
for idx := range cidAll {
if cidAll[idx] == currentCID {
find = true
index = idx
break
}
}
if !find {
rlog.Warnf("[BUG] ConsumerGroup=%s, ConsumerId=%s not in cidAll:%+v", consumerGroup, currentCID, cidAll)
return nil
}
mqSize := len(mqAll)
cidSize := len(cidAll)
mod := mqSize % cidSize
var averageSize int
if mqSize <= cidSize {
averageSize = 1
} else {
if mod > 0 && index < mod {
averageSize = mqSize/cidSize + 1
} else {
averageSize = mqSize / cidSize
}
}
var startIndex int
if mod > 0 && index < mod {
startIndex = index * averageSize
} else {
startIndex = index*averageSize + mod
}
num := utils.MinInt(averageSize, mqSize-startIndex)
result := []*primitive.MessageQueue{}
for i := 0; i < num; i++ {
result = append(result, mqAll[(startIndex+i)%mqSize])
}
return result
}
func AllocateByAveragelyCircle(consumerGroup, currentCID string, mqAll []*primitive.MessageQueue,
cidAll []string) []*primitive.MessageQueue {
if currentCID == "" || len(mqAll) == 0 || len(cidAll) == 0 {
return nil
}
var (
find bool
index int
)
for idx := range cidAll {
if cidAll[idx] == currentCID {
find = true
index = idx
break
}
}
if !find {
rlog.Warnf("[BUG] ConsumerGroup=%s, ConsumerId=%s not in cidAll:%+v", consumerGroup, currentCID, cidAll)
return nil
}
result := []*primitive.MessageQueue{}
for i := index; i < len(mqAll); i++ {
if i%len(cidAll) == index {
result = append(result, mqAll[i])
}
}
return result
}
// TODO
func AllocateByMachineNearby(consumerGroup, currentCID string, mqAll []*primitive.MessageQueue,
cidAll []string) []*primitive.MessageQueue {
return AllocateByAveragely(consumerGroup, currentCID, mqAll, cidAll)
}
func AllocateByConfig(list []*primitive.MessageQueue) AllocateStrategy {
return func(consumerGroup, currentCID string, mqAll []*primitive.MessageQueue, cidAll []string) []*primitive.MessageQueue {
return list
}
}
func AllocateByMachineRoom(consumeridcs []string) AllocateStrategy {
return func(consumerGroup, currentCID string, mqAll []*primitive.MessageQueue, cidAll []string) []*primitive.MessageQueue {
if currentCID == "" || len(mqAll) == 0 || len(cidAll) == 0 {
return nil
}
var (
find bool
index int
)
for idx := range cidAll {
if cidAll[idx] == currentCID {
find = true
index = idx
break
}
}
if !find {
rlog.Warnf("[BUG] ConsumerGroup=%s, ConsumerId=%s not in cidAll:%+v", consumerGroup, currentCID, cidAll)
return nil
}
premqAll := []*primitive.MessageQueue{}
for _, mq := range mqAll {
temp := strings.Split(mq.BrokerName, "@")
if len(temp) == 2 {
for _, idc := range consumeridcs {
if idc == temp[0] {
premqAll = append(premqAll, mq)
}
}
}
}
mod := len(premqAll) / len(cidAll)
rem := len(premqAll) % len(cidAll)
startIndex := mod * index
endIndex := startIndex + mod
result := []*primitive.MessageQueue{}
for i := startIndex; i < endIndex; i++ {
result = append(result, mqAll[i])
}
if rem > index {
result = append(result, premqAll[index+mod*len(cidAll)])
}
return result
}
}
func AllocateByConsistentHash(virtualNodeCnt int) AllocateStrategy {
return func(consumerGroup, currentCID string, mqAll []*primitive.MessageQueue, cidAll []string) []*primitive.MessageQueue {
if currentCID == "" || len(mqAll) == 0 || len(cidAll) == 0 {
return nil
}
var (
find bool
)
for idx := range cidAll {
if cidAll[idx] == currentCID {
find = true
break
}
}
if !find {
rlog.Warnf("[BUG] ConsumerGroup=%s, ConsumerId=%s not in cidAll:%+v", consumerGroup, currentCID, cidAll)
return nil
}
c := consistent.New()
c.NumberOfReplicas = virtualNodeCnt
for _, cid := range cidAll {
c.Add(cid)
}
result := []*primitive.MessageQueue{}
for _, mq := range mqAll {
clientNode, err := c.Get(mq.String())
if err != nil {
rlog.Warnf("[BUG] AllocateByConsistentHash err:%s", err.Error())
}
if currentCID == clientNode {
result = append(result, mq)
}
}
return result
}
}