com.aliyun.openservices.ons.api.exactlyonce.manager.util.OffsetUtil Maven / Gradle / Ivy
package com.aliyun.openservices.ons.api.exactlyonce.manager.util;
import com.aliyun.openservices.shade.com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.consumer.store.ReadOffsetType;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.impl.consumer.ProcessQueue;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.common.message.MessageQueue;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.logging.InternalLogger;
import com.aliyun.openservices.ons.api.exactlyonce.manager.TransactionManager;
import com.aliyun.openservices.ons.api.impl.util.ClientLoggerUtil;
/**
* @author gongshi
*/
public class OffsetUtil {
private static final InternalLogger LOGGER = ClientLoggerUtil.getClientLogger();
public static Long getMQSafeOffset(MessageQueue mq, String consumerGroup) {
if (mq == null) {
return null;
}
DefaultMQPushConsumer consumer = TransactionManager.getConsumer(consumerGroup).iterator().next();
if (consumer == null) {
return null;
} else {
// offset is -2 in old code when exception raised during offset read, just keep the same return value.
long realOffset = -2;
try {
final Optional optionalOffset =
consumer.getDefaultMQPushConsumerImpl().getOffsetStore().readOffset(mq,
ReadOffsetType.READ_FROM_STORE);
// offset is -1 in old code when offset not found during offset read, just keep the same return value.
realOffset = optionalOffset.isPresent() ? optionalOffset.get() : -1;
} catch (Throwable t) {
LOGGER.error("Failed to read offset, mq={}", mq, t);
}
return realOffset - consumer.getPullThresholdForQueue() * 2;
}
}
public static Set getMessageQueue(String consumerGroup, String topic) {
Set mqAll = new HashSet();
Set consumerSet = TransactionManager.getConsumer(consumerGroup);
if (consumerSet == null || consumerSet.isEmpty()) {
return mqAll;
}
for (DefaultMQPushConsumer consumer : consumerSet) {
try {
Set mqSet = consumer.getDefaultMQPushConsumerImpl().fetchSubscribeMessageQueues(topic);
if (mqSet != null) {
mqAll.addAll(mqSet);
}
} catch (Exception e) {
LogUtil.error(LOGGER, "fetchSubscribeMessageQueues fail, topic:{}, err:{}", topic, e.getMessage());
}
}
return mqAll;
}
public static List getCurrentConsumeQueue(String consumerGroup) {
Set consumerSet = TransactionManager.getConsumer(consumerGroup);
if (consumerSet == null) {
return null;
}
List mqAll = new ArrayList();
for (DefaultMQPushConsumer consumer : consumerSet) {
try {
ConcurrentMap processTable = consumer.getDefaultMQPushConsumerImpl().getRebalanceImpl().getProcessQueueTable();
if (processTable != null) {
mqAll.addAll(processTable.keySet());
}
} catch (Exception e) {
LogUtil.error(LOGGER, "getProcessQueueTable fail, consumerGroup:{}, err:{}", consumer.getConsumerGroup(), e.getMessage());
}
}
return mqAll;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy