maedhroz commented on a change in pull request #1045: URL: https://github.com/apache/cassandra/pull/1045#discussion_r648773684
########## File path: test/unit/org/apache/cassandra/transport/RateLimitingTest.java ########## @@ -0,0 +1,129 @@ +/* + * 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.cassandra.transport; + +import java.io.IOException; +import java.util.Collection; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; + +import com.google.common.base.Ticker; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.cql3.CQLTester; +import org.apache.cassandra.cql3.QueryOptions; +import org.apache.cassandra.exceptions.OverloadedException; +import org.apache.cassandra.transport.messages.QueryMessage; +import org.apache.cassandra.utils.Throwables; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +@SuppressWarnings("UnstableApiUsage") +@RunWith(Parameterized.class) +public class RateLimitingTest extends CQLTester +{ + @Parameterized.Parameter + public ProtocolVersion version; + + @Parameterized.Parameters(name="{0}") + public static Collection<Object[]> versions() + { + return ProtocolVersion.SUPPORTED.stream() + .map(v -> new Object[]{v}) + .collect(Collectors.toList()); + } + + private static final AtomicLong TICK = new AtomicLong(0); + + private static final Ticker TICKER = new Ticker() + { + @Override + public long read() + { + return TICK.get(); + } + }; + + @BeforeClass + public static void setLimits() + { + DatabaseDescriptor.setNativeTransportReceiveQueueCapacityInBytes(1); + requireNetworkWithoutDriver(); + } + + @Before + public void resetLimits() + { + ClientResourceLimits.setGlobalLimit(Long.MAX_VALUE); + ClientResourceLimits.GLOBAL_REQUEST_LIMITER.reset(1000, 0.0, Ticker.systemTicker()); + } + + // TODO: Test large messages and back-pressure... + + @Test + public void shouldThrowOnOverload() throws IOException + { + try (SimpleClient client = client().connect(false, true)) + { + QueryMessage queryMessage = new QueryMessage("CREATE TABLE IF NOT EXISTS " + KEYSPACE + ".atable (pk int PRIMARY KEY, v text)", queryOptions()); + client.execute(queryMessage); + + double permitsPerSecond = 1000.0; + double burstSeconds = 0.0; + ClientResourceLimits.GLOBAL_REQUEST_LIMITER.reset(permitsPerSecond, burstSeconds, TICKER); + + try + { + client.execute(new QueryMessage("INSERT INTO " + KEYSPACE + ".atable (pk, v) VALUES (1, 'foo')", queryOptions())); + client.execute(new QueryMessage("INSERT INTO " + KEYSPACE + ".atable (pk, v) VALUES (2, 'bar')", queryOptions())); Review comment: For V3 and V4, this second query, as expected, triggers the rater-limiter and the `OverloadedException` is caught below. In the V5 and V6 case, it seems like the `OverloadedException` is never properly packaged as an `ErrorMessage` and picked up by the `SimpleClient`. The client just apprehends a timeout, and the logs show something like this: ``` WARN [SimpleClient-nioEventLoopGroup:1] 2021-06-09 19:39:15,188 DefaultChannelPipeline.java:1152 - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception. java.lang.RuntimeException: Unexpected error at org.apache.cassandra.transport.SimpleClient$InitialHandler.lambda$configureModernPipeline$1(SimpleClient.java:442) at org.apache.cassandra.transport.CQLMessageHandler.handleError(CQLMessageHandler.java:344) at org.apache.cassandra.transport.CQLMessageHandler.handleError(CQLMessageHandler.java:327) at org.apache.cassandra.transport.CQLMessageHandler.discardAndThrow(CQLMessageHandler.java:196) at org.apache.cassandra.transport.CQLMessageHandler.processOneContainedMessage(CQLMessageHandler.java:164) at org.apache.cassandra.net.AbstractMessageHandler.processFrameOfContainedMessages(AbstractMessageHandler.java:239) at org.apache.cassandra.net.AbstractMessageHandler.processIntactFrame(AbstractMessageHandler.java:224) at org.apache.cassandra.net.AbstractMessageHandler.process(AbstractMessageHandler.java:215) at org.apache.cassandra.transport.CQLMessageHandler.process(CQLMessageHandler.java:139) at org.apache.cassandra.net.FrameDecoder.deliver(FrameDecoder.java:322) at org.apache.cassandra.net.FrameDecoder.channelRead(FrameDecoder.java:286) at org.apache.cassandra.net.FrameDecoder.channelRead(FrameDecoder.java:269) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748) Caused by: org.apache.cassandra.transport.messages.ErrorMessage$WrappedException: org.apache.cassandra.exceptions.OverloadedException: Server is in overloaded state. Cannot accept more requests at this point. at org.apache.cassandra.transport.messages.ErrorMessage.wrap(ErrorMessage.java:470) ... 27 common frames omitted Caused by: org.apache.cassandra.exceptions.OverloadedException: Server is in overloaded state. Cannot accept more requests at this point. ... 25 common frames omitted ``` -- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

