16
16
*/
17
17
package org .apache .rocketmq .broker .offset ;
18
18
19
+ import com .google .common .base .Strings ;
19
20
import com .google .common .collect .Maps ;
20
21
import java .util .HashMap ;
21
22
import java .util .HashSet ;
26
27
import java .util .concurrent .ConcurrentHashMap ;
27
28
import java .util .concurrent .ConcurrentMap ;
28
29
import java .util .concurrent .atomic .AtomicLong ;
29
-
30
- import com .google .common .base .Strings ;
31
-
32
30
import java .util .function .Function ;
33
31
import org .apache .rocketmq .broker .BrokerController ;
34
32
import org .apache .rocketmq .broker .BrokerPathConfigHelper ;
41
39
import org .apache .rocketmq .remoting .protocol .DataVersion ;
42
40
import org .apache .rocketmq .remoting .protocol .RemotingSerializable ;
43
41
42
+ import static org .apache .rocketmq .common .MixAll .BROADCAST_KEY ;
43
+
44
44
public class ConsumerOffsetManager extends ConfigManager {
45
45
protected static final Logger LOG = LoggerFactory .getLogger (LoggerName .BROKER_LOGGER_NAME );
46
46
public static final String TOPIC_GROUP_SEPARATOR = "@" ;
47
47
48
48
protected DataVersion dataVersion = new DataVersion ();
49
49
50
- protected ConcurrentMap <String /* topic@group */ , ConcurrentMap <Integer , Long >> offsetTable =
51
- new ConcurrentHashMap <>(512 );
50
+ protected ConcurrentMap <String /* topic@group */ , ConcurrentMap <Integer , Long >> offsetTable = new ConcurrentHashMap <>(512 );
52
51
53
- protected final ConcurrentMap <String , ConcurrentMap <Integer , Long >> resetOffsetTable =
54
- new ConcurrentHashMap <>(512 );
52
+ protected final ConcurrentMap <String , ConcurrentMap <Integer , Long >> resetOffsetTable = new ConcurrentHashMap <>(512 );
55
53
56
- private final ConcurrentMap <String /* topic@group */ , ConcurrentMap <Integer , Long >> pullOffsetTable =
57
- new ConcurrentHashMap <>(512 );
54
+ private final ConcurrentMap <String /* topic@group */ , ConcurrentMap <Integer , Long >> pullOffsetTable = new ConcurrentHashMap <>(512 );
58
55
59
56
protected transient BrokerController brokerController ;
60
57
@@ -78,7 +75,7 @@ public void cleanOffset(String group) {
78
75
String topicAtGroup = next .getKey ();
79
76
if (topicAtGroup .contains (group )) {
80
77
String [] arrays = topicAtGroup .split (TOPIC_GROUP_SEPARATOR );
81
- if (arrays . length == 2 && group .equals (arrays [1 ])) {
78
+ if (validateOffsetTableKey ( topicAtGroup ) && group .equals (arrays [1 ])) {
82
79
it .remove ();
83
80
removeConsumerOffset (topicAtGroup );
84
81
LOG .warn ("Clean group's offset, {}, {}" , topicAtGroup , next .getValue ());
@@ -94,7 +91,7 @@ public void cleanOffsetByTopic(String topic) {
94
91
String topicAtGroup = next .getKey ();
95
92
if (topicAtGroup .contains (topic )) {
96
93
String [] arrays = topicAtGroup .split (TOPIC_GROUP_SEPARATOR );
97
- if (arrays . length == 2 && topic .equals (arrays [0 ])) {
94
+ if (validateOffsetTableKey ( topicAtGroup ) && topic .equals (arrays [0 ])) {
98
95
it .remove ();
99
96
removeConsumerOffset (topicAtGroup );
100
97
LOG .warn ("Clean topic's offset, {}, {}" , topicAtGroup , next .getValue ());
@@ -109,12 +106,11 @@ public void scanUnsubscribedTopic() {
109
106
Entry <String , ConcurrentMap <Integer , Long >> next = it .next ();
110
107
String topicAtGroup = next .getKey ();
111
108
String [] arrays = topicAtGroup .split (TOPIC_GROUP_SEPARATOR );
112
- if (arrays . length == 2 ) {
109
+ if (validateOffsetTableKey ( topicAtGroup ) ) {
113
110
String topic = arrays [0 ];
114
111
String group = arrays [1 ];
115
112
116
- if (null == brokerController .getConsumerManager ().findSubscriptionData (group , topic )
117
- && this .offsetBehindMuchThanData (topic , next .getValue ())) {
113
+ if (null == brokerController .getConsumerManager ().findSubscriptionData (group , topic ) && this .offsetBehindMuchThanData (topic , next .getValue ())) {
118
114
it .remove ();
119
115
removeConsumerOffset (topicAtGroup );
120
116
LOG .warn ("remove topic offset, {}" , topicAtGroup );
@@ -139,13 +135,12 @@ private boolean offsetBehindMuchThanData(final String topic, ConcurrentMap<Integ
139
135
140
136
public Set <String > whichTopicByConsumer (final String group ) {
141
137
Set <String > topics = new HashSet <>();
142
-
143
138
Iterator <Entry <String , ConcurrentMap <Integer , Long >>> it = this .offsetTable .entrySet ().iterator ();
144
139
while (it .hasNext ()) {
145
140
Entry <String , ConcurrentMap <Integer , Long >> next = it .next ();
146
141
String topicAtGroup = next .getKey ();
147
142
String [] arrays = topicAtGroup .split (TOPIC_GROUP_SEPARATOR );
148
- if (arrays . length == 2 ) {
143
+ if (validateOffsetTableKey ( topicAtGroup ) ) {
149
144
if (group .equals (arrays [1 ])) {
150
145
topics .add (arrays [0 ]);
151
146
}
@@ -163,7 +158,7 @@ public Set<String> whichGroupByTopic(final String topic) {
163
158
Entry <String , ConcurrentMap <Integer , Long >> next = it .next ();
164
159
String topicAtGroup = next .getKey ();
165
160
String [] arrays = topicAtGroup .split (TOPIC_GROUP_SEPARATOR );
166
- if (arrays . length == 2 ) {
161
+ if (validateOffsetTableKey ( topicAtGroup ) ) {
167
162
if (topic .equals (arrays [0 ])) {
168
163
groups .add (arrays [1 ]);
169
164
}
@@ -178,7 +173,7 @@ public Map<String, Set<String>> getGroupTopicMap() {
178
173
179
174
for (String key : this .offsetTable .keySet ()) {
180
175
String [] arr = key .split (TOPIC_GROUP_SEPARATOR );
181
- if (arr . length == 2 ) {
176
+ if (validateOffsetTableKey ( key ) ) {
182
177
String topic = arr [0 ];
183
178
String group = arr [1 ];
184
179
@@ -224,16 +219,16 @@ public void commitPullOffset(final String clientHost, final String group, final
224
219
final long offset ) {
225
220
// topic@group
226
221
String key = topic + TOPIC_GROUP_SEPARATOR + group ;
227
- ConcurrentMap <Integer , Long > map = this .pullOffsetTable .computeIfAbsent (
228
- key , k -> new ConcurrentHashMap <>(32 ));
222
+ ConcurrentMap <Integer , Long > map = this .pullOffsetTable .computeIfAbsent (key , k -> new ConcurrentHashMap <>(32 ));
229
223
map .put (queueId , offset );
230
224
}
231
225
232
226
/**
233
227
* If the target queue has temporary reset offset, return the reset-offset.
234
228
* Otherwise, return the current consume offset in the offset store.
235
- * @param group Consumer group
236
- * @param topic Topic
229
+ *
230
+ * @param group Consumer group
231
+ * @param topic Topic
237
232
* @param queueId Queue ID
238
233
* @return current consume offset or reset offset if there were one.
239
234
*/
@@ -261,8 +256,9 @@ public long queryOffset(final String group, final String topic, final int queueI
261
256
262
257
/**
263
258
* Query pull offset in pullOffsetTable
264
- * @param group Consumer group
265
- * @param topic Topic
259
+ *
260
+ * @param group Consumer group
261
+ * @param topic Topic
266
262
* @param queueId Queue ID
267
263
* @return latest pull offset of consumer group
268
264
*/
@@ -330,7 +326,7 @@ public Map<Integer, Long> queryMinOffsetInAllGroup(final String topic, final Str
330
326
Iterator <String > it = topicGroups .iterator ();
331
327
while (it .hasNext ()) {
332
328
String topicAtGroup = it .next ();
333
- if (group .equals (topicAtGroup .split (TOPIC_GROUP_SEPARATOR )[1 ])) {
329
+ if (validateOffsetTableKey ( topicAtGroup ) && group .equals (topicAtGroup .split (TOPIC_GROUP_SEPARATOR )[1 ])) {
334
330
it .remove ();
335
331
removeConsumerOffset (topicAtGroup );
336
332
}
@@ -341,7 +337,7 @@ public Map<Integer, Long> queryMinOffsetInAllGroup(final String topic, final Str
341
337
for (Map .Entry <String , ConcurrentMap <Integer , Long >> offSetEntry : this .offsetTable .entrySet ()) {
342
338
String topicGroup = offSetEntry .getKey ();
343
339
String [] topicGroupArr = topicGroup .split (TOPIC_GROUP_SEPARATOR );
344
- if (topic .equals (topicGroupArr [0 ])) {
340
+ if (validateOffsetTableKey ( topicGroup ) && topic .equals (topicGroupArr [0 ])) {
345
341
for (Entry <Integer , Long > entry : offSetEntry .getValue ().entrySet ()) {
346
342
long minOffset = this .brokerController .getMessageStore ().getMinOffsetInQueue (topic , entry .getKey ());
347
343
if (entry .getValue () >= minOffset ) {
@@ -407,7 +403,7 @@ public void removeOffset(final String group) {
407
403
String topicAtGroup = entry .getKey ();
408
404
if (topicAtGroup .contains (group )) {
409
405
String [] arrays = topicAtGroup .split (TOPIC_GROUP_SEPARATOR );
410
- if (arrays . length == 2 && group .equals (arrays [1 ])) {
406
+ if (validateOffsetTableKey ( topicAtGroup ) && group .equals (arrays [1 ])) {
411
407
it .remove ();
412
408
removeConsumerOffset (topicAtGroup );
413
409
removed = true ;
@@ -421,14 +417,12 @@ public void removeOffset(final String group) {
421
417
boolean clearReset = deleteFunction .apply (this .resetOffsetTable .entrySet ().iterator ());
422
418
boolean clearPull = deleteFunction .apply (this .pullOffsetTable .entrySet ().iterator ());
423
419
424
- LOG .info ("Consumer offset manager clean group offset, groupName={}, " +
425
- "offsetTable={}, resetOffsetTable={}, pullOffsetTable={}" , group , clearOffset , clearReset , clearPull );
420
+ LOG .info ("Consumer offset manager clean group offset, groupName={}, " + "offsetTable={}, resetOffsetTable={}, pullOffsetTable={}" , group , clearOffset , clearReset , clearPull );
426
421
}
427
422
428
423
public void assignResetOffset (String topic , String group , int queueId , long offset ) {
429
424
if (Strings .isNullOrEmpty (topic ) || Strings .isNullOrEmpty (group ) || queueId < 0 || offset < 0 ) {
430
- LOG .warn ("Illegal arguments when assigning reset offset. Topic={}, group={}, queueId={}, offset={}" ,
431
- topic , group , queueId , offset );
425
+ LOG .warn ("Illegal arguments when assigning reset offset. Topic={}, group={}, queueId={}, offset={}" , topic , group , queueId , offset );
432
426
return ;
433
427
}
434
428
@@ -461,4 +455,9 @@ public Long queryThenEraseResetOffset(String topic, String group, Integer queueI
461
455
return map .remove (queueId );
462
456
}
463
457
}
464
- }
458
+
459
+ public boolean validateOffsetTableKey (String key ) {
460
+ String [] arr = key .split (TOPIC_GROUP_SEPARATOR );
461
+ return arr .length == 2 || (arr .length == 3 && BROADCAST_KEY .equals (arr [2 ]));
462
+ }
463
+ }
0 commit comments