Search in sources :

Example 16 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 17 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 18 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)

Example 19 with SubscriptionGroupWrapper

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

the class DefaultMQAdminExtTest method testGetAllSubscriptionGroup.

@Test
public void testGetAllSubscriptionGroup() throws InterruptedException, MQBrokerException, RemotingTimeoutException, RemotingSendRequestException, RemotingConnectException {
    SubscriptionGroupWrapper subscriptionGroupWrapper = defaultMQAdminExt.getAllSubscriptionGroup("127.0.0.1:10911", 10000);
    assertThat(subscriptionGroupWrapper.getSubscriptionGroupTable().get("Consumer-group-one").getBrokerId()).isEqualTo(1234);
    assertThat(subscriptionGroupWrapper.getSubscriptionGroupTable().get("Consumer-group-one").getGroupName()).isEqualTo("Consumer-group-one");
    assertThat(subscriptionGroupWrapper.getSubscriptionGroupTable().get("Consumer-group-one").isConsumeBroadcastEnable()).isTrue();
}
Also used : SubscriptionGroupWrapper(org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper) Test(org.junit.Test)

Example 20 with SubscriptionGroupWrapper

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

the class ConnectUtil method fetchAllConsumerGroupList.

public static Set<String> fetchAllConsumerGroupList(ConnectConfig connectConfig) {
    Set<String> consumerGroupSet = Sets.newHashSet();
    DefaultMQAdminExt defaultMQAdminExt = null;
    try {
        defaultMQAdminExt = startMQAdminTool(connectConfig);
        ClusterInfo clusterInfo = defaultMQAdminExt.examineBrokerClusterInfo();
        for (BrokerData brokerData : clusterInfo.getBrokerAddrTable().values()) {
            SubscriptionGroupWrapper subscriptionGroupWrapper = defaultMQAdminExt.getAllSubscriptionGroup(brokerData.selectBrokerAddr(), 3000L);
            consumerGroupSet.addAll(subscriptionGroupWrapper.getSubscriptionGroupTable().keySet());
        }
    } catch (Exception e) {
        throw new RuntimeException("fetch all topic  failed", e);
    } finally {
        if (defaultMQAdminExt != null) {
            defaultMQAdminExt.shutdown();
        }
    }
    return consumerGroupSet;
}
Also used : ClusterInfo(org.apache.rocketmq.common.protocol.body.ClusterInfo) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) SubscriptionGroupWrapper(org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

SubscriptionGroupWrapper (org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper)26 Map (java.util.Map)11 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)11 HashMap (java.util.HashMap)9 SubscriptionGroupConfig (org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)9 MQClientException (org.apache.rocketmq.client.exception.MQClientException)7 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)7 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)7 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 KVTable (org.apache.rocketmq.common.protocol.body.KVTable)5 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)5 TopicConfig (org.apache.rocketmq.common.TopicConfig)4 ClusterInfo (org.apache.rocketmq.common.protocol.body.ClusterInfo)4 TopicConfigSerializeWrapper (org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper)4 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)4 Field (java.lang.reflect.Field)3 HashSet (java.util.HashSet)3 List (java.util.List)3 TreeMap (java.util.TreeMap)3