Search in sources :

Example 11 with SubscriptionGroupWrapper

use of org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQAdminExtTest method init.

@BeforeClass
public static void init() throws Exception {
    mQClientAPIImpl = mock(MQClientAPIImpl.class);
    defaultMQAdminExt = new DefaultMQAdminExt();
    defaultMQAdminExtImpl = new DefaultMQAdminExtImpl(defaultMQAdminExt, 1000);
    Field field = DefaultMQAdminExtImpl.class.getDeclaredField("mqClientInstance");
    field.setAccessible(true);
    field.set(defaultMQAdminExtImpl, mqClientInstance);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mqClientInstance, mQClientAPIImpl);
    field = DefaultMQAdminExt.class.getDeclaredField("defaultMQAdminExtImpl");
    field.setAccessible(true);
    field.set(defaultMQAdminExt, defaultMQAdminExtImpl);
    properties.setProperty("maxMessageSize", "5000000");
    properties.setProperty("flushDelayOffsetInterval", "15000");
    properties.setProperty("serverSocketRcvBufSize", "655350");
    when(mQClientAPIImpl.getBrokerConfig(anyString(), anyLong())).thenReturn(properties);
    Set<String> topicSet = new HashSet<>();
    topicSet.add("topic_one");
    topicSet.add("topic_two");
    topicList.setTopicList(topicSet);
    when(mQClientAPIImpl.getTopicListFromNameServer(anyLong())).thenReturn(topicList);
    List<BrokerData> brokerDatas = new ArrayList<>();
    HashMap<Long, String> brokerAddrs = new HashMap<>();
    brokerAddrs.put(1234l, "127.0.0.1:10911");
    BrokerData brokerData = new BrokerData();
    brokerData.setCluster("default-cluster");
    brokerData.setBrokerName("default-broker");
    brokerData.setBrokerAddrs(brokerAddrs);
    brokerDatas.add(brokerData);
    topicRouteData.setBrokerDatas(brokerDatas);
    topicRouteData.setQueueDatas(new ArrayList<QueueData>());
    topicRouteData.setFilterServerTable(new HashMap<String, List<String>>());
    when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(topicRouteData);
    HashMap<String, String> result = new HashMap<>();
    result.put("id", "1234");
    result.put("brokerName", "default-broker");
    kvTable.setTable(result);
    when(mQClientAPIImpl.getBrokerRuntimeInfo(anyString(), anyLong())).thenReturn(kvTable);
    HashMap<String, BrokerData> brokerAddrTable = new HashMap<>();
    brokerAddrTable.put("default-broker", brokerData);
    brokerAddrTable.put("broker-test", new BrokerData());
    clusterInfo.setBrokerAddrTable(brokerAddrTable);
    clusterInfo.setClusterAddrTable(new HashMap<String, Set<String>>());
    when(mQClientAPIImpl.getBrokerClusterInfo(anyLong())).thenReturn(clusterInfo);
    when(mQClientAPIImpl.cleanExpiredConsumeQueue(anyString(), anyLong())).thenReturn(true);
    Set<String> clusterList = new HashSet<>();
    clusterList.add("default-cluster-one");
    clusterList.add("default-cluster-two");
    when(mQClientAPIImpl.getClusterList(anyString(), anyLong())).thenReturn(clusterList);
    GroupList groupList = new GroupList();
    HashSet<String> groups = new HashSet<>();
    groups.add("consumer-group-one");
    groups.add("consumer-group-two");
    groupList.setGroupList(groups);
    when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(topicRouteData);
    when(mQClientAPIImpl.queryTopicConsumeByWho(anyString(), anyString(), anyLong())).thenReturn(groupList);
    SubscriptionGroupWrapper subscriptionGroupWrapper = new SubscriptionGroupWrapper();
    ConcurrentHashMap<String, SubscriptionGroupConfig> subscriptions = new ConcurrentHashMap<>();
    SubscriptionGroupConfig subscriptionGroupConfig = new SubscriptionGroupConfig();
    subscriptionGroupConfig.setConsumeBroadcastEnable(true);
    subscriptionGroupConfig.setBrokerId(1234);
    subscriptionGroupConfig.setGroupName("Consumer-group-one");
    subscriptions.put("Consumer-group-one", subscriptionGroupConfig);
    subscriptionGroupWrapper.setSubscriptionGroupTable(subscriptions);
    when(mQClientAPIImpl.getAllSubscriptionGroup(anyString(), anyLong())).thenReturn(subscriptionGroupWrapper);
    String topicListConfig = "topicListConfig";
    when(mQClientAPIImpl.getKVConfigValue(anyString(), anyString(), anyLong())).thenReturn(topicListConfig);
    KVTable kvTable = new KVTable();
    HashMap<String, String> kv = new HashMap<>();
    kv.put("broker-name", "broker-one");
    kv.put("cluster-name", "default-cluster");
    kvTable.setTable(kv);
    when(mQClientAPIImpl.getKVListByNamespace(anyString(), anyLong())).thenReturn(kvTable);
    ConsumeStats consumeStats = new ConsumeStats();
    consumeStats.setConsumeTps(1234);
    MessageQueue messageQueue = new MessageQueue();
    OffsetWrapper offsetWrapper = new OffsetWrapper();
    HashMap<MessageQueue, OffsetWrapper> stats = new HashMap<>();
    stats.put(messageQueue, offsetWrapper);
    consumeStats.setOffsetTable(stats);
    when(mQClientAPIImpl.getConsumeStats(anyString(), anyString(), anyString(), anyLong())).thenReturn(consumeStats);
    ConsumerConnection consumerConnection = new ConsumerConnection();
    consumerConnection.setConsumeType(ConsumeType.CONSUME_PASSIVELY);
    consumerConnection.setMessageModel(MessageModel.CLUSTERING);
    HashSet<Connection> connections = new HashSet<>();
    connections.add(new Connection());
    consumerConnection.setConnectionSet(connections);
    consumerConnection.setSubscriptionTable(new ConcurrentHashMap<String, SubscriptionData>());
    consumerConnection.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    when(mQClientAPIImpl.getConsumerConnectionList(anyString(), anyString(), anyLong())).thenReturn(consumerConnection);
    ProducerConnection producerConnection = new ProducerConnection();
    Connection connection = new Connection();
    connection.setClientAddr("127.0.0.1:9898");
    connection.setClientId("PID_12345");
    HashSet<Connection> connectionSet = new HashSet<Connection>();
    connectionSet.add(connection);
    producerConnection.setConnectionSet(connectionSet);
    when(mQClientAPIImpl.getProducerConnectionList(anyString(), anyString(), anyLong())).thenReturn(producerConnection);
    when(mQClientAPIImpl.wipeWritePermOfBroker(anyString(), anyString(), anyLong())).thenReturn(6);
    TopicStatsTable topicStatsTable = new TopicStatsTable();
    topicStatsTable.setOffsetTable(new HashMap<MessageQueue, TopicOffset>());
    Map<String, Map<MessageQueue, Long>> consumerStatus = new HashMap<>();
    when(mQClientAPIImpl.invokeBrokerToGetConsumerStatus(anyString(), anyString(), anyString(), anyString(), anyLong())).thenReturn(consumerStatus);
    List<QueueTimeSpan> queueTimeSpanList = new ArrayList<>();
    when(mQClientAPIImpl.queryConsumeTimeSpan(anyString(), anyString(), anyString(), anyLong())).thenReturn(queueTimeSpanList);
    ConsumerRunningInfo consumerRunningInfo = new ConsumerRunningInfo();
    consumerRunningInfo.setJstack("test");
    consumerRunningInfo.setMqTable(new TreeMap<MessageQueue, ProcessQueueInfo>());
    consumerRunningInfo.setStatusTable(new TreeMap<String, ConsumeStatus>());
    consumerRunningInfo.setSubscriptionSet(new TreeSet<SubscriptionData>());
    when(mQClientAPIImpl.getConsumerRunningInfo(anyString(), anyString(), anyString(), anyBoolean(), anyLong())).thenReturn(consumerRunningInfo);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) QueueData(org.apache.rocketmq.common.protocol.route.QueueData) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ProducerConnection(org.apache.rocketmq.common.protocol.body.ProducerConnection) GroupList(org.apache.rocketmq.common.protocol.body.GroupList) TopicStatsTable(org.apache.rocketmq.common.admin.TopicStatsTable) TopicList(org.apache.rocketmq.common.protocol.body.TopicList) GroupList(org.apache.rocketmq.common.protocol.body.GroupList) List(java.util.List) ArrayList(java.util.ArrayList) ConsumeStatsList(org.apache.rocketmq.common.protocol.body.ConsumeStatsList) HashSet(java.util.HashSet) KVTable(org.apache.rocketmq.common.protocol.body.KVTable) ConsumeStats(org.apache.rocketmq.common.admin.ConsumeStats) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) ConsumeStatus(org.apache.rocketmq.common.protocol.body.ConsumeStatus) QueueTimeSpan(org.apache.rocketmq.common.protocol.body.QueueTimeSpan) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) SubscriptionGroupWrapper(org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig) OffsetWrapper(org.apache.rocketmq.common.admin.OffsetWrapper) Field(java.lang.reflect.Field) TopicOffset(org.apache.rocketmq.common.admin.TopicOffset) ConsumerRunningInfo(org.apache.rocketmq.common.protocol.body.ConsumerRunningInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ProcessQueueInfo(org.apache.rocketmq.common.protocol.body.ProcessQueueInfo) Connection(org.apache.rocketmq.common.protocol.body.Connection) ConsumerConnection(org.apache.rocketmq.common.protocol.body.ConsumerConnection) ProducerConnection(org.apache.rocketmq.common.protocol.body.ProducerConnection) MQClientAPIImpl(org.apache.rocketmq.client.impl.MQClientAPIImpl) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance) BeforeClass(org.junit.BeforeClass)

Example 12 with SubscriptionGroupWrapper

use of org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper in project rocketmq-externals by apache.

the class MQAdminExtImpl method examineSubscriptionGroupConfig.

@Override
public SubscriptionGroupConfig examineSubscriptionGroupConfig(String addr, String group) {
    RemotingClient remotingClient = MQAdminInstance.threadLocalRemotingClient();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_ALL_SUBSCRIPTIONGROUP_CONFIG, null);
    RemotingCommand response = null;
    try {
        response = remotingClient.invokeSync(addr, request, 3000);
    } catch (Exception err) {
        throw Throwables.propagate(err);
    }
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                SubscriptionGroupWrapper subscriptionGroupWrapper = decode(response.getBody(), SubscriptionGroupWrapper.class);
                return subscriptionGroupWrapper.getSubscriptionGroupTable().get(group);
            }
        default:
            throw Throwables.propagate(new MQBrokerException(response.getCode(), response.getRemark()));
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) RemotingClient(org.apache.rocketmq.remoting.RemotingClient) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) SubscriptionGroupWrapper(org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper)

Example 13 with SubscriptionGroupWrapper

use of org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper in project rocketmq by apache.

the class ExportMetadataCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        defaultMQAdminExt.start();
        String filePath = !commandLine.hasOption('f') ? DEFAULT_FILE_PATH : commandLine.getOptionValue('f').trim();
        boolean specialTopic = commandLine.hasOption('s');
        if (commandLine.hasOption('b')) {
            final String brokerAddr = commandLine.getOptionValue('b').trim();
            if (commandLine.hasOption('t')) {
                filePath = filePath + "/topic.json";
                TopicConfigSerializeWrapper topicConfigSerializeWrapper = defaultMQAdminExt.getUserTopicConfig(brokerAddr, specialTopic, 10000L);
                MixAll.string2FileNotSafe(JSON.toJSONString(topicConfigSerializeWrapper, true), filePath);
                System.out.printf("export %s success", filePath);
            } else if (commandLine.hasOption('g')) {
                filePath = filePath + "/subscriptionGroup.json";
                SubscriptionGroupWrapper subscriptionGroupWrapper = defaultMQAdminExt.getUserSubscriptionGroup(brokerAddr, 10000L);
                MixAll.string2FileNotSafe(JSON.toJSONString(subscriptionGroupWrapper, true), filePath);
                System.out.printf("export %s success", filePath);
            }
        } else if (commandLine.hasOption('c')) {
            String clusterName = commandLine.getOptionValue('c').trim();
            Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName);
            Map<String, TopicConfig> topicConfigMap = new HashMap<>();
            Map<String, SubscriptionGroupConfig> subGroupConfigMap = new HashMap<>();
            Map<String, Object> result = new HashMap<>();
            for (String addr : masterSet) {
                TopicConfigSerializeWrapper topicConfigSerializeWrapper = defaultMQAdminExt.getUserTopicConfig(addr, specialTopic, 10000L);
                SubscriptionGroupWrapper subscriptionGroupWrapper = defaultMQAdminExt.getUserSubscriptionGroup(addr, 10000);
                for (Map.Entry<String, TopicConfig> entry : topicConfigSerializeWrapper.getTopicConfigTable().entrySet()) {
                    TopicConfig topicConfig = topicConfigMap.get(entry.getKey());
                    if (null != topicConfig) {
                        entry.getValue().setWriteQueueNums(topicConfig.getWriteQueueNums() + entry.getValue().getWriteQueueNums());
                        entry.getValue().setReadQueueNums(topicConfig.getReadQueueNums() + entry.getValue().getReadQueueNums());
                    }
                    topicConfigMap.put(entry.getKey(), entry.getValue());
                }
                for (Map.Entry<String, SubscriptionGroupConfig> entry : subscriptionGroupWrapper.getSubscriptionGroupTable().entrySet()) {
                    SubscriptionGroupConfig subscriptionGroupConfig = subGroupConfigMap.get(entry.getKey());
                    if (null != subscriptionGroupConfig) {
                        entry.getValue().setRetryQueueNums(subscriptionGroupConfig.getRetryQueueNums() + entry.getValue().getRetryQueueNums());
                    }
                    subGroupConfigMap.put(entry.getKey(), entry.getValue());
                }
            }
            String exportPath;
            if (commandLine.hasOption('t')) {
                result.put("topicConfigTable", topicConfigMap);
                exportPath = filePath + "/topic.json";
            } else if (commandLine.hasOption('g')) {
                result.put("subscriptionGroupTable", subGroupConfigMap);
                exportPath = filePath + "/subscriptionGroup.json";
            } else {
                result.put("topicConfigTable", topicConfigMap);
                result.put("subscriptionGroupTable", subGroupConfigMap);
                exportPath = filePath + "/metadata.json";
            }
            result.put("exportTime", System.currentTimeMillis());
            MixAll.string2FileNotSafe(JSON.toJSONString(result, true), exportPath);
            System.out.printf("export %s success%n", exportPath);
        } else {
            ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : Set(java.util.Set) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) TopicConfigSerializeWrapper(org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper) TopicConfig(org.apache.rocketmq.common.TopicConfig) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) HashMap(java.util.HashMap) Map(java.util.Map) SubscriptionGroupWrapper(org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper)

Example 14 with SubscriptionGroupWrapper

use of org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper in project rocketmq by apache.

the class DefaultMQAdminExtImpl method getUserSubscriptionGroup.

@Override
public SubscriptionGroupWrapper getUserSubscriptionGroup(final String brokerAddr, long timeoutMillis) throws InterruptedException, RemotingTimeoutException, RemotingSendRequestException, RemotingConnectException, MQBrokerException {
    SubscriptionGroupWrapper subscriptionGroupWrapper = this.mqClientInstance.getMQClientAPIImpl().getAllSubscriptionGroup(brokerAddr, timeoutMillis);
    Iterator<Entry<String, SubscriptionGroupConfig>> iterator = subscriptionGroupWrapper.getSubscriptionGroupTable().entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, SubscriptionGroupConfig> configEntry = iterator.next();
        if (MixAll.isSysConsumerGroup(configEntry.getKey()) || SYSTEM_GROUP_SET.contains(configEntry.getKey())) {
            iterator.remove();
        }
    }
    return subscriptionGroupWrapper;
}
Also used : Entry(java.util.Map.Entry) Map(java.util.Map) HashMap(java.util.HashMap) SubscriptionGroupWrapper(org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)

Example 15 with SubscriptionGroupWrapper

use of org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper in project rocketmq by apache.

the class SlaveSynchronize method syncSubscriptionGroupConfig.

private void syncSubscriptionGroupConfig() {
    String masterAddrBak = this.masterAddr;
    if (masterAddrBak != null && !masterAddrBak.equals(brokerController.getBrokerAddr())) {
        try {
            SubscriptionGroupWrapper subscriptionWrapper = this.brokerController.getBrokerOuterAPI().getAllSubscriptionGroupConfig(masterAddrBak);
            if (!this.brokerController.getSubscriptionGroupManager().getDataVersion().equals(subscriptionWrapper.getDataVersion())) {
                SubscriptionGroupManager subscriptionGroupManager = this.brokerController.getSubscriptionGroupManager();
                subscriptionGroupManager.getDataVersion().assignNewOne(subscriptionWrapper.getDataVersion());
                subscriptionGroupManager.getSubscriptionGroupTable().clear();
                subscriptionGroupManager.getSubscriptionGroupTable().putAll(subscriptionWrapper.getSubscriptionGroupTable());
                subscriptionGroupManager.persist();
                log.info("Update slave Subscription Group from master, {}", masterAddrBak);
            }
        } catch (Exception e) {
            log.error("SyncSubscriptionGroup Exception, {}", masterAddrBak, e);
        }
    }
}
Also used : SubscriptionGroupManager(org.apache.rocketmq.broker.subscription.SubscriptionGroupManager) SubscriptionGroupWrapper(org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper) IOException(java.io.IOException)

Aggregations

SubscriptionGroupWrapper (org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper)17 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 Map (java.util.Map)7 SubscriptionGroupConfig (org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)6 HashMap (java.util.HashMap)5 MQClientException (org.apache.rocketmq.client.exception.MQClientException)5 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)5 ArrayList (java.util.ArrayList)4 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)4 Set (java.util.Set)3 KVTable (org.apache.rocketmq.common.protocol.body.KVTable)3 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 HashSet (java.util.HashSet)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 SubscriptionGroupManager (org.apache.rocketmq.broker.subscription.SubscriptionGroupManager)2