You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "xjlgod (via GitHub)" <gi...@apache.org> on 2024/08/14 13:13:55 UTC
[PR] feature: add single server rale limit [incubator-seata]
xjlgod opened a new pull request, #6756:
URL: https://github.com/apache/incubator-seata/pull/6756
<!-- Please make sure you have read and understood the contributing guidelines -->
- [ ] I have registered the PR [changes](../changes).
### Ⅰ. Describe what this PR did
Support flow limiting control for a single server:
- Flow limiting components: Select Bucket4j components to implement flow limit control.
- Processing flow limit control: Process flow limiting at the top level in DefaultCordinator, and perform flow limiting checks in the onRequest method. Only GlobalBegin request will be limited.
- Transaction end identity: All transaction ends need to be able to correctly identify flow limit control and throw corresponding exceptions.
- Metrics: Use a new metrics record events when current limiting is triggered
### Ⅱ. Does this pull request fix one issue?
<!-- If that, add "fixes #xxx" below in the next line, for example, fixes #97. -->
### Ⅲ. Why don't you add test cases (unit test/integration test)?
### Ⅳ. Describe how to verify it
### Ⅴ. Special notes for reviews
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "funky-eyes (via GitHub)" <gi...@apache.org>.
funky-eyes commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1899933866
##########
server/src/main/java/org/apache/seata/server/limit/ratelimit/RateLimiterHandlerAbstract.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.seata.server.limit.ratelimit;
+
+import org.apache.seata.common.XID;
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.NumberUtils;
+import org.apache.seata.config.CachedConfigurationChangeListener;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationChangeEvent;
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.protocol.MessageType;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.AbstractTransactionRequestToTC;
+import org.apache.seata.core.protocol.transaction.AbstractTransactionResponse;
+import org.apache.seata.core.protocol.transaction.GlobalBeginResponse;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.server.limit.AbstractTransactionRequestHandler;
+import org.apache.seata.server.metrics.MetricsPublisher;
+
+/**
+ * RateLimiterHandler
+ */
+public class RateLimiterHandlerAbstract extends AbstractTransactionRequestHandler implements CachedConfigurationChangeListener {
+ /**
+ * The instance of RateLimiterHandler
+ */
+ private static volatile RateLimiterHandlerAbstract instance;
+
+ /**
+ * The instance of RateLimiter
+ */
+ private final RateLimiter rateLimiter;
+
+ /**
+ * The config of RateLimiterHandler
+ */
+ private final RateLimiterHandlerConfig config;
+
+ public RateLimiterHandlerAbstract(RateLimiter rateLimiter) {
+ this.rateLimiter = rateLimiter;
+ this.config = new RateLimiterHandlerConfig();
+ }
+
+ private RateLimiterHandlerAbstract() {
+ rateLimiter = EnhancedServiceLoader.load(RateLimiter.class);
+ config = rateLimiter.obtainConfig();
+
+ Configuration config = ConfigurationFactory.getInstance();
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this);
+ }
+
+ @Override
+ public AbstractTransactionResponse handle(AbstractTransactionRequestToTC originRequest, RpcContext context) {
+ if (!rateLimiter.isEnable()) {
+ return next(originRequest, context);
+ }
+
+ if (MessageType.TYPE_GLOBAL_BEGIN == originRequest.getTypeCode()) {
+ if (!rateLimiter.canPass()) {
+ GlobalBeginResponse response = new GlobalBeginResponse();
+ response.setTransactionExceptionCode(TransactionExceptionCode.BeginFailed);
+ response.setResultCode(ResultCode.Failed);
+ RateLimitInfo rateLimitInfo = RateLimitInfo.generateRateLimitInfo(context.getApplicationId(),
+ RateLimitInfo.GLOBAL_BEGIN_FAILED, context.getClientId(), XID.getIpAddressAndPort());
+ MetricsPublisher.postRateLimitEvent(rateLimitInfo);
+ response.setMsg(String.format("TransactionException[rate limit exception, rate limit info: %s]", rateLimitInfo));
+ return response;
+ }
+ }
+ return next(originRequest, context);
+ }
+
+ public static RateLimiterHandlerAbstract getInstance() {
+ if (instance == null) {
+ synchronized (RateLimiterHandlerAbstract.class) {
+ if (instance == null) {
+ instance = new RateLimiterHandlerAbstract();
+ }
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void onChangeEvent(ConfigurationChangeEvent event) {
+ String dataId = event.getDataId();
+ String newValue = event.getNewValue();
+ if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) {
+ config.setEnable(Boolean.parseBoolean(newValue));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) {
+ config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) {
+ config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) {
+ config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum()));
+ }
+ rateLimiter.reInit(config);
Review Comment:
ok
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1897942338
##########
server/src/main/java/org/apache/seata/server/ratelimit/RateLimiterHandler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seata.server.ratelimit;
+
+import org.apache.seata.common.XID;
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.NumberUtils;
+import org.apache.seata.config.CachedConfigurationChangeListener;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationChangeEvent;
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.protocol.AbstractMessage;
+import org.apache.seata.core.protocol.AbstractResultMessage;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.GlobalBeginRequest;
+import org.apache.seata.core.protocol.transaction.GlobalBeginResponse;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.server.metrics.MetricsPublisher;
+
+/**
+ * RateLimiterHandler
+ */
+public class RateLimiterHandler implements CachedConfigurationChangeListener {
+ /**
+ * The instance of RateLimiterHandler
+ */
+ private static volatile RateLimiterHandler instance;
+
+ /**
+ * The instance of RateLimiter
+ */
+ private final RateLimiter rateLimiter;
+
+ /**
+ * The config of RateLimiterHandler
+ */
+ private final RateLimiterHandlerConfig config;
+
+ public RateLimiterHandler(RateLimiter rateLimiter) {
+ this.rateLimiter = rateLimiter;
+ this.config = new RateLimiterHandlerConfig();
+ }
+
+ private RateLimiterHandler() {
+ rateLimiter = EnhancedServiceLoader.load(RateLimiter.class);
+ config = rateLimiter.obtainConfig();
+
+ Configuration config = ConfigurationFactory.getInstance();
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this);
+ }
+
+ public static RateLimiterHandler getInstance() {
+ if (instance == null) {
+ synchronized (RateLimiterHandler.class) {
+ if (instance == null) {
+ instance = new RateLimiterHandler();
+ }
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void onChangeEvent(ConfigurationChangeEvent event) {
+ String dataId = event.getDataId();
+ String newValue = event.getNewValue();
+ if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) {
+ config.setEnable(Boolean.parseBoolean(newValue));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) {
+ config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) {
+ config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) {
+ config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum()));
+ }
+ rateLimiter.reInit(config);
+ }
+
+ public AbstractResultMessage handle(AbstractMessage request, RpcContext rpcContext) {
+ if (!rateLimiter.isEnable()) {
+ return null;
+ }
+
+ if (request instanceof GlobalBeginRequest) {
Review Comment:
fixed it.
##########
server/src/main/resources/file.conf:
##########
Review Comment:
fixed it.
##########
server/src/main/java/org/apache/seata/server/ratelimit/RateLimiterHandler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seata.server.ratelimit;
+
+import org.apache.seata.common.XID;
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.NumberUtils;
+import org.apache.seata.config.CachedConfigurationChangeListener;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationChangeEvent;
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.protocol.AbstractMessage;
+import org.apache.seata.core.protocol.AbstractResultMessage;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.GlobalBeginRequest;
+import org.apache.seata.core.protocol.transaction.GlobalBeginResponse;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.server.metrics.MetricsPublisher;
+
+/**
+ * RateLimiterHandler
+ */
+public class RateLimiterHandler implements CachedConfigurationChangeListener {
+ /**
+ * The instance of RateLimiterHandler
+ */
+ private static volatile RateLimiterHandler instance;
+
+ /**
+ * The instance of RateLimiter
+ */
+ private final RateLimiter rateLimiter;
+
+ /**
+ * The config of RateLimiterHandler
+ */
+ private final RateLimiterHandlerConfig config;
+
+ public RateLimiterHandler(RateLimiter rateLimiter) {
+ this.rateLimiter = rateLimiter;
+ this.config = new RateLimiterHandlerConfig();
+ }
+
+ private RateLimiterHandler() {
+ rateLimiter = EnhancedServiceLoader.load(RateLimiter.class);
+ config = rateLimiter.obtainConfig();
+
+ Configuration config = ConfigurationFactory.getInstance();
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this);
+ }
+
+ public static RateLimiterHandler getInstance() {
+ if (instance == null) {
+ synchronized (RateLimiterHandler.class) {
+ if (instance == null) {
+ instance = new RateLimiterHandler();
+ }
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void onChangeEvent(ConfigurationChangeEvent event) {
+ String dataId = event.getDataId();
+ String newValue = event.getNewValue();
+ if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) {
+ config.setEnable(Boolean.parseBoolean(newValue));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) {
+ config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) {
+ config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) {
+ config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum()));
+ }
+ rateLimiter.reInit(config);
+ }
+
+ public AbstractResultMessage handle(AbstractMessage request, RpcContext rpcContext) {
+ if (!rateLimiter.isEnable()) {
+ return null;
+ }
+
+ if (request instanceof GlobalBeginRequest) {
+ if (!rateLimiter.canPass()) {
+ GlobalBeginResponse response = new GlobalBeginResponse();
+ response.setTransactionExceptionCode(TransactionExceptionCode.BeginFailed);
+ response.setResultCode(ResultCode.Failed);
+ RateLimitInfo rateLimitInfo = RateLimitInfo.generateRateLimitInfo(rpcContext.getApplicationId(),
+ RateLimitInfo.GLOBAL_BEGIN_FAILED, rpcContext.getClientId(), XID.getIpAddressAndPort());
+ MetricsPublisher.postRateLimitEvent(rateLimitInfo);
+ response.setMsg(String.format("TransactionException[rate limit exception, rate limit info: %s]", rateLimitInfo));
Review Comment:
fixed it.
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1896774908
##########
server/src/main/java/org/apache/seata/server/ratelimit/RateLimiterHandler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seata.server.ratelimit;
+
+import org.apache.seata.common.XID;
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.NumberUtils;
+import org.apache.seata.config.CachedConfigurationChangeListener;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationChangeEvent;
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.protocol.AbstractMessage;
+import org.apache.seata.core.protocol.AbstractResultMessage;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.GlobalBeginRequest;
+import org.apache.seata.core.protocol.transaction.GlobalBeginResponse;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.server.metrics.MetricsPublisher;
+
+/**
+ * RateLimiterHandler
+ */
+public class RateLimiterHandler implements CachedConfigurationChangeListener {
+ /**
+ * The instance of RateLimiterHandler
+ */
+ private static volatile RateLimiterHandler instance;
+
+ /**
+ * The instance of RateLimiter
+ */
+ private final RateLimiter rateLimiter;
+
+ /**
+ * The config of RateLimiterHandler
+ */
+ private final RateLimiterHandlerConfig config;
+
+ public RateLimiterHandler(RateLimiter rateLimiter) {
+ this.rateLimiter = rateLimiter;
+ this.config = new RateLimiterHandlerConfig();
+ }
+
+ private RateLimiterHandler() {
+ rateLimiter = EnhancedServiceLoader.load(RateLimiter.class);
+ config = rateLimiter.obtainConfig();
+
+ Configuration config = ConfigurationFactory.getInstance();
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this);
+ }
+
+ public static RateLimiterHandler getInstance() {
+ if (instance == null) {
+ synchronized (RateLimiterHandler.class) {
+ if (instance == null) {
+ instance = new RateLimiterHandler();
+ }
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void onChangeEvent(ConfigurationChangeEvent event) {
+ String dataId = event.getDataId();
+ String newValue = event.getNewValue();
+ if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) {
+ config.setEnable(Boolean.parseBoolean(newValue));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) {
+ config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) {
+ config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) {
+ config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum()));
+ }
+ rateLimiter.reInit(config);
+ }
+
+ public AbstractResultMessage handle(AbstractMessage request, RpcContext rpcContext) {
+ if (!rateLimiter.isEnable()) {
+ return null;
+ }
+
+ if (request instanceof GlobalBeginRequest) {
Review Comment:
这个当时是说的后面来优化,限流request的集合是指的比如是分支开始?如果是配置集合,我理解那就必须在客户端也看看是否做对应修改,以免不兼容?
This was the later optimization. Is the collection of current restriction Request refers to the start of the branch? If it is a configuration collection, you must also see the corresponding modification on the client, so as not to be incompatible?
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1896358654
##########
server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java:
##########
@@ -76,6 +66,17 @@
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
Review Comment:
fixed it
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "funky-eyes (via GitHub)" <gi...@apache.org>.
funky-eyes commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1898910284
##########
server/src/main/resources/application.example.yml:
##########
@@ -159,6 +159,11 @@ seata:
session:
branch-async-queue-size: 5000 #branch async remove queue size
enable-branch-async-remove: false #enable to asynchronous remove branchSession
+ ratelimit:
Review Comment:
默认值调大
Default value up
##########
server/src/main/java/org/apache/seata/server/limit/ratelimit/RateLimiterHandlerAbstract.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.seata.server.limit.ratelimit;
+
+import org.apache.seata.common.XID;
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.NumberUtils;
+import org.apache.seata.config.CachedConfigurationChangeListener;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationChangeEvent;
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.protocol.MessageType;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.AbstractTransactionRequestToTC;
+import org.apache.seata.core.protocol.transaction.AbstractTransactionResponse;
+import org.apache.seata.core.protocol.transaction.GlobalBeginResponse;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.server.limit.AbstractTransactionRequestHandler;
+import org.apache.seata.server.metrics.MetricsPublisher;
+
+/**
+ * RateLimiterHandler
+ */
+public class RateLimiterHandlerAbstract extends AbstractTransactionRequestHandler implements CachedConfigurationChangeListener {
+ /**
+ * The instance of RateLimiterHandler
+ */
+ private static volatile RateLimiterHandlerAbstract instance;
+
+ /**
+ * The instance of RateLimiter
+ */
+ private final RateLimiter rateLimiter;
+
+ /**
+ * The config of RateLimiterHandler
+ */
+ private final RateLimiterHandlerConfig config;
+
+ public RateLimiterHandlerAbstract(RateLimiter rateLimiter) {
+ this.rateLimiter = rateLimiter;
+ this.config = new RateLimiterHandlerConfig();
+ }
+
+ private RateLimiterHandlerAbstract() {
+ rateLimiter = EnhancedServiceLoader.load(RateLimiter.class);
+ config = rateLimiter.obtainConfig();
+
+ Configuration config = ConfigurationFactory.getInstance();
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this);
+ }
+
+ @Override
+ public AbstractTransactionResponse handle(AbstractTransactionRequestToTC originRequest, RpcContext context) {
+ if (!rateLimiter.isEnable()) {
+ return next(originRequest, context);
+ }
+
+ if (MessageType.TYPE_GLOBAL_BEGIN == originRequest.getTypeCode()) {
+ if (!rateLimiter.canPass()) {
+ GlobalBeginResponse response = new GlobalBeginResponse();
+ response.setTransactionExceptionCode(TransactionExceptionCode.BeginFailed);
+ response.setResultCode(ResultCode.Failed);
+ RateLimitInfo rateLimitInfo = RateLimitInfo.generateRateLimitInfo(context.getApplicationId(),
+ RateLimitInfo.GLOBAL_BEGIN_FAILED, context.getClientId(), XID.getIpAddressAndPort());
+ MetricsPublisher.postRateLimitEvent(rateLimitInfo);
+ response.setMsg(String.format("TransactionException[rate limit exception, rate limit info: %s]", rateLimitInfo));
+ return response;
+ }
+ }
+ return next(originRequest, context);
+ }
+
+ public static RateLimiterHandlerAbstract getInstance() {
+ if (instance == null) {
+ synchronized (RateLimiterHandlerAbstract.class) {
+ if (instance == null) {
+ instance = new RateLimiterHandlerAbstract();
+ }
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void onChangeEvent(ConfigurationChangeEvent event) {
+ String dataId = event.getDataId();
+ String newValue = event.getNewValue();
+ if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) {
+ config.setEnable(Boolean.parseBoolean(newValue));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) {
+ config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) {
+ config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) {
+ config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum()));
+ }
+ rateLimiter.reInit(config);
Review Comment:
为什么不直接在TokenBucketLimiter中进行监听?
Why not listen directly in TokenBucketLimiter?
##########
server/src/main/resources/application.raft.example.yml:
##########
@@ -131,6 +131,11 @@ seata:
session:
branch-async-queue-size: 5000 #branch async remove queue size
enable-branch-async-remove: false #enable to asynchronous remove branchSession
+ ratelimit:
Review Comment:
默认值调大
Default value up
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rale limit [incubator-seata]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#issuecomment-2308437883
## [Codecov](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
Attention: Patch coverage is `46.74556%` with `90 lines` in your changes missing coverage. Please review.
> Project coverage is 52.29%. Comparing base [(`5601c36`)](https://app.codecov.io/gh/apache/incubator-seata/commit/5601c36217e3f5dd0a84bd66f61bd7c0d6b5076d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`da1f952`)](https://app.codecov.io/gh/apache/incubator-seata/commit/da1f9528e04018e2fa67f4b939d78234959b7ea8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
> Report is 2 commits behind head on 2.x.
| [Files](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [...va/org/apache/seata/core/event/RateLimitEvent.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=core%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fcore%2Fevent%2FRateLimitEvent.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Y29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2VhdGEvY29yZS9ldmVudC9SYXRlTGltaXRFdmVudC5qYXZh) | 0.00% | [23 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...che/seata/server/ratelimit/TokenBucketLimiter.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fserver%2Fratelimit%2FTokenBucketLimiter.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zZXJ2ZXIvcmF0ZWxpbWl0L1Rva2VuQnVja2V0TGltaXRlci5qYXZh) | 45.71% | [19 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...e/properties/server/ServerRateLimitProperties.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=seata-spring-autoconfigure%2Fseata-spring-autoconfigure-server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fspring%2Fboot%2Fautoconfigure%2Fproperties%2Fserver%2FServerRateLimitProperties.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VhdGEtc3ByaW5nLWF1dG9jb25maWd1cmUvc2VhdGEtc3ByaW5nLWF1dG9jb25maWd1cmUtc2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zcHJpbmcvYm9vdC9hdXRvY29uZmlndXJlL3Byb3BlcnRpZXMvc2VydmVyL1NlcnZlclJhdGVMaW1pdFByb3BlcnRpZXMuamF2YQ==) | 7.69% | [12 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...e/seata/server/coordinator/DefaultCoordinator.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fserver%2Fcoordinator%2FDefaultCoordinator.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zZXJ2ZXIvY29vcmRpbmF0b3IvRGVmYXVsdENvb3JkaW5hdG9yLmphdmE=) | 9.09% | [9 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...che/seata/server/ratelimit/RateLimiterHandler.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fserver%2Fratelimit%2FRateLimiterHandler.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zZXJ2ZXIvcmF0ZWxpbWl0L1JhdGVMaW1pdGVySGFuZGxlci5qYXZh) | 56.52% | [9 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...handler/GlobalTransactionalInterceptorHandler.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=integration-tx-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fintegration%2Ftx%2Fapi%2Finterceptor%2Fhandler%2FGlobalTransactionalInterceptorHandler.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-aW50ZWdyYXRpb24tdHgtYXBpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9pbnRlZ3JhdGlvbi90eC9hcGkvaW50ZXJjZXB0b3IvaGFuZGxlci9HbG9iYWxUcmFuc2FjdGlvbmFsSW50ZXJjZXB0b3JIYW5kbGVyLmphdmE=) | 0.00% | [3 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...ava/io/seata/tm/api/DefaultFailureHandlerImpl.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=compatible%2Fsrc%2Fmain%2Fjava%2Fio%2Fseata%2Ftm%2Fapi%2FDefaultFailureHandlerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Y29tcGF0aWJsZS9zcmMvbWFpbi9qYXZhL2lvL3NlYXRhL3RtL2FwaS9EZWZhdWx0RmFpbHVyZUhhbmRsZXJJbXBsLmphdmE=) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...ga/engine/tm/DefaultSagaTransactionalTemplate.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=saga%2Fseata-saga-spring%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fsaga%2Fengine%2Ftm%2FDefaultSagaTransactionalTemplate.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2FnYS9zZWF0YS1zYWdhLXNwcmluZy9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2VhdGEvc2FnYS9lbmdpbmUvdG0vRGVmYXVsdFNhZ2FUcmFuc2FjdGlvbmFsVGVtcGxhdGUuamF2YQ==) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...zer/seata/protocol/AbstractResultMessageCodec.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=serializer%2Fseata-serializer-seata%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fserializer%2Fseata%2Fprotocol%2FAbstractResultMessageCodec.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VyaWFsaXplci9zZWF0YS1zZXJpYWxpemVyLXNlYXRhL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zZXJpYWxpemVyL3NlYXRhL3Byb3RvY29sL0Fic3RyYWN0UmVzdWx0TWVzc2FnZUNvZGVjLmphdmE=) | 0.00% | [0 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...org/apache/seata/tm/DefaultTransactionManager.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=tm%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Ftm%2FDefaultTransactionManager.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-dG0vc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NlYXRhL3RtL0RlZmF1bHRUcmFuc2FjdGlvbk1hbmFnZXIuamF2YQ==) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| ... and [3 more](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | |
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## 2.x #6756 +/- ##
============================================
+ Coverage 52.15% 52.29% +0.14%
- Complexity 6289 6332 +43
============================================
Files 1064 1069 +5
Lines 37194 37380 +186
Branches 4414 4430 +16
============================================
+ Hits 19399 19549 +150
- Misses 15882 15902 +20
- Partials 1913 1929 +16
```
| [Files](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [...ava/org/apache/seata/common/ConfigurationKeys.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=common%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fcommon%2FConfigurationKeys.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Y29tbW9uL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9jb21tb24vQ29uZmlndXJhdGlvbktleXMuamF2YQ==) | `0.00% <ø> (ø)` | |
| [...seata/core/exception/TransactionExceptionCode.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=core%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fcore%2Fexception%2FTransactionExceptionCode.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Y29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2VhdGEvY29yZS9leGNlcHRpb24vVHJhbnNhY3Rpb25FeGNlcHRpb25Db2RlLmphdmE=) | `100.00% <100.00%> (ø)` | |
| [...ava/org/apache/seata/core/protocol/ResultCode.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=core%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fcore%2Fprotocol%2FResultCode.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Y29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2VhdGEvY29yZS9wcm90b2NvbC9SZXN1bHRDb2RlLmphdmE=) | `100.00% <100.00%> (ø)` | |
| [...ta/spring/boot/autoconfigure/StarterConstants.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=seata-spring-autoconfigure%2Fseata-spring-autoconfigure-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fspring%2Fboot%2Fautoconfigure%2FStarterConstants.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VhdGEtc3ByaW5nLWF1dG9jb25maWd1cmUvc2VhdGEtc3ByaW5nLWF1dG9jb25maWd1cmUtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2VhdGEvc3ByaW5nL2Jvb3QvYXV0b2NvbmZpZ3VyZS9TdGFydGVyQ29uc3RhbnRzLmphdmE=) | `100.00% <ø> (ø)` | |
| [.../apache/seata/server/metrics/MeterIdConstants.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fserver%2Fmetrics%2FMeterIdConstants.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zZXJ2ZXIvbWV0cmljcy9NZXRlcklkQ29uc3RhbnRzLmphdmE=) | `100.00% <100.00%> (ø)` | |
| [.../apache/seata/server/metrics/MetricsPublisher.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fserver%2Fmetrics%2FMetricsPublisher.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zZXJ2ZXIvbWV0cmljcy9NZXRyaWNzUHVibGlzaGVyLmphdmE=) | `94.73% <100.00%> (+0.98%)` | :arrow_up: |
| [...apache/seata/server/metrics/MetricsSubscriber.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fserver%2Fmetrics%2FMetricsSubscriber.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zZXJ2ZXIvbWV0cmljcy9NZXRyaWNzU3Vic2NyaWJlci5qYXZh) | `21.70% <100.00%> (+7.29%)` | :arrow_up: |
| [...g/apache/seata/server/ratelimit/RateLimitInfo.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fserver%2Fratelimit%2FRateLimitInfo.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zZXJ2ZXIvcmF0ZWxpbWl0L1JhdGVMaW1pdEluZm8uamF2YQ==) | `100.00% <100.00%> (ø)` | |
| [...org/apache/seata/tm/api/TransactionalExecutor.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=tm%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Ftm%2Fapi%2FTransactionalExecutor.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-dG0vc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NlYXRhL3RtL2FwaS9UcmFuc2FjdGlvbmFsRXhlY3V0b3IuamF2YQ==) | `65.51% <100.00%> (+1.23%)` | :arrow_up: |
| [...configure/SeataServerEnvironmentPostProcessor.java](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree&filepath=seata-spring-autoconfigure%2Fseata-spring-autoconfigure-server%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fspring%2Fboot%2Fautoconfigure%2FSeataServerEnvironmentPostProcessor.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2VhdGEtc3ByaW5nLWF1dG9jb25maWd1cmUvc2VhdGEtc3ByaW5nLWF1dG9jb25maWd1cmUtc2VydmVyL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9zcHJpbmcvYm9vdC9hdXRvY29uZmlndXJlL1NlYXRhU2VydmVyRW52aXJvbm1lbnRQb3N0UHJvY2Vzc29yLmphdmE=) | `0.00% <0.00%> (ø)` | |
| ... and [12 more](https://app.codecov.io/gh/apache/incubator-seata/pull/6756?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | |
... and [14 files with indirect coverage changes](https://app.codecov.io/gh/apache/incubator-seata/pull/6756/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
</details>
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod commented on PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#issuecomment-2560613075
> The rate limit has to immediate effect when it enable or change
fixed it.
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1899105127
##########
server/src/main/java/org/apache/seata/server/limit/ratelimit/RateLimiterHandlerAbstract.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.seata.server.limit.ratelimit;
+
+import org.apache.seata.common.XID;
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.NumberUtils;
+import org.apache.seata.config.CachedConfigurationChangeListener;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationChangeEvent;
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.protocol.MessageType;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.AbstractTransactionRequestToTC;
+import org.apache.seata.core.protocol.transaction.AbstractTransactionResponse;
+import org.apache.seata.core.protocol.transaction.GlobalBeginResponse;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.server.limit.AbstractTransactionRequestHandler;
+import org.apache.seata.server.metrics.MetricsPublisher;
+
+/**
+ * RateLimiterHandler
+ */
+public class RateLimiterHandlerAbstract extends AbstractTransactionRequestHandler implements CachedConfigurationChangeListener {
+ /**
+ * The instance of RateLimiterHandler
+ */
+ private static volatile RateLimiterHandlerAbstract instance;
+
+ /**
+ * The instance of RateLimiter
+ */
+ private final RateLimiter rateLimiter;
+
+ /**
+ * The config of RateLimiterHandler
+ */
+ private final RateLimiterHandlerConfig config;
+
+ public RateLimiterHandlerAbstract(RateLimiter rateLimiter) {
+ this.rateLimiter = rateLimiter;
+ this.config = new RateLimiterHandlerConfig();
+ }
+
+ private RateLimiterHandlerAbstract() {
+ rateLimiter = EnhancedServiceLoader.load(RateLimiter.class);
+ config = rateLimiter.obtainConfig();
+
+ Configuration config = ConfigurationFactory.getInstance();
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this);
+ }
+
+ @Override
+ public AbstractTransactionResponse handle(AbstractTransactionRequestToTC originRequest, RpcContext context) {
+ if (!rateLimiter.isEnable()) {
+ return next(originRequest, context);
+ }
+
+ if (MessageType.TYPE_GLOBAL_BEGIN == originRequest.getTypeCode()) {
+ if (!rateLimiter.canPass()) {
+ GlobalBeginResponse response = new GlobalBeginResponse();
+ response.setTransactionExceptionCode(TransactionExceptionCode.BeginFailed);
+ response.setResultCode(ResultCode.Failed);
+ RateLimitInfo rateLimitInfo = RateLimitInfo.generateRateLimitInfo(context.getApplicationId(),
+ RateLimitInfo.GLOBAL_BEGIN_FAILED, context.getClientId(), XID.getIpAddressAndPort());
+ MetricsPublisher.postRateLimitEvent(rateLimitInfo);
+ response.setMsg(String.format("TransactionException[rate limit exception, rate limit info: %s]", rateLimitInfo));
+ return response;
+ }
+ }
+ return next(originRequest, context);
+ }
+
+ public static RateLimiterHandlerAbstract getInstance() {
+ if (instance == null) {
+ synchronized (RateLimiterHandlerAbstract.class) {
+ if (instance == null) {
+ instance = new RateLimiterHandlerAbstract();
+ }
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void onChangeEvent(ConfigurationChangeEvent event) {
+ String dataId = event.getDataId();
+ String newValue = event.getNewValue();
+ if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) {
+ config.setEnable(Boolean.parseBoolean(newValue));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) {
+ config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) {
+ config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) {
+ config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum()));
+ }
+ rateLimiter.reInit(config);
Review Comment:
因为想的是ratelimithandler下面可以通过spi加载不同的限流器,然后具体的限流器重新加载由具体的限流器实现
By ratelimithandler, you can load different current limiter through spi, and then the specific current limiter reloads by the new config from Ratelimithandler
##########
server/src/main/resources/application.raft.example.yml:
##########
@@ -131,6 +131,11 @@ seata:
session:
branch-async-queue-size: 5000 #branch async remove queue size
enable-branch-async-remove: false #enable to asynchronous remove branchSession
+ ratelimit:
Review Comment:
fixed it
##########
server/src/main/resources/application.example.yml:
##########
@@ -159,6 +159,11 @@ seata:
session:
branch-async-queue-size: 5000 #branch async remove queue size
enable-branch-async-remove: false #enable to asynchronous remove branchSession
+ ratelimit:
Review Comment:
fixed it
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod merged PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "funky-eyes (via GitHub)" <gi...@apache.org>.
funky-eyes commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1895149384
##########
compatible/src/main/java/io/seata/tm/api/DefaultFailureHandlerImpl.java:
##########
@@ -28,6 +26,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.concurrent.TimeUnit;
Review Comment:
```suggestion
```
##########
compatible/src/main/java/io/seata/tm/api/DefaultFailureHandlerImpl.java:
##########
@@ -16,8 +16,6 @@
*/
package io.seata.tm.api;
-import java.util.concurrent.TimeUnit;
-
import io.netty.util.HashedWheelTimer;
Review Comment:
```suggestion
import java.util.concurrent.TimeUnit;
import io.netty.util.HashedWheelTimer;
```
##########
server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java:
##########
@@ -76,6 +66,17 @@
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
Review Comment:
```suggestion
```
##########
metrics/seata-metrics-api/src/main/java/org/apache/seata/metrics/IdConstants.java:
##########
@@ -79,4 +81,9 @@ public interface IdConstants {
String STATUS_VALUE_AFTER_ROLLBACKED_KEY = "AfterRollbacked";
+ String LIMIT_TYPE_KEY = "limitType";
+
+ String CLIENT_ID_KEY = "clientId";
+
+ String SERVER_IP_ADDRESS_AND_PORT_KEY = "serverIpAddressAndPort";
Review Comment:
Isn't 'hostAndPort' more concise?
##########
script/config-center/config.txt:
##########
@@ -162,7 +162,10 @@ server.raft.serialization=jackson
server.raft.compressor=none
server.raft.sync=true
-
+server.ratelimit.enable=false
+server.ratelimit.bucketTokenNumPerSecond = 1
+server.ratelimit.bucketTokenMaxNum = 1
+server.ratelimit.bucketTokenInitialNum = 1
Review Comment:
Some users may blindly copy this configuration, so it's better to set the threshold to a larger value
##########
server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java:
##########
@@ -16,17 +16,6 @@
*/
package org.apache.seata.server.coordinator;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.concurrent.ArrayBlockingQueue;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
-
import io.netty.channel.Channel;
Review Comment:
```suggestion
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import io.netty.channel.Channel;
```
##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/handler/GlobalTransactionalInterceptorHandler.java:
##########
@@ -59,6 +52,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.lang.reflect.Method;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
Review Comment:
```suggestion
```
##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/handler/GlobalTransactionalInterceptorHandler.java:
##########
@@ -16,13 +16,6 @@
*/
package org.apache.seata.integration.tx.api.interceptor.handler;
-import java.lang.reflect.Method;
-import java.util.LinkedHashSet;
-import java.util.Set;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
-
import com.google.common.eventbus.Subscribe;
Review Comment:
```suggestion
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import com.google.common.eventbus.Subscribe;
```
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod commented on PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#issuecomment-2560620552
> The rate limit has to immediate effect when it enable or change.
Had added hot config.
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rale limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1845460215
##########
common/src/main/java/org/apache/seata/common/ConfigurationKeys.java:
##########
@@ -1059,4 +1059,29 @@ public interface ConfigurationKeys {
* The constant META_PREFIX
*/
String META_PREFIX = SEATA_FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR + FILE_ROOT_REGISTRY + FILE_CONFIG_SPLIT_CHAR + "metadata.";
+
+ /**
+ * The constant RATE_LIMIT_PREFIX.
+ */
+ String RATE_LIMIT_PREFIX = SERVER_PREFIX + "ratelimit.";
+
+ /**
+ * The constant RATE_LIMIT_BUCKET_TOKEN_SECOND_NUM.
+ */
+ String RATE_LIMIT_BUCKET_TOKEN_SECOND_NUM = RATE_LIMIT_PREFIX + "bucketTokenSecondNum";
Review Comment:
fixed it
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rale limit [incubator-seata]
Posted by "l81893521 (via GitHub)" <gi...@apache.org>.
l81893521 commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1844972951
##########
common/src/main/java/org/apache/seata/common/ConfigurationKeys.java:
##########
@@ -1059,4 +1059,29 @@ public interface ConfigurationKeys {
* The constant META_PREFIX
*/
String META_PREFIX = SEATA_FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR + FILE_ROOT_REGISTRY + FILE_CONFIG_SPLIT_CHAR + "metadata.";
+
+ /**
+ * The constant RATE_LIMIT_PREFIX.
+ */
+ String RATE_LIMIT_PREFIX = SERVER_PREFIX + "ratelimit.";
+
+ /**
+ * The constant RATE_LIMIT_BUCKET_TOKEN_SECOND_NUM.
+ */
+ String RATE_LIMIT_BUCKET_TOKEN_SECOND_NUM = RATE_LIMIT_PREFIX + "bucketTokenSecondNum";
Review Comment:
bucketTokenNumPerSecond
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "funky-eyes (via GitHub)" <gi...@apache.org>.
funky-eyes commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1896744584
##########
server/src/main/java/org/apache/seata/server/ratelimit/RateLimiterHandler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seata.server.ratelimit;
+
+import org.apache.seata.common.XID;
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.NumberUtils;
+import org.apache.seata.config.CachedConfigurationChangeListener;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationChangeEvent;
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.protocol.AbstractMessage;
+import org.apache.seata.core.protocol.AbstractResultMessage;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.GlobalBeginRequest;
+import org.apache.seata.core.protocol.transaction.GlobalBeginResponse;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.server.metrics.MetricsPublisher;
+
+/**
+ * RateLimiterHandler
+ */
+public class RateLimiterHandler implements CachedConfigurationChangeListener {
+ /**
+ * The instance of RateLimiterHandler
+ */
+ private static volatile RateLimiterHandler instance;
+
+ /**
+ * The instance of RateLimiter
+ */
+ private final RateLimiter rateLimiter;
+
+ /**
+ * The config of RateLimiterHandler
+ */
+ private final RateLimiterHandlerConfig config;
+
+ public RateLimiterHandler(RateLimiter rateLimiter) {
+ this.rateLimiter = rateLimiter;
+ this.config = new RateLimiterHandlerConfig();
+ }
+
+ private RateLimiterHandler() {
+ rateLimiter = EnhancedServiceLoader.load(RateLimiter.class);
+ config = rateLimiter.obtainConfig();
+
+ Configuration config = ConfigurationFactory.getInstance();
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this);
+ }
+
+ public static RateLimiterHandler getInstance() {
+ if (instance == null) {
+ synchronized (RateLimiterHandler.class) {
+ if (instance == null) {
+ instance = new RateLimiterHandler();
+ }
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void onChangeEvent(ConfigurationChangeEvent event) {
+ String dataId = event.getDataId();
+ String newValue = event.getNewValue();
+ if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) {
+ config.setEnable(Boolean.parseBoolean(newValue));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) {
+ config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) {
+ config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) {
+ config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum()));
+ }
+ rateLimiter.reInit(config);
+ }
+
+ public AbstractResultMessage handle(AbstractMessage request, RpcContext rpcContext) {
+ if (!rateLimiter.isEnable()) {
+ return null;
+ }
+
+ if (request instanceof GlobalBeginRequest) {
+ if (!rateLimiter.canPass()) {
+ GlobalBeginResponse response = new GlobalBeginResponse();
+ response.setTransactionExceptionCode(TransactionExceptionCode.BeginFailed);
+ response.setResultCode(ResultCode.Failed);
+ RateLimitInfo rateLimitInfo = RateLimitInfo.generateRateLimitInfo(rpcContext.getApplicationId(),
+ RateLimitInfo.GLOBAL_BEGIN_FAILED, rpcContext.getClientId(), XID.getIpAddressAndPort());
+ MetricsPublisher.postRateLimitEvent(rateLimitInfo);
+ response.setMsg(String.format("TransactionException[rate limit exception, rate limit info: %s]", rateLimitInfo));
Review Comment:
限流器应该只是限流作用,为什么要跟业务逻辑耦合在一起?
The role of limited viewership should only be limited viewership, why should it be coupled with business logic?
##########
server/src/main/resources/file.conf:
##########
Review Comment:
Delete it
##########
server/src/main/java/org/apache/seata/server/ratelimit/RateLimiterHandler.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seata.server.ratelimit;
+
+import org.apache.seata.common.XID;
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.NumberUtils;
+import org.apache.seata.config.CachedConfigurationChangeListener;
+import org.apache.seata.config.Configuration;
+import org.apache.seata.config.ConfigurationChangeEvent;
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.protocol.AbstractMessage;
+import org.apache.seata.core.protocol.AbstractResultMessage;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.GlobalBeginRequest;
+import org.apache.seata.core.protocol.transaction.GlobalBeginResponse;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.server.metrics.MetricsPublisher;
+
+/**
+ * RateLimiterHandler
+ */
+public class RateLimiterHandler implements CachedConfigurationChangeListener {
+ /**
+ * The instance of RateLimiterHandler
+ */
+ private static volatile RateLimiterHandler instance;
+
+ /**
+ * The instance of RateLimiter
+ */
+ private final RateLimiter rateLimiter;
+
+ /**
+ * The config of RateLimiterHandler
+ */
+ private final RateLimiterHandlerConfig config;
+
+ public RateLimiterHandler(RateLimiter rateLimiter) {
+ this.rateLimiter = rateLimiter;
+ this.config = new RateLimiterHandlerConfig();
+ }
+
+ private RateLimiterHandler() {
+ rateLimiter = EnhancedServiceLoader.load(RateLimiter.class);
+ config = rateLimiter.obtainConfig();
+
+ Configuration config = ConfigurationFactory.getInstance();
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this);
+ config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this);
+ }
+
+ public static RateLimiterHandler getInstance() {
+ if (instance == null) {
+ synchronized (RateLimiterHandler.class) {
+ if (instance == null) {
+ instance = new RateLimiterHandler();
+ }
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void onChangeEvent(ConfigurationChangeEvent event) {
+ String dataId = event.getDataId();
+ String newValue = event.getNewValue();
+ if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) {
+ config.setEnable(Boolean.parseBoolean(newValue));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) {
+ config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) {
+ config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum()));
+ }
+ if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) {
+ config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum()));
+ }
+ rateLimiter.reInit(config);
+ }
+
+ public AbstractResultMessage handle(AbstractMessage request, RpcContext rpcContext) {
+ if (!rateLimiter.isEnable()) {
+ return null;
+ }
+
+ if (request instanceof GlobalBeginRequest) {
Review Comment:
为什么不使用MessageType来判断,未来是否限流器可以作用于更多的request?只要对外提供一个配置项,允许用户配置一个需要限流request的集合,这里只要判断MessageType是否包含在其中即可
Why not use MessageType to determine whether the limit viewership of requests can be used in the future? Just provide a configuration item to allow users to configure a collection that needs to limit viewership of requests. Here, just determine whether MessageType is included in it
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add single server rate limit [incubator-seata]
Posted by "xjlgod (via GitHub)" <gi...@apache.org>.
xjlgod commented on code in PR #6756:
URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1896358529
##########
compatible/src/main/java/io/seata/tm/api/DefaultFailureHandlerImpl.java:
##########
@@ -16,8 +16,6 @@
*/
package io.seata.tm.api;
-import java.util.concurrent.TimeUnit;
-
import io.netty.util.HashedWheelTimer;
Review Comment:
fixed it
##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/handler/GlobalTransactionalInterceptorHandler.java:
##########
@@ -59,6 +52,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.lang.reflect.Method;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
Review Comment:
fixed it
##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/handler/GlobalTransactionalInterceptorHandler.java:
##########
@@ -16,13 +16,6 @@
*/
package org.apache.seata.integration.tx.api.interceptor.handler;
-import java.lang.reflect.Method;
-import java.util.LinkedHashSet;
-import java.util.Set;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
-
import com.google.common.eventbus.Subscribe;
Review Comment:
fixed it
##########
metrics/seata-metrics-api/src/main/java/org/apache/seata/metrics/IdConstants.java:
##########
@@ -79,4 +81,9 @@ public interface IdConstants {
String STATUS_VALUE_AFTER_ROLLBACKED_KEY = "AfterRollbacked";
+ String LIMIT_TYPE_KEY = "limitType";
+
+ String CLIENT_ID_KEY = "clientId";
+
+ String SERVER_IP_ADDRESS_AND_PORT_KEY = "serverIpAddressAndPort";
Review Comment:
fixed it
##########
script/config-center/config.txt:
##########
@@ -162,7 +162,10 @@ server.raft.serialization=jackson
server.raft.compressor=none
server.raft.sync=true
-
+server.ratelimit.enable=false
+server.ratelimit.bucketTokenNumPerSecond = 1
+server.ratelimit.bucketTokenMaxNum = 1
+server.ratelimit.bucketTokenInitialNum = 1
Review Comment:
fixed it
##########
server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java:
##########
@@ -16,17 +16,6 @@
*/
package org.apache.seata.server.coordinator;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.concurrent.ArrayBlockingQueue;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
-
import io.netty.channel.Channel;
Review Comment:
fixed it
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org