Search in sources :

Example 6 with OverloadedException

use of org.apache.cassandra.exceptions.OverloadedException in project cassandra by apache.

the class ClientResourceLimitsTest method testChangingLimitsAtRuntime.

@Test
public void testChangingLimitsAtRuntime() {
    SimpleClient client = client(true);
    try {
        QueryMessage smallMessage = new QueryMessage(String.format("CREATE TABLE %s.atable (pk int PRIMARY KEY, v text)", KEYSPACE), V5_DEFAULT_OPTIONS);
        client.execute(smallMessage);
        try {
            client.execute(queryMessage());
            fail();
        } catch (RuntimeException e) {
            assertTrue(e.getCause() instanceof OverloadedException);
        }
        // change global limit, query will still fail because endpoint limit
        ClientResourceLimits.setGlobalLimit(HIGH_LIMIT);
        Assert.assertEquals("new global limit not returned by EndpointPayloadTrackers", HIGH_LIMIT, ClientResourceLimits.getGlobalLimit());
        Assert.assertEquals("new global limit not returned by DatabaseDescriptor", HIGH_LIMIT, DatabaseDescriptor.getNativeTransportMaxConcurrentRequestsInBytes());
        try {
            client.execute(queryMessage());
            fail();
        } catch (RuntimeException e) {
            assertTrue(e.getCause() instanceof OverloadedException);
        }
        // change endpoint limit, query will now succeed
        ClientResourceLimits.setEndpointLimit(HIGH_LIMIT);
        Assert.assertEquals("new endpoint limit not returned by EndpointPayloadTrackers", HIGH_LIMIT, ClientResourceLimits.getEndpointLimit());
        Assert.assertEquals("new endpoint limit not returned by DatabaseDescriptor", HIGH_LIMIT, DatabaseDescriptor.getNativeTransportMaxConcurrentRequestsInBytesPerIp());
        client.execute(queryMessage());
        // ensure new clients also see the new raised limits
        client.close();
        client = client(true);
        client.execute(queryMessage());
        // lower the global limit and ensure the query fails again
        ClientResourceLimits.setGlobalLimit(LOW_LIMIT);
        Assert.assertEquals("new global limit not returned by EndpointPayloadTrackers", LOW_LIMIT, ClientResourceLimits.getGlobalLimit());
        Assert.assertEquals("new global limit not returned by DatabaseDescriptor", LOW_LIMIT, DatabaseDescriptor.getNativeTransportMaxConcurrentRequestsInBytes());
        try {
            client.execute(queryMessage());
            fail();
        } catch (RuntimeException e) {
            assertTrue(e.getCause() instanceof OverloadedException);
        }
        // lower the endpoint limit and ensure existing clients also have requests that fail
        ClientResourceLimits.setEndpointLimit(60);
        Assert.assertEquals("new endpoint limit not returned by EndpointPayloadTrackers", 60, ClientResourceLimits.getEndpointLimit());
        Assert.assertEquals("new endpoint limit not returned by DatabaseDescriptor", 60, DatabaseDescriptor.getNativeTransportMaxConcurrentRequestsInBytesPerIp());
        try {
            client.execute(smallMessage);
            fail();
        } catch (RuntimeException e) {
            assertTrue(e.getCause() instanceof OverloadedException);
        }
        // ensure new clients also see the new lowered limit
        client.close();
        client = client(true);
        try {
            client.execute(smallMessage);
            fail();
        } catch (RuntimeException e) {
            assertTrue(e.getCause() instanceof OverloadedException);
        }
        // put the test state back
        ClientResourceLimits.setEndpointLimit(LOW_LIMIT);
        Assert.assertEquals("new endpoint limit not returned by EndpointPayloadTrackers", LOW_LIMIT, ClientResourceLimits.getEndpointLimit());
        Assert.assertEquals("new endpoint limit not returned by DatabaseDescriptor", LOW_LIMIT, DatabaseDescriptor.getNativeTransportMaxConcurrentRequestsInBytesPerIp());
    } finally {
        client.close();
    }
}
Also used : QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) OverloadedException(org.apache.cassandra.exceptions.OverloadedException)

Example 7 with OverloadedException

use of org.apache.cassandra.exceptions.OverloadedException in project cassandra by apache.

the class RateLimitingTest method testBytesInFlightOverload.

private void testBytesInFlightOverload(int payloadSize) throws Exception {
    try (SimpleClient client = client().connect(false, true)) {
        StorageService.instance.setNativeTransportRateLimitingEnabled(false);
        QueryMessage queryMessage = new QueryMessage("CREATE TABLE IF NOT EXISTS " + KEYSPACE + ".atable (pk int PRIMARY KEY, v text)", queryOptions());
        client.execute(queryMessage);
        StorageService.instance.setNativeTransportRateLimitingEnabled(true);
        ClientResourceLimits.GLOBAL_REQUEST_LIMITER.setRate(OVERLOAD_PERMITS_PER_SECOND, ticker);
        ClientResourceLimits.setGlobalLimit(1);
        try {
            // The first query takes the one available permit, but should fail on the bytes in flight limit.
            client.execute(queryMessage(payloadSize));
        } catch (RuntimeException e) {
            assertTrue(Throwables.anyCauseMatches(e, cause -> cause instanceof OverloadedException));
        }
    } finally {
        // Sanity check bytes in flight limiter.
        assertEquals(0, ClientResourceLimits.getCurrentGlobalUsage());
        StorageService.instance.setNativeTransportRateLimitingEnabled(false);
    }
}
Also used : QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) OverloadedException(org.apache.cassandra.exceptions.OverloadedException)

Aggregations

OverloadedException (org.apache.cassandra.exceptions.OverloadedException)7 QueryMessage (org.apache.cassandra.transport.messages.QueryMessage)4 ArrayList (java.util.ArrayList)2 CounterMutation (org.apache.cassandra.db.CounterMutation)2 IMutation (org.apache.cassandra.db.IMutation)2 CasWriteTimeoutException (org.apache.cassandra.exceptions.CasWriteTimeoutException)2 UnavailableException (org.apache.cassandra.exceptions.UnavailableException)2 WriteFailureException (org.apache.cassandra.exceptions.WriteFailureException)2 WriteTimeoutException (org.apache.cassandra.exceptions.WriteTimeoutException)2 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 ByteBuf (io.netty.buffer.ByteBuf)1 InetAddress (java.net.InetAddress)1 ServerSocket (java.net.ServerSocket)1 java.util (java.util)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 java.util.concurrent (java.util.concurrent)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1