public TopicConfigManager(BrokerController brokerController) {
    this.brokerController = brokerController;

    // MixAll.DEFAULT_TOPIC
    TopicConfig topicConfig = new TopicConfig(MixAll.DEFAULT_TOPIC);
    topicConfig.setReadQueueNums(
        this.brokerController.getBrokerConfig().getDefaultTopicQueueNums());
    topicConfig.setWriteQueueNums(
        this.brokerController.getBrokerConfig().getDefaultTopicQueueNums());
    int perm =
        this.brokerController.getBrokerConfig().isAutoCreateTopicEnable()
            ? PermName.PERM_INHERIT
            : 0;
    perm |= PermName.PERM_READ | PermName.PERM_WRITE;
    topicConfig.setPerm(perm);
    this.topicConfigTable.put(topicConfig.getTopicName(), topicConfig);

    // MixAll.SELF_TEST_TOPIC
    topicConfig = new TopicConfig(MixAll.SELF_TEST_TOPIC);
    topicConfig.setReadQueueNums(1);
    topicConfig.setWriteQueueNums(1);
    this.topicConfigTable.put(topicConfig.getTopicName(), topicConfig);

    // 集群名字
    topicConfig = new TopicConfig(this.brokerController.getBrokerConfig().getBrokerClusterName());
    perm = PermName.PERM_INHERIT;
    if (this.brokerController.getBrokerConfig().isClusterTopicEnable()) {
      perm |= PermName.PERM_READ | PermName.PERM_WRITE;
    }
    topicConfig.setPerm(perm);
    this.topicConfigTable.put(topicConfig.getTopicName(), topicConfig);
  }
  private RemotingCommand updateAndCreateTopic(ChannelHandlerContext ctx, RemotingCommand request)
      throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final CreateTopicRequestHeader requestHeader =
        (CreateTopicRequestHeader)
            request.decodeCommandCustomHeader(CreateTopicRequestHeader.class);
    log.info(
        "updateAndCreateTopic called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));

    // Topic名字是否与保留字段冲突
    if (requestHeader
        .getTopic()
        .equals(this.brokerController.getBrokerConfig().getBrokerClusterName())) {
      String errorMsg =
          "the topic[" + requestHeader.getTopic() + "] is conflict with system reserved words.";
      log.warn(errorMsg);
      response.setCode(ResponseCode.SYSTEM_ERROR_VALUE);
      response.setRemark(errorMsg);
      return response;
    }

    TopicConfig topicConfig = new TopicConfig(requestHeader.getTopic());
    topicConfig.setReadQueueNums(requestHeader.getReadQueueNums());
    topicConfig.setWriteQueueNums(requestHeader.getWriteQueueNums());
    topicConfig.setTopicFilterType(requestHeader.getTopicFilterTypeEnum());
    topicConfig.setPerm(requestHeader.getPerm());

    this.brokerController.getTopicConfigManager().updateTopicConfig(topicConfig);

    response.setCode(ResponseCode.SUCCESS_VALUE);
    response.setRemark(null);
    return response;
  }
  public TopicConfig createTopicInSendMessageBackMethod( //
      final String topic, //
      final int clientDefaultTopicQueueNums) {
    TopicConfig topicConfig = this.topicConfigTable.get(topic);
    if (topicConfig != null) return topicConfig;

    boolean createNew = false;

    try {
      if (this.lockTopicConfigTable.tryLock(LockTimeoutMillis, TimeUnit.MILLISECONDS)) {
        try {
          topicConfig = this.topicConfigTable.get(topic);
          if (topicConfig != null) return topicConfig;

          topicConfig = new TopicConfig(topic);
          topicConfig.setReadQueueNums(clientDefaultTopicQueueNums);
          topicConfig.setWriteQueueNums(clientDefaultTopicQueueNums);

          log.info("create new topic {}", topicConfig);
          this.topicConfigTable.put(topic, topicConfig);
          createNew = true;
          this.persist();
        } finally {
          this.lockTopicConfigTable.unlock();
        }
      }
    } catch (InterruptedException e) {
      log.error("createTopicInSendMessageBackMethod exception", e);
    }

    if (createNew) {
      this.brokerController.registerBrokerAll();
    }

    return topicConfig;
  }
  @Override
  public void execute(final CommandLine commandLine, final Options options, RPCHook rpcHook) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
      TopicConfig topicConfig = new TopicConfig();
      topicConfig.setReadQueueNums(8);
      topicConfig.setWriteQueueNums(8);
      topicConfig.setTopicName(commandLine.getOptionValue('t').trim());

      // readQueueNums
      if (commandLine.hasOption('r')) {
        topicConfig.setReadQueueNums(Integer.parseInt(commandLine.getOptionValue('r').trim()));
      }

      // writeQueueNums
      if (commandLine.hasOption('w')) {
        topicConfig.setWriteQueueNums(Integer.parseInt(commandLine.getOptionValue('w').trim()));
      }

      // perm
      if (commandLine.hasOption('p')) {
        topicConfig.setPerm(Integer.parseInt(commandLine.getOptionValue('p').trim()));
      }

      boolean isUnit = false;
      if (commandLine.hasOption('u')) {
        isUnit = Boolean.parseBoolean(commandLine.getOptionValue('u').trim());
      }

      boolean isCenterSync = false;
      if (commandLine.hasOption('s')) {
        isCenterSync = Boolean.parseBoolean(commandLine.getOptionValue('s').trim());
      }

      int topicCenterSync = TopicSysFlag.buildSysFlag(isUnit, isCenterSync);
      topicConfig.setTopicSysFlag(topicCenterSync);

      boolean isOrder = false;
      if (commandLine.hasOption('o')) {
        isOrder = Boolean.parseBoolean(commandLine.getOptionValue('o').trim());
      }
      topicConfig.setOrder(isOrder);

      if (commandLine.hasOption('b')) {
        String addr = commandLine.getOptionValue('b').trim();

        defaultMQAdminExt.start();
        defaultMQAdminExt.createAndUpdateTopicConfig(addr, topicConfig);

        if (isOrder) {
          String brokerName = CommandUtil.fetchBrokerNameByAddr(defaultMQAdminExt, addr);
          String orderConf = brokerName + ":" + topicConfig.getWriteQueueNums();
          defaultMQAdminExt.createOrUpdateOrderConf(topicConfig.getTopicName(), orderConf, false);
          System.out.println(
              String.format(
                  "set broker orderConf. isOrder=%s, orderConf=[%s]",
                  isOrder, orderConf.toString()));
        }
        System.out.printf("create topic to %s success.%n", addr);
        System.out.println(topicConfig);
        return;

      } else if (commandLine.hasOption('c')) {
        String clusterName = commandLine.getOptionValue('c').trim();

        defaultMQAdminExt.start();

        Set<String> masterSet =
            CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName);
        for (String addr : masterSet) {
          defaultMQAdminExt.createAndUpdateTopicConfig(addr, topicConfig);
          System.out.printf("create topic to %s success.%n", addr);
        }

        if (isOrder) {
          Set<String> brokerNameSet =
              CommandUtil.fetchBrokerNameByClusterName(defaultMQAdminExt, clusterName);
          StringBuilder orderConf = new StringBuilder();
          String splitor = "";
          for (String s : brokerNameSet) {
            orderConf.append(splitor).append(s).append(":").append(topicConfig.getWriteQueueNums());
            splitor = ";";
          }
          defaultMQAdminExt.createOrUpdateOrderConf(
              topicConfig.getTopicName(), orderConf.toString(), true);
          System.out.println(
              String.format(
                  "set cluster orderConf. isOrder=%s, orderConf=[%s]",
                  isOrder, orderConf.toString()));
        }

        System.out.println(topicConfig);
        return;
      }

      ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      defaultMQAdminExt.shutdown();
    }
  }
  /** 发消息时,如果Topic不存在,尝试创建 */
  public TopicConfig createTopicInSendMessageMethod(
      final String topic,
      final String defaultTopic,
      final String remoteAddress,
      final int clientDefaultTopicQueueNums) {
    TopicConfig topicConfig = null;
    boolean createNew = false;

    try {
      if (this.lockTopicConfigTable.tryLock(LockTimeoutMillis, TimeUnit.MILLISECONDS)) {
        try {
          topicConfig = this.topicConfigTable.get(topic);
          if (topicConfig != null) return topicConfig;

          TopicConfig defaultTopicConfig = this.topicConfigTable.get(defaultTopic);
          if (defaultTopicConfig != null) {
            if (PermName.isInherited(defaultTopicConfig.getPerm())) {
              topicConfig = new TopicConfig(topic);

              int queueNums =
                  clientDefaultTopicQueueNums > defaultTopicConfig.getWriteQueueNums()
                      ? defaultTopicConfig.getWriteQueueNums()
                      : clientDefaultTopicQueueNums;

              if (queueNums < 0) {
                queueNums = 0;
              }

              topicConfig.setReadQueueNums(queueNums);
              topicConfig.setWriteQueueNums(queueNums);
              int perm = defaultTopicConfig.getPerm();
              perm &= ~PermName.PERM_INHERIT;
              topicConfig.setPerm(perm);
              topicConfig.setTopicFilterType(defaultTopicConfig.getTopicFilterType());
            } else {
              log.warn(
                  "create new topic failed, because the default topic["
                      + defaultTopic
                      + "] no perm, "
                      + defaultTopicConfig.getPerm()
                      + " producer: "
                      + remoteAddress);
            }
          } else {
            log.warn(
                "create new topic failed, because the default topic["
                    + defaultTopic
                    + "] not exist."
                    + " producer: "
                    + remoteAddress);
          }

          if (topicConfig != null) {
            log.info(
                "create new topic by default topic["
                    + defaultTopic
                    + "], "
                    + topicConfig
                    + " producer: "
                    + remoteAddress);

            this.topicConfigTable.put(topic, topicConfig);

            createNew = true;

            this.persist();
          }
        } finally {
          this.lockTopicConfigTable.unlock();
        }
      }
    } catch (InterruptedException e) {
      log.error("createTopicInSendMessageMethod exception", e);
    }

    if (createNew) {
      this.brokerController.registerBrokerAll();
    }

    return topicConfig;
  }