Search in sources :

Example 16 with BrokerConfig

use of org.apache.rocketmq.common.BrokerConfig in project ignite by apache.

the class TestRocketMQServer method startBroker.

/**
 * Starts a test broker.
 *
 * @throws Exception If fails.
 */
private void startBroker() throws Exception {
    BrokerConfig brokerCfg = new BrokerConfig();
    NettyServerConfig nettySrvCfg = new NettyServerConfig();
    MessageStoreConfig storeCfg = new MessageStoreConfig();
    brokerCfg.setBrokerName(TEST_BROKER);
    brokerCfg.setBrokerClusterName(TEST_CLUSTER);
    brokerCfg.setBrokerIP1(TEST_IP);
    brokerCfg.setNamesrvAddr(TEST_IP + ":" + NAME_SERVER_PORT);
    storeCfg.setStorePathRootDir(System.getProperty("java.io.tmpdir") + separator + "store-" + UUID.randomUUID());
    storeCfg.setStorePathCommitLog(System.getProperty("java.io.tmpdir") + separator + "commitlog");
    storeCfg.setHaListenPort(HA_PORT);
    nettySrvCfg.setListenPort(BROKER_PORT);
    broker = new BrokerController(brokerCfg, nettySrvCfg, new NettyClientConfig(), storeCfg);
    broker.initialize();
    broker.start();
    log.info("Started broker [" + TEST_BROKER + "] at " + BROKER_PORT);
}
Also used : MessageStoreConfig(org.apache.rocketmq.store.config.MessageStoreConfig) BrokerConfig(org.apache.rocketmq.common.BrokerConfig) NettyClientConfig(org.apache.rocketmq.remoting.netty.NettyClientConfig) BrokerController(org.apache.rocketmq.broker.BrokerController) NettyServerConfig(org.apache.rocketmq.remoting.netty.NettyServerConfig)

Example 17 with BrokerConfig

use of org.apache.rocketmq.common.BrokerConfig in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class BrokerStartup method createBrokerController.

public static BrokerController createBrokerController(String[] args) {
    // 设置版本
    System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION));
    // Socket发送缓冲区大小
    if (null == System.getProperty(NettySystemConfig.COM_ROCKETMQ_REMOTING_SOCKET_SNDBUF_SIZE)) {
        NettySystemConfig.socketSndbufSize = 131072;
    }
    // Socket接收缓冲区大小
    if (null == System.getProperty(NettySystemConfig.COM_ROCKETMQ_REMOTING_SOCKET_RCVBUF_SIZE)) {
        NettySystemConfig.socketRcvbufSize = 131072;
    }
    try {
        // PackageConflictDetect.detectFastjson();
        // 解析命令行
        Options options = ServerUtil.buildCommandlineOptions(new Options());
        commandLine = ServerUtil.parseCmdLine("mqbroker", args, buildCommandlineOptions(options), new PosixParser());
        if (null == commandLine) {
            System.exit(-1);
        }
        // 初始化配置文件
        final BrokerConfig brokerConfig = new BrokerConfig();
        // 如果我们直接运行的话会报一个错误
        // Please set the ROCKETMQ_HOME variable in your environment to match the location of the RocketMQ installation
        brokerConfig.setRocketmqHome("D:\\\\eclipse-workspace\\\\rocketmq-rocketmq-all-4.1.0-incubating\\\\rocketmq-rocketmq-all-4.1.0-incubating\\\\distribution");
        brokerConfig.setNamesrvAddr("localhost:9876");
        final NettyServerConfig nettyServerConfig = new NettyServerConfig();
        final NettyClientConfig nettyClientConfig = new NettyClientConfig();
        nettyServerConfig.setListenPort(10911);
        // 配置信息
        final MessageStoreConfig messageStoreConfig = new MessageStoreConfig();
        // 如果是slave,修改默认值 - 10
        if (BrokerRole.SLAVE == messageStoreConfig.getBrokerRole()) {
            int ratio = messageStoreConfig.getAccessMessageInMemoryMaxRatio() - 10;
            messageStoreConfig.setAccessMessageInMemoryMaxRatio(ratio);
        }
        // 指定配置文件
        if (commandLine.hasOption('c')) {
            String file = commandLine.getOptionValue('c');
            if (file != null) {
                configFile = file;
                InputStream in = new BufferedInputStream(new FileInputStream(file));
                properties = new Properties();
                properties.load(in);
                properties2SystemEnv(properties);
                MixAll.properties2Object(properties, brokerConfig);
                MixAll.properties2Object(properties, nettyServerConfig);
                MixAll.properties2Object(properties, nettyClientConfig);
                MixAll.properties2Object(properties, messageStoreConfig);
                BrokerPathConfigHelper.setBrokerConfigPath(file);
                in.close();
            }
        }
        MixAll.properties2Object(ServerUtil.commandLine2Properties(commandLine), brokerConfig);
        if (null == brokerConfig.getRocketmqHome()) {
            System.out.printf("Please set the " + MixAll.ROCKETMQ_HOME_ENV + " variable in your environment to match the location of the RocketMQ installation");
            System.exit(-2);
        }
        // 获取namesrv地址
        String namesrvAddr = brokerConfig.getNamesrvAddr();
        if (null != namesrvAddr) {
            try {
                // 看到这里就知道为什么多个以 ; 分隔了
                String[] addrArray = namesrvAddr.split(";");
                for (String addr : addrArray) {
                    // 转换SocketAddress对象
                    RemotingUtil.string2SocketAddress(addr);
                }
            } catch (Exception e) {
                System.out.printf("The Name Server Address[%s] illegal, please set it as follows, \"127.0.0.1:9876;192.168.0.1:9876\"%n", namesrvAddr);
                System.exit(-3);
            }
        }
        // BrokerId事项
        switch(messageStoreConfig.getBrokerRole()) {
            case ASYNC_MASTER:
            case SYNC_MASTER:
                // 知道为什么主是 0
                brokerConfig.setBrokerId(MixAll.MASTER_ID);
                break;
            case SLAVE:
                if (brokerConfig.getBrokerId() <= 0) {
                    // 从必须 > 0
                    System.out.printf("Slave's brokerId must be > 0");
                    System.exit(-3);
                }
                break;
            default:
                break;
        }
        // Master监听Slave请求的端口,默认为服务端口+1
        messageStoreConfig.setHaListenPort(nettyServerConfig.getListenPort() + 1);
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(lc);
        lc.reset();
        configurator.doConfigure(brokerConfig.getRocketmqHome() + "/conf/logback_broker.xml");
        if (commandLine.hasOption('p')) {
            // 打印所以字段
            Logger console = LoggerFactory.getLogger(LoggerName.BROKER_CONSOLE_NAME);
            MixAll.printObjectProperties(console, brokerConfig);
            MixAll.printObjectProperties(console, nettyServerConfig);
            MixAll.printObjectProperties(console, nettyClientConfig);
            MixAll.printObjectProperties(console, messageStoreConfig);
            System.exit(0);
        } else if (commandLine.hasOption('m')) {
            // 打印重要字段
            Logger console = LoggerFactory.getLogger(LoggerName.BROKER_CONSOLE_NAME);
            MixAll.printObjectProperties(console, brokerConfig, true);
            MixAll.printObjectProperties(console, nettyServerConfig, true);
            MixAll.printObjectProperties(console, nettyClientConfig, true);
            MixAll.printObjectProperties(console, messageStoreConfig, true);
            System.exit(0);
        }
        log = LoggerFactory.getLogger(LoggerName.BROKER_LOGGER_NAME);
        MixAll.printObjectProperties(log, brokerConfig);
        MixAll.printObjectProperties(log, nettyServerConfig);
        MixAll.printObjectProperties(log, nettyClientConfig);
        MixAll.printObjectProperties(log, messageStoreConfig);
        final BrokerController controller = new // 
        BrokerController(// 
        brokerConfig, // 
        nettyServerConfig, // 
        nettyClientConfig, messageStoreConfig);
        // remember all configs to prevent discard
        controller.getConfiguration().registerConfig(properties);
        boolean initResult = controller.initialize();
        if (!initResult) {
            controller.shutdown();
            System.exit(-3);
        }
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            private volatile boolean hasShutdown = false;

            private AtomicInteger shutdownTimes = new AtomicInteger(0);

            @Override
            public void run() {
                synchronized (this) {
                    log.info("Shutdown hook was invoked, {}", this.shutdownTimes.incrementAndGet());
                    if (!this.hasShutdown) {
                        this.hasShutdown = true;
                        long beginTime = System.currentTimeMillis();
                        controller.shutdown();
                        long consumingTimeTotal = System.currentTimeMillis() - beginTime;
                        log.info("Shutdown hook over, consuming total time(ms): {}", consumingTimeTotal);
                    }
                }
            }
        }, "ShutdownHook"));
        return controller;
    } catch (Throwable e) {
        e.printStackTrace();
        System.exit(-1);
    }
    return null;
}
Also used : Options(org.apache.commons.cli.Options) BrokerConfig(org.apache.rocketmq.common.BrokerConfig) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PosixParser(org.apache.commons.cli.PosixParser) Properties(java.util.Properties) Logger(org.slf4j.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) FileInputStream(java.io.FileInputStream) MessageStoreConfig(org.apache.rocketmq.store.config.MessageStoreConfig) BufferedInputStream(java.io.BufferedInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NettyClientConfig(org.apache.rocketmq.remoting.netty.NettyClientConfig) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) NettyServerConfig(org.apache.rocketmq.remoting.netty.NettyServerConfig)

Example 18 with BrokerConfig

use of org.apache.rocketmq.common.BrokerConfig in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class CommitLogDispatcherCalcBitMapTest method testDispatch.

@Test
public void testDispatch() {
    BrokerConfig brokerConfig = new BrokerConfig();
    brokerConfig.setEnableCalcFilterBitMap(true);
    ConsumerFilterManager filterManager = ConsumerFilterManagerTest.gen(10, 10);
    CommitLogDispatcherCalcBitMap calcBitMap = new CommitLogDispatcherCalcBitMap(brokerConfig, filterManager);
    for (int i = 0; i < 10; i++) {
        Map<String, String> properties = new HashMap<String, String>(4);
        properties.put("a", String.valueOf(i * 10 + 5));
        String topic = "topic" + i;
        DispatchRequest dispatchRequest = new DispatchRequest(topic, 0, i * 100 + 123, 100, (long) ("tags" + i).hashCode(), System.currentTimeMillis(), i, null, UUID.randomUUID().toString(), 0, 0, properties);
        calcBitMap.dispatch(dispatchRequest);
        assertThat(dispatchRequest.getBitMap()).isNotNull();
        BitsArray bits = BitsArray.create(dispatchRequest.getBitMap());
        Collection<ConsumerFilterData> filterDatas = filterManager.get(topic);
        for (ConsumerFilterData filterData : filterDatas) {
            if (filterManager.getBloomFilter().isHit(filterData.getBloomFilterData(), bits)) {
                try {
                    assertThat((Boolean) filterData.getCompiledExpression().evaluate(new MessageEvaluationContext(properties))).isTrue();
                } catch (Exception e) {
                    e.printStackTrace();
                    assertThat(true).isFalse();
                }
            } else {
                try {
                    assertThat((Boolean) filterData.getCompiledExpression().evaluate(new MessageEvaluationContext(properties))).isFalse();
                } catch (Exception e) {
                    e.printStackTrace();
                    assertThat(true).isFalse();
                }
            }
        }
    }
}
Also used : BrokerConfig(org.apache.rocketmq.common.BrokerConfig) HashMap(java.util.HashMap) BitsArray(org.apache.rocketmq.filter.util.BitsArray) DispatchRequest(org.apache.rocketmq.store.DispatchRequest) Test(org.junit.Test)

Example 19 with BrokerConfig

use of org.apache.rocketmq.common.BrokerConfig in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MessageStoreWithFilterTest method gen.

protected DefaultMessageStore gen(ConsumerFilterManager filterManager) throws Exception {
    MessageStoreConfig messageStoreConfig = buildStoreConfig(commitLogFileSize, cqFileSize, true, cqExtFileSize);
    BrokerConfig brokerConfig = new BrokerConfig();
    brokerConfig.setEnableCalcFilterBitMap(true);
    brokerConfig.setMaxErrorRateOfBloomFilter(20);
    brokerConfig.setExpectConsumerNumUseFilter(64);
    DefaultMessageStore master = new DefaultMessageStore(messageStoreConfig, new BrokerStatsManager(brokerConfig.getBrokerClusterName()), new MessageArrivingListener() {

        @Override
        public void arriving(String topic, int queueId, long logicOffset, long tagsCode, long msgStoreTime, byte[] filterBitMap, Map<String, String> properties) {
        // System.out.println(String.format("Msg coming: %s, %d, %d, %d",
        // topic, queueId, logicOffset, tagsCode));
        }
    }, brokerConfig);
    master.getDispatcherList().addFirst(new CommitLogDispatcher() {

        @Override
        public void dispatch(DispatchRequest request) {
            try {
            // System.out.println(String.format("offset:%d, bitMap:%s", request.getCommitLogOffset(),
            // BitsArray.create(request.getBitMap()).toString()));
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    });
    master.getDispatcherList().addFirst(new CommitLogDispatcherCalcBitMap(brokerConfig, filterManager));
    assertThat(master.load()).isTrue();
    master.start();
    return master;
}
Also used : BrokerStatsManager(org.apache.rocketmq.store.stats.BrokerStatsManager) BrokerConfig(org.apache.rocketmq.common.BrokerConfig) DefaultMessageStore(org.apache.rocketmq.store.DefaultMessageStore) DispatchRequest(org.apache.rocketmq.store.DispatchRequest) MessageStoreConfig(org.apache.rocketmq.store.config.MessageStoreConfig) MessageArrivingListener(org.apache.rocketmq.store.MessageArrivingListener) CommitLogDispatcher(org.apache.rocketmq.store.CommitLogDispatcher)

Example 20 with BrokerConfig

use of org.apache.rocketmq.common.BrokerConfig in project rocketmq by apache.

the class CommitLogDispatcherCalcBitMapTest method testDispatch_blankFilterData.

@Test
public void testDispatch_blankFilterData() {
    BrokerConfig brokerConfig = new BrokerConfig();
    brokerConfig.setEnableCalcFilterBitMap(true);
    ConsumerFilterManager filterManager = new ConsumerFilterManager();
    CommitLogDispatcherCalcBitMap calcBitMap = new CommitLogDispatcherCalcBitMap(brokerConfig, filterManager);
    for (int i = 0; i < 10; i++) {
        Map<String, String> properties = new HashMap<String, String>(4);
        properties.put("a", String.valueOf(i * 10 + 5));
        String topic = "topic" + i;
        DispatchRequest dispatchRequest = new DispatchRequest(topic, 0, i * 100 + 123, 100, (long) ("tags" + i).hashCode(), System.currentTimeMillis(), i, null, UUID.randomUUID().toString(), 0, 0, properties);
        calcBitMap.dispatch(dispatchRequest);
        assertThat(dispatchRequest.getBitMap()).isNull();
    }
}
Also used : BrokerConfig(org.apache.rocketmq.common.BrokerConfig) HashMap(java.util.HashMap) DispatchRequest(org.apache.rocketmq.store.DispatchRequest) Test(org.junit.Test)

Aggregations

BrokerConfig (org.apache.rocketmq.common.BrokerConfig)28 MessageStoreConfig (org.apache.rocketmq.store.config.MessageStoreConfig)22 NettyClientConfig (org.apache.rocketmq.remoting.netty.NettyClientConfig)13 NettyServerConfig (org.apache.rocketmq.remoting.netty.NettyServerConfig)13 Test (org.junit.Test)11 BrokerController (org.apache.rocketmq.broker.BrokerController)9 DispatchRequest (org.apache.rocketmq.store.DispatchRequest)8 HashMap (java.util.HashMap)6 BrokerStatsManager (org.apache.rocketmq.store.stats.BrokerStatsManager)6 MQClientException (org.apache.rocketmq.client.exception.MQClientException)5 BitsArray (org.apache.rocketmq.filter.util.BitsArray)4 LoggerContext (ch.qos.logback.classic.LoggerContext)2 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)2 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Properties (java.util.Properties)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Options (org.apache.commons.cli.Options)2