48
48
import org .apache .rocketmq .remoting .protocol .subscription .SubscriptionGroupConfig ;
49
49
import org .apache .rocketmq .store .DefaultMessageFilter ;
50
50
import org .apache .rocketmq .store .MessageStore ;
51
+ import org .apache .rocketmq .store .exception .ConsumeQueueException ;
51
52
52
53
public class ConsumerLagCalculator {
53
54
private final BrokerConfig brokerConfig ;
@@ -212,45 +213,61 @@ public void calculateLag(Consumer<CalculateLagResult> lagRecorder) {
212
213
213
214
CalculateLagResult result = new CalculateLagResult (info .group , info .topic , false );
214
215
215
- Pair <Long , Long > lag = getConsumerLagStats (info .group , info .topic , info .isPop );
216
- if (lag != null ) {
217
- result .lag = lag .getObject1 ();
218
- result .earliestUnconsumedTimestamp = lag .getObject2 ();
216
+ try {
217
+ Pair <Long , Long > lag = getConsumerLagStats (info .group , info .topic , info .isPop );
218
+ if (lag != null ) {
219
+ result .lag = lag .getObject1 ();
220
+ result .earliestUnconsumedTimestamp = lag .getObject2 ();
221
+ }
222
+ lagRecorder .accept (result );
223
+ } catch (ConsumeQueueException e ) {
224
+ LOGGER .error ("Failed to get lag stats" , e );
219
225
}
220
- lagRecorder .accept (result );
221
226
222
227
if (info .isPop ) {
223
- Pair <Long , Long > retryLag = getConsumerLagStats (info .group , info .retryTopic , true );
228
+ try {
229
+ Pair <Long , Long > retryLag = getConsumerLagStats (info .group , info .retryTopic , true );
224
230
225
- result = new CalculateLagResult (info .group , info .topic , true );
226
- if (retryLag != null ) {
227
- result .lag = retryLag .getObject1 ();
228
- result .earliestUnconsumedTimestamp = retryLag .getObject2 ();
231
+ result = new CalculateLagResult (info .group , info .topic , true );
232
+ if (retryLag != null ) {
233
+ result .lag = retryLag .getObject1 ();
234
+ result .earliestUnconsumedTimestamp = retryLag .getObject2 ();
235
+ }
236
+ lagRecorder .accept (result );
237
+ } catch (ConsumeQueueException e ) {
238
+ LOGGER .error ("Failed to get lag stats" , e );
229
239
}
230
- lagRecorder .accept (result );
231
240
}
232
241
});
233
242
}
234
243
235
244
public void calculateInflight (Consumer <CalculateInflightResult > inflightRecorder ) {
236
245
processAllGroup (info -> {
237
246
CalculateInflightResult result = new CalculateInflightResult (info .group , info .topic , false );
238
- Pair <Long , Long > inFlight = getInFlightMsgStats (info .group , info .topic , info .isPop );
239
- if (inFlight != null ) {
240
- result .inFlight = inFlight .getObject1 ();
241
- result .earliestUnPulledTimestamp = inFlight .getObject2 ();
247
+ try {
248
+ Pair <Long , Long > inFlight = getInFlightMsgStats (info .group , info .topic , info .isPop );
249
+ if (inFlight != null ) {
250
+ result .inFlight = inFlight .getObject1 ();
251
+ result .earliestUnPulledTimestamp = inFlight .getObject2 ();
252
+ }
253
+ inflightRecorder .accept (result );
254
+ } catch (ConsumeQueueException e ) {
255
+ LOGGER .error ("Failed to get inflight message stats" , e );
242
256
}
243
- inflightRecorder .accept (result );
244
257
245
258
if (info .isPop ) {
246
- Pair <Long , Long > retryInFlight = getInFlightMsgStats (info .group , info .retryTopic , true );
259
+ try {
260
+ Pair <Long , Long > retryInFlight = getInFlightMsgStats (info .group , info .retryTopic , true );
247
261
248
- result = new CalculateInflightResult (info .group , info .topic , true );
249
- if (retryInFlight != null ) {
250
- result .inFlight = retryInFlight .getObject1 ();
251
- result .earliestUnPulledTimestamp = retryInFlight .getObject2 ();
262
+ result = new CalculateInflightResult (info .group , info .topic , true );
263
+ if (retryInFlight != null ) {
264
+ result .inFlight = retryInFlight .getObject1 ();
265
+ result .earliestUnPulledTimestamp = retryInFlight .getObject2 ();
266
+ }
267
+ inflightRecorder .accept (result );
268
+ } catch (ConsumeQueueException e ) {
269
+ LOGGER .error ("Failed to get inflight message stats" , e );
252
270
}
253
- inflightRecorder .accept (result );
254
271
}
255
272
});
256
273
}
@@ -259,20 +276,28 @@ public void calculateAvailable(Consumer<CalculateAvailableResult> availableRecor
259
276
processAllGroup (info -> {
260
277
CalculateAvailableResult result = new CalculateAvailableResult (info .group , info .topic , false );
261
278
262
- result .available = getAvailableMsgCount (info .group , info .topic , info .isPop );
263
- availableRecorder .accept (result );
279
+ try {
280
+ result .available = getAvailableMsgCount (info .group , info .topic , info .isPop );
281
+ availableRecorder .accept (result );
282
+ } catch (ConsumeQueueException e ) {
283
+ LOGGER .error ("Failed to get available message count" , e );
284
+ }
264
285
265
- if (info .isPop ) {
266
- long retryAvailable = getAvailableMsgCount (info .group , info .retryTopic , true );
267
286
268
- result = new CalculateAvailableResult (info .group , info .topic , true );
269
- result .available = retryAvailable ;
270
- availableRecorder .accept (result );
287
+ if (info .isPop ) {
288
+ try {
289
+ long retryAvailable = getAvailableMsgCount (info .group , info .retryTopic , true );
290
+ result = new CalculateAvailableResult (info .group , info .topic , true );
291
+ result .available = retryAvailable ;
292
+ availableRecorder .accept (result );
293
+ } catch (ConsumeQueueException e ) {
294
+ LOGGER .error ("Failed to get available message count" , e );
295
+ }
271
296
}
272
297
});
273
298
}
274
299
275
- public Pair <Long , Long > getConsumerLagStats (String group , String topic , boolean isPop ) {
300
+ public Pair <Long , Long > getConsumerLagStats (String group , String topic , boolean isPop ) throws ConsumeQueueException {
276
301
long total = 0L ;
277
302
long earliestUnconsumedTimestamp = Long .MAX_VALUE ;
278
303
@@ -298,7 +323,8 @@ public Pair<Long, Long> getConsumerLagStats(String group, String topic, boolean
298
323
return new Pair <>(total , earliestUnconsumedTimestamp );
299
324
}
300
325
301
- public Pair <Long , Long > getConsumerLagStats (String group , String topic , int queueId , boolean isPop ) {
326
+ public Pair <Long , Long > getConsumerLagStats (String group , String topic , int queueId , boolean isPop )
327
+ throws ConsumeQueueException {
302
328
long brokerOffset = messageStore .getMaxOffsetInQueue (topic , queueId );
303
329
if (brokerOffset < 0 ) {
304
330
brokerOffset = 0 ;
@@ -329,7 +355,7 @@ public Pair<Long, Long> getConsumerLagStats(String group, String topic, int queu
329
355
return new Pair <>(lag , consumerStoreTimeStamp );
330
356
}
331
357
332
- public Pair <Long , Long > getInFlightMsgStats (String group , String topic , boolean isPop ) {
358
+ public Pair <Long , Long > getInFlightMsgStats (String group , String topic , boolean isPop ) throws ConsumeQueueException {
333
359
long total = 0L ;
334
360
long earliestUnPulledTimestamp = Long .MAX_VALUE ;
335
361
@@ -355,7 +381,8 @@ public Pair<Long, Long> getInFlightMsgStats(String group, String topic, boolean
355
381
return new Pair <>(total , earliestUnPulledTimestamp );
356
382
}
357
383
358
- public Pair <Long , Long > getInFlightMsgStats (String group , String topic , int queueId , boolean isPop ) {
384
+ public Pair <Long , Long > getInFlightMsgStats (String group , String topic , int queueId , boolean isPop )
385
+ throws ConsumeQueueException {
359
386
if (isPop ) {
360
387
long inflight = popInflightMessageCounter .getGroupPopInFlightMessageNum (topic , group , queueId );
361
388
long pullOffset = popBufferMergeService .getLatestOffset (topic , group , queueId );
@@ -384,7 +411,7 @@ public Pair<Long, Long> getInFlightMsgStats(String group, String topic, int queu
384
411
return new Pair <>(inflight , pullStoreTimeStamp );
385
412
}
386
413
387
- public long getAvailableMsgCount (String group , String topic , boolean isPop ) {
414
+ public long getAvailableMsgCount (String group , String topic , boolean isPop ) throws ConsumeQueueException {
388
415
long total = 0L ;
389
416
390
417
if (group == null || topic == null ) {
@@ -403,7 +430,8 @@ public long getAvailableMsgCount(String group, String topic, boolean isPop) {
403
430
return total ;
404
431
}
405
432
406
- public long getAvailableMsgCount (String group , String topic , int queueId , boolean isPop ) {
433
+ public long getAvailableMsgCount (String group , String topic , int queueId , boolean isPop )
434
+ throws ConsumeQueueException {
407
435
long brokerOffset = messageStore .getMaxOffsetInQueue (topic , queueId );
408
436
if (brokerOffset < 0 ) {
409
437
brokerOffset = 0 ;
0 commit comments