You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/05/17 07:40:35 UTC
[GitHub] [rocketmq] hzh0425 opened a new pull request, #4333: [Summer of code] Stand alone a new controller module
hzh0425 opened a new pull request, #4333:
URL: https://github.com/apache/rocketmq/pull/4333
## What is the purpose of the change
tracking issue: https://github.com/apache/rocketmq/issues/4330
## Brief changelog
Add a new module -- controller.
Make the controller independent from name-srv and can be deployed independently
## Verifying this change
Follow this checklist to help us incorporate your contribution quickly and easily. Notice, `it would be helpful if you could finish the following 5 checklist(the last one is not necessary)before request the community to review your PR`.
- [x] Make sure there is a [Github issue](https://github.com/apache/rocketmq/issues) filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
- [x] Format the pull request title like `[ISSUE #123] Fix UnknownException when host config not exist`. Each commit in the pull request should have a meaningful subject line and body.
- [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [x] Write necessary unit-test(over 80% coverage) to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in [test module](https://github.com/apache/rocketmq/tree/master/test).
- [x] Run `mvn -B clean apache-rat:check findbugs:findbugs checkstyle:checkstyle` to make sure basic checks pass. Run `mvn clean install -DskipITs` to make sure unit-test pass. Run `mvn clean test-compile failsafe:integration-test` to make sure integration-test pass.
- [ ] If this contribution is large, please file an [Apache Individual Contributor License Agreement](http://www.apache.org/licenses/#clas).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [rocketmq] codecov-commenter commented on pull request #4333: [Summer of code] Stand alone a new controller module
Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on PR #4333:
URL: https://github.com/apache/rocketmq/pull/4333#issuecomment-1129874738
# [Codecov](https://codecov.io/gh/apache/rocketmq/pull/4333?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
> Merging [#4333](https://codecov.io/gh/apache/rocketmq/pull/4333?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (7bfc685) into [5.0.0-beta-dledger-controller](https://codecov.io/gh/apache/rocketmq/commit/2f6da9e77b5dded080a746b8c10546cf457baf6d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (2f6da9e) will **decrease** coverage by `0.06%`.
> The diff coverage is `47.47%`.
```diff
@@ Coverage Diff @@
## 5.0.0-beta-dledger-controller #4333 +/- ##
===================================================================
- Coverage 43.50% 43.43% -0.07%
- Complexity 6271 6305 +34
===================================================================
Files 829 835 +6
Lines 58937 59199 +262
Branches 8046 8068 +22
===================================================================
+ Hits 25639 25715 +76
- Misses 29974 30153 +179
- Partials 3324 3331 +7
```
| [Impacted Files](https://codecov.io/gh/apache/rocketmq/pull/4333?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
|---|---|---|
| [...ava/org/apache/rocketmq/common/BrokerAddrInfo.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29tbW9uL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9yb2NrZXRtcS9jb21tb24vQnJva2VyQWRkckluZm8uamF2YQ==) | `0.00% <0.00%> (ø)` | |
| [...ache/rocketmq/common/namesrv/ControllerConfig.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29tbW9uL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9yb2NrZXRtcS9jb21tb24vbmFtZXNydi9Db250cm9sbGVyQ29uZmlnLmphdmE=) | `0.00% <0.00%> (ø)` | |
| [...amesrv/controller/BrokerRegisterRequestHeader.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29tbW9uL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9yb2NrZXRtcS9jb21tb24vcHJvdG9jb2wvaGVhZGVyL25hbWVzcnYvY29udHJvbGxlci9Ccm9rZXJSZWdpc3RlclJlcXVlc3RIZWFkZXIuamF2YQ==) | `0.00% <0.00%> (ø)` | |
| [.../apache/rocketmq/controller/ControllerStartup.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29udHJvbGxlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcm9ja2V0bXEvY29udHJvbGxlci9Db250cm9sbGVyU3RhcnR1cC5qYXZh) | `0.00% <0.00%> (ø)` | |
| [...controller/impl/DledgerControllerStateMachine.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29udHJvbGxlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcm9ja2V0bXEvY29udHJvbGxlci9pbXBsL0RsZWRnZXJDb250cm9sbGVyU3RhdGVNYWNoaW5lLmphdmE=) | `85.00% <ø> (ø)` | |
| [.../controller/impl/event/AlterSyncStateSetEvent.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29udHJvbGxlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcm9ja2V0bXEvY29udHJvbGxlci9pbXBsL2V2ZW50L0FsdGVyU3luY1N0YXRlU2V0RXZlbnQuamF2YQ==) | `100.00% <ø> (ø)` | |
| [...etmq/controller/impl/event/ApplyBrokerIdEvent.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29udHJvbGxlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcm9ja2V0bXEvY29udHJvbGxlci9pbXBsL2V2ZW50L0FwcGx5QnJva2VySWRFdmVudC5qYXZh) | `100.00% <ø> (ø)` | |
| [...cketmq/controller/impl/event/ControllerResult.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29udHJvbGxlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcm9ja2V0bXEvY29udHJvbGxlci9pbXBsL2V2ZW50L0NvbnRyb2xsZXJSZXN1bHQuamF2YQ==) | `78.26% <ø> (ø)` | |
| [...cketmq/controller/impl/event/ElectMasterEvent.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29udHJvbGxlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcm9ja2V0bXEvY29udHJvbGxlci9pbXBsL2V2ZW50L0VsZWN0TWFzdGVyRXZlbnQuamF2YQ==) | `100.00% <ø> (ø)` | |
| [...ocketmq/controller/impl/event/EventSerializer.java](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29udHJvbGxlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcm9ja2V0bXEvY29udHJvbGxlci9pbXBsL2V2ZW50L0V2ZW50U2VyaWFsaXplci5qYXZh) | `71.42% <ø> (ø)` | |
| ... and [27 more](https://codecov.io/gh/apache/rocketmq/pull/4333/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/rocketmq/pull/4333?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/rocketmq/pull/4333?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [2f6da9e...7bfc685](https://codecov.io/gh/apache/rocketmq/pull/4333?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [rocketmq] coveralls commented on pull request #4333: [Summer of code] Stand alone a new controller module
Posted by GitBox <gi...@apache.org>.
coveralls commented on PR #4333:
URL: https://github.com/apache/rocketmq/pull/4333#issuecomment-1128577054
[](https://coveralls.io/builds/49182841)
Coverage increased (+0.06%) to 47.804% when pulling **4fce05c8d7d3e3e88697dcf9a92f0420b058af16 on hzh0425:feature/Standalone_controller_module** into **2f6da9e77b5dded080a746b8c10546cf457baf6d on apache:5.0.0-beta-dledger-controller**.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [rocketmq] RongtongJin commented on a diff in pull request #4333: [Summer of code] Stand alone a new controller module
Posted by GitBox <gi...@apache.org>.
RongtongJin commented on code in PR #4333:
URL: https://github.com/apache/rocketmq/pull/4333#discussion_r875592406
##########
controller/src/main/java/org/apache/rocketmq/controller/impl/DefaultBrokerHeartbeatManager.java:
##########
@@ -0,0 +1,225 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.controller.impl;
+
+import io.netty.channel.Channel;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.rocketmq.common.ThreadFactoryImpl;
+import org.apache.rocketmq.common.constant.LoggerName;
+import org.apache.rocketmq.common.namesrv.ControllerConfig;
+import org.apache.rocketmq.controller.BrokerHeartbeatManager;
+import org.apache.rocketmq.logging.InternalLogger;
+import org.apache.rocketmq.logging.InternalLoggerFactory;
+import org.apache.rocketmq.remoting.common.RemotingUtil;
+
+public class DefaultBrokerHeartbeatManager implements BrokerHeartbeatManager {
+ private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.CONTROLLER_LOGGER_NAME);
+ private static final long DEFAULT_BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2;
+ private final ScheduledExecutorService scheduledService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("DefaultBrokerHeartbeatManager_scheduledService_"));
+ private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactoryImpl("DefaultBrokerHeartbeatManager_executorService_"));
+
+ private final ControllerConfig controllerConfig;
+ private final Map<BrokerAddrInfo/* brokerAddr */, BrokerLiveInfo> brokerLiveTable;
+ private final List<BrokerLifecycleListener> brokerLifecycleListeners;
+
+ public DefaultBrokerHeartbeatManager(final ControllerConfig controllerConfig) {
+ this.controllerConfig = controllerConfig;
+ this.brokerLiveTable = new ConcurrentHashMap<>(256);
+ this.brokerLifecycleListeners = new ArrayList<>();
+ }
+
+ @Override
+ public void start() {
+ this.scheduledService.scheduleAtFixedRate(this::scanNotActiveBroker, 2000, this.controllerConfig.getScanNotActiveBrokerInterval(), TimeUnit.MILLISECONDS);
+ }
+
+ @Override
+ public void shutdown() {
+ this.scheduledService.shutdown();
+ this.executor.shutdown();
+ }
+
+ public void scanNotActiveBroker() {
+ try {
+ log.info("start scanNotActiveBroker");
+ final Iterator<Map.Entry<BrokerAddrInfo, BrokerLiveInfo>> iterator = this.brokerLiveTable.entrySet().iterator();
+ while (iterator.hasNext()) {
+ final Map.Entry<BrokerAddrInfo, BrokerLiveInfo> next = iterator.next();
+ long last = next.getValue().lastUpdateTimestamp;
+ long timeoutMillis = next.getValue().heartbeatTimeoutMillis;
+ if ((last + timeoutMillis) < System.currentTimeMillis()) {
+ final Channel channel = next.getValue().channel;
+ if (channel != null) {
+ RemotingUtil.closeChannel(channel);
+ }
+ iterator.remove();
+ this.executor.submit(() ->
+ notifyBrokerInActive(next.getValue().brokerName, next.getKey().brokerAddr, next.getValue().brokerId));
+ log.warn("The broker channel expired, {} {}ms", next.getKey(), timeoutMillis);
+ }
+ }
+ } catch (Exception e) {
+ log.error("scanNotActiveBroker exception", e);
+ }
+ }
+
+ private void notifyBrokerInActive(String brokerName, String brokerAddr, Long brokerId) {
+ for (BrokerLifecycleListener listener : this.brokerLifecycleListeners) {
+ listener.onBrokerInactive(brokerName, brokerAddr, brokerId);
+ }
+ }
+
+ @Override
+ public void addBrokerLifecycleListener(BrokerLifecycleListener listener) {
+ this.brokerLifecycleListeners.add(listener);
+ }
+
+ @Override
+ public void registerBroker(String clusterName, String brokerName, String brokerAddr,
+ long brokerId, Long timeoutMillis, Channel channel) {
+ final BrokerAddrInfo addrInfo = new BrokerAddrInfo(clusterName, brokerAddr);
+ final BrokerLiveInfo prevBrokerLiveInfo = this.brokerLiveTable.put(addrInfo,
+ new BrokerLiveInfo(brokerName,
+ brokerId,
+ System.currentTimeMillis(),
+ timeoutMillis == null ? DEFAULT_BROKER_CHANNEL_EXPIRED_TIME : timeoutMillis,
+ channel));
+ if (prevBrokerLiveInfo == null) {
+ log.info("new broker registered, {}, brokerId:{}", addrInfo, brokerId);
+ }
+ }
+
+ @Override
+ public void onBrokerHeartbeat(String clusterName, String brokerAddr) {
+ BrokerAddrInfo addrInfo = new BrokerAddrInfo(clusterName, brokerAddr);
+ BrokerLiveInfo prev = this.brokerLiveTable.get(addrInfo);
+ if (prev != null) {
+ prev.lastUpdateTimestamp = System.currentTimeMillis();
+ }
+ }
+
+ @Override
+ public void onBrokerChannelClose(Channel channel) {
+ synchronized (this) {
+ for (Map.Entry<BrokerAddrInfo, BrokerLiveInfo> entry : this.brokerLiveTable.entrySet()) {
+ if (entry.getValue().channel == channel) {
+ this.executor.submit(() ->
+ notifyBrokerInActive(entry.getValue().brokerName, entry.getKey().brokerAddr, entry.getValue().brokerId));
+ break;
+ }
+ }
+ }
+ }
+
+ @Override
+ public boolean isBrokerActive(String clusterName, String brokerAddr) {
+ final BrokerLiveInfo info = this.brokerLiveTable.get(new BrokerAddrInfo(clusterName, brokerAddr));
+ if (info != null) {
+ long last = info.lastUpdateTimestamp;
+ long timeoutMillis = info.heartbeatTimeoutMillis;
+ return (last + timeoutMillis) >= System.currentTimeMillis();
+ }
+ return false;
+ }
+
+ static class BrokerLiveInfo {
+ private final String brokerName;
+ private final long brokerId;
+ private final long heartbeatTimeoutMillis;
+ private final Channel channel;
+ private long lastUpdateTimestamp;
+
+ public BrokerLiveInfo(String brokerName, long brokerId, long lastUpdateTimestamp, long heartbeatTimeoutMillis,
+ Channel channel) {
+ this.brokerName = brokerName;
+ this.brokerId = brokerId;
+ this.lastUpdateTimestamp = lastUpdateTimestamp;
+ this.heartbeatTimeoutMillis = heartbeatTimeoutMillis;
+ this.channel = channel;
+ }
+
+ @Override public String toString() {
+ return "BrokerLiveInfo{" +
+ "brokerName='" + brokerName + '\'' +
+ ", brokerId=" + brokerId +
+ ", lastUpdateTimestamp=" + lastUpdateTimestamp +
+ ", heartbeatTimeoutMillis=" + heartbeatTimeoutMillis +
+ ", channel=" + channel +
+ '}';
+ }
+ }
+
+ /**
+ * broker address information
+ */
+ static class BrokerAddrInfo {
Review Comment:
可以把这个类提取到common模块,controller和namesrv一起复用
##########
controller/src/main/java/org/apache/rocketmq/controller/impl/DefaultBrokerHeartbeatManager.java:
##########
@@ -0,0 +1,225 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.controller.impl;
+
+import io.netty.channel.Channel;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.rocketmq.common.ThreadFactoryImpl;
+import org.apache.rocketmq.common.constant.LoggerName;
+import org.apache.rocketmq.common.namesrv.ControllerConfig;
+import org.apache.rocketmq.controller.BrokerHeartbeatManager;
+import org.apache.rocketmq.logging.InternalLogger;
+import org.apache.rocketmq.logging.InternalLoggerFactory;
+import org.apache.rocketmq.remoting.common.RemotingUtil;
+
+public class DefaultBrokerHeartbeatManager implements BrokerHeartbeatManager {
+ private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.CONTROLLER_LOGGER_NAME);
+ private static final long DEFAULT_BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2;
+ private final ScheduledExecutorService scheduledService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("DefaultBrokerHeartbeatManager_scheduledService_"));
+ private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactoryImpl("DefaultBrokerHeartbeatManager_executorService_"));
+
+ private final ControllerConfig controllerConfig;
+ private final Map<BrokerAddrInfo/* brokerAddr */, BrokerLiveInfo> brokerLiveTable;
+ private final List<BrokerLifecycleListener> brokerLifecycleListeners;
+
+ public DefaultBrokerHeartbeatManager(final ControllerConfig controllerConfig) {
+ this.controllerConfig = controllerConfig;
+ this.brokerLiveTable = new ConcurrentHashMap<>(256);
+ this.brokerLifecycleListeners = new ArrayList<>();
+ }
+
+ @Override
+ public void start() {
+ this.scheduledService.scheduleAtFixedRate(this::scanNotActiveBroker, 2000, this.controllerConfig.getScanNotActiveBrokerInterval(), TimeUnit.MILLISECONDS);
+ }
+
+ @Override
+ public void shutdown() {
+ this.scheduledService.shutdown();
+ this.executor.shutdown();
+ }
+
+ public void scanNotActiveBroker() {
+ try {
+ log.info("start scanNotActiveBroker");
+ final Iterator<Map.Entry<BrokerAddrInfo, BrokerLiveInfo>> iterator = this.brokerLiveTable.entrySet().iterator();
+ while (iterator.hasNext()) {
+ final Map.Entry<BrokerAddrInfo, BrokerLiveInfo> next = iterator.next();
+ long last = next.getValue().lastUpdateTimestamp;
+ long timeoutMillis = next.getValue().heartbeatTimeoutMillis;
+ if ((last + timeoutMillis) < System.currentTimeMillis()) {
+ final Channel channel = next.getValue().channel;
+ if (channel != null) {
+ RemotingUtil.closeChannel(channel);
+ }
+ iterator.remove();
+ this.executor.submit(() ->
+ notifyBrokerInActive(next.getValue().brokerName, next.getKey().brokerAddr, next.getValue().brokerId));
+ log.warn("The broker channel expired, {} {}ms", next.getKey(), timeoutMillis);
+ }
+ }
+ } catch (Exception e) {
+ log.error("scanNotActiveBroker exception", e);
+ }
+ }
+
+ private void notifyBrokerInActive(String brokerName, String brokerAddr, Long brokerId) {
+ for (BrokerLifecycleListener listener : this.brokerLifecycleListeners) {
+ listener.onBrokerInactive(brokerName, brokerAddr, brokerId);
+ }
+ }
+
+ @Override
+ public void addBrokerLifecycleListener(BrokerLifecycleListener listener) {
+ this.brokerLifecycleListeners.add(listener);
+ }
+
+ @Override
+ public void registerBroker(String clusterName, String brokerName, String brokerAddr,
+ long brokerId, Long timeoutMillis, Channel channel) {
+ final BrokerAddrInfo addrInfo = new BrokerAddrInfo(clusterName, brokerAddr);
+ final BrokerLiveInfo prevBrokerLiveInfo = this.brokerLiveTable.put(addrInfo,
+ new BrokerLiveInfo(brokerName,
+ brokerId,
+ System.currentTimeMillis(),
+ timeoutMillis == null ? DEFAULT_BROKER_CHANNEL_EXPIRED_TIME : timeoutMillis,
+ channel));
+ if (prevBrokerLiveInfo == null) {
+ log.info("new broker registered, {}, brokerId:{}", addrInfo, brokerId);
+ }
+ }
+
+ @Override
+ public void onBrokerHeartbeat(String clusterName, String brokerAddr) {
+ BrokerAddrInfo addrInfo = new BrokerAddrInfo(clusterName, brokerAddr);
+ BrokerLiveInfo prev = this.brokerLiveTable.get(addrInfo);
+ if (prev != null) {
+ prev.lastUpdateTimestamp = System.currentTimeMillis();
+ }
+ }
+
+ @Override
+ public void onBrokerChannelClose(Channel channel) {
+ synchronized (this) {
+ for (Map.Entry<BrokerAddrInfo, BrokerLiveInfo> entry : this.brokerLiveTable.entrySet()) {
+ if (entry.getValue().channel == channel) {
+ this.executor.submit(() ->
+ notifyBrokerInActive(entry.getValue().brokerName, entry.getKey().brokerAddr, entry.getValue().brokerId));
+ break;
+ }
+ }
+ }
+ }
+
+ @Override
+ public boolean isBrokerActive(String clusterName, String brokerAddr) {
+ final BrokerLiveInfo info = this.brokerLiveTable.get(new BrokerAddrInfo(clusterName, brokerAddr));
+ if (info != null) {
+ long last = info.lastUpdateTimestamp;
+ long timeoutMillis = info.heartbeatTimeoutMillis;
+ return (last + timeoutMillis) >= System.currentTimeMillis();
+ }
+ return false;
+ }
+
+ static class BrokerLiveInfo {
+ private final String brokerName;
+ private final long brokerId;
Review Comment:
这里为什么需要brokerId?
##########
controller/src/main/java/org/apache/rocketmq/controller/impl/DefaultBrokerHeartbeatManager.java:
##########
@@ -0,0 +1,225 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.controller.impl;
+
+import io.netty.channel.Channel;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.rocketmq.common.ThreadFactoryImpl;
+import org.apache.rocketmq.common.constant.LoggerName;
+import org.apache.rocketmq.common.namesrv.ControllerConfig;
+import org.apache.rocketmq.controller.BrokerHeartbeatManager;
+import org.apache.rocketmq.logging.InternalLogger;
+import org.apache.rocketmq.logging.InternalLoggerFactory;
+import org.apache.rocketmq.remoting.common.RemotingUtil;
+
+public class DefaultBrokerHeartbeatManager implements BrokerHeartbeatManager {
+ private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.CONTROLLER_LOGGER_NAME);
+ private static final long DEFAULT_BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2;
Review Comment:
The default time is too long. In nameserver, the registration interval is 30s by default, so the timeout time is 120s
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [rocketmq] hzh0425 commented on a diff in pull request #4333: [Summer of code] Stand alone a new controller module
Posted by GitBox <gi...@apache.org>.
hzh0425 commented on code in PR #4333:
URL: https://github.com/apache/rocketmq/pull/4333#discussion_r875717860
##########
controller/src/main/java/org/apache/rocketmq/controller/impl/DefaultBrokerHeartbeatManager.java:
##########
@@ -0,0 +1,225 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.controller.impl;
+
+import io.netty.channel.Channel;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.rocketmq.common.ThreadFactoryImpl;
+import org.apache.rocketmq.common.constant.LoggerName;
+import org.apache.rocketmq.common.namesrv.ControllerConfig;
+import org.apache.rocketmq.controller.BrokerHeartbeatManager;
+import org.apache.rocketmq.logging.InternalLogger;
+import org.apache.rocketmq.logging.InternalLoggerFactory;
+import org.apache.rocketmq.remoting.common.RemotingUtil;
+
+public class DefaultBrokerHeartbeatManager implements BrokerHeartbeatManager {
+ private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.CONTROLLER_LOGGER_NAME);
+ private static final long DEFAULT_BROKER_CHANNEL_EXPIRED_TIME = 1000 * 60 * 2;
+ private final ScheduledExecutorService scheduledService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("DefaultBrokerHeartbeatManager_scheduledService_"));
+ private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactoryImpl("DefaultBrokerHeartbeatManager_executorService_"));
+
+ private final ControllerConfig controllerConfig;
+ private final Map<BrokerAddrInfo/* brokerAddr */, BrokerLiveInfo> brokerLiveTable;
+ private final List<BrokerLifecycleListener> brokerLifecycleListeners;
+
+ public DefaultBrokerHeartbeatManager(final ControllerConfig controllerConfig) {
+ this.controllerConfig = controllerConfig;
+ this.brokerLiveTable = new ConcurrentHashMap<>(256);
+ this.brokerLifecycleListeners = new ArrayList<>();
+ }
+
+ @Override
+ public void start() {
+ this.scheduledService.scheduleAtFixedRate(this::scanNotActiveBroker, 2000, this.controllerConfig.getScanNotActiveBrokerInterval(), TimeUnit.MILLISECONDS);
+ }
+
+ @Override
+ public void shutdown() {
+ this.scheduledService.shutdown();
+ this.executor.shutdown();
+ }
+
+ public void scanNotActiveBroker() {
+ try {
+ log.info("start scanNotActiveBroker");
+ final Iterator<Map.Entry<BrokerAddrInfo, BrokerLiveInfo>> iterator = this.brokerLiveTable.entrySet().iterator();
+ while (iterator.hasNext()) {
+ final Map.Entry<BrokerAddrInfo, BrokerLiveInfo> next = iterator.next();
+ long last = next.getValue().lastUpdateTimestamp;
+ long timeoutMillis = next.getValue().heartbeatTimeoutMillis;
+ if ((last + timeoutMillis) < System.currentTimeMillis()) {
+ final Channel channel = next.getValue().channel;
+ if (channel != null) {
+ RemotingUtil.closeChannel(channel);
+ }
+ iterator.remove();
+ this.executor.submit(() ->
+ notifyBrokerInActive(next.getValue().brokerName, next.getKey().brokerAddr, next.getValue().brokerId));
+ log.warn("The broker channel expired, {} {}ms", next.getKey(), timeoutMillis);
+ }
+ }
+ } catch (Exception e) {
+ log.error("scanNotActiveBroker exception", e);
+ }
+ }
+
+ private void notifyBrokerInActive(String brokerName, String brokerAddr, Long brokerId) {
+ for (BrokerLifecycleListener listener : this.brokerLifecycleListeners) {
+ listener.onBrokerInactive(brokerName, brokerAddr, brokerId);
+ }
+ }
+
+ @Override
+ public void addBrokerLifecycleListener(BrokerLifecycleListener listener) {
+ this.brokerLifecycleListeners.add(listener);
+ }
+
+ @Override
+ public void registerBroker(String clusterName, String brokerName, String brokerAddr,
+ long brokerId, Long timeoutMillis, Channel channel) {
+ final BrokerAddrInfo addrInfo = new BrokerAddrInfo(clusterName, brokerAddr);
+ final BrokerLiveInfo prevBrokerLiveInfo = this.brokerLiveTable.put(addrInfo,
+ new BrokerLiveInfo(brokerName,
+ brokerId,
+ System.currentTimeMillis(),
+ timeoutMillis == null ? DEFAULT_BROKER_CHANNEL_EXPIRED_TIME : timeoutMillis,
+ channel));
+ if (prevBrokerLiveInfo == null) {
+ log.info("new broker registered, {}, brokerId:{}", addrInfo, brokerId);
+ }
+ }
+
+ @Override
+ public void onBrokerHeartbeat(String clusterName, String brokerAddr) {
+ BrokerAddrInfo addrInfo = new BrokerAddrInfo(clusterName, brokerAddr);
+ BrokerLiveInfo prev = this.brokerLiveTable.get(addrInfo);
+ if (prev != null) {
+ prev.lastUpdateTimestamp = System.currentTimeMillis();
+ }
+ }
+
+ @Override
+ public void onBrokerChannelClose(Channel channel) {
+ synchronized (this) {
+ for (Map.Entry<BrokerAddrInfo, BrokerLiveInfo> entry : this.brokerLiveTable.entrySet()) {
+ if (entry.getValue().channel == channel) {
+ this.executor.submit(() ->
+ notifyBrokerInActive(entry.getValue().brokerName, entry.getKey().brokerAddr, entry.getValue().brokerId));
+ break;
+ }
+ }
+ }
+ }
+
+ @Override
+ public boolean isBrokerActive(String clusterName, String brokerAddr) {
+ final BrokerLiveInfo info = this.brokerLiveTable.get(new BrokerAddrInfo(clusterName, brokerAddr));
+ if (info != null) {
+ long last = info.lastUpdateTimestamp;
+ long timeoutMillis = info.heartbeatTimeoutMillis;
+ return (last + timeoutMillis) >= System.currentTimeMillis();
+ }
+ return false;
+ }
+
+ static class BrokerLiveInfo {
+ private final String brokerName;
+ private final long brokerId;
Review Comment:
brokerInactive 的时候需要判断该 broker 是否为 Master, 因为 controller 单独部署, 所以需要保存 brokerId.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [rocketmq] RongtongJin merged pull request #4333: [Summer of code] Stand alone a new controller module
Posted by GitBox <gi...@apache.org>.
RongtongJin merged PR #4333:
URL: https://github.com/apache/rocketmq/pull/4333
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [rocketmq] hzh0425 commented on pull request #4333: [Summer of code] Stand alone a new controller module
Posted by GitBox <gi...@apache.org>.
hzh0425 commented on PR #4333:
URL: https://github.com/apache/rocketmq/pull/4333#issuecomment-1129923398
@RongtongJin maybe this pr can be merged
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org