Search in sources :

Example 1 with TokenBucketLimiterImpl

use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.

the class ActiveMQSessionContext method createConsumer.

@Override
public ClientConsumerInternal createConsumer(SimpleString queueName, SimpleString filterString, int windowSize, int maxRate, int ackBatchSize, boolean browseOnly, Executor executor, Executor flowControlExecutor) throws ActiveMQException {
    long consumerID = idGenerator.generateID();
    ActiveMQConsumerContext consumerContext = new ActiveMQConsumerContext(consumerID);
    SessionCreateConsumerMessage request = new SessionCreateConsumerMessage(consumerID, queueName, filterString, browseOnly, true);
    SessionQueueQueryResponseMessage queueInfo;
    if (sessionChannel.getConnection().isVersionBeforeAddressChange()) {
        queueInfo = (SessionQueueQueryResponseMessage) sessionChannel.sendBlocking(request, PacketImpl.SESS_QUEUEQUERY_RESP_V2);
    } else {
        queueInfo = (SessionQueueQueryResponseMessage) sessionChannel.sendBlocking(request, PacketImpl.SESS_QUEUEQUERY_RESP_V3);
    }
    return new ClientConsumerImpl(session, consumerContext, queueName, filterString, browseOnly, calcWindowSize(windowSize), ackBatchSize, maxRate > 0 ? new TokenBucketLimiterImpl(maxRate, false) : null, executor, flowControlExecutor, this, queueInfo.toQueueQuery(), lookupTCCL());
}
Also used : ClientConsumerImpl(org.apache.activemq.artemis.core.client.impl.ClientConsumerImpl) SessionQueueQueryResponseMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionQueueQueryResponseMessage) TokenBucketLimiterImpl(org.apache.activemq.artemis.utils.TokenBucketLimiterImpl) SessionCreateConsumerMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionCreateConsumerMessage)

Example 2 with TokenBucketLimiterImpl

use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.

the class TokenBucketLimiterImplTest method testRate.

private void testRate(final int rate, final boolean spin) throws Exception {
    // Allow for 5% error
    final double error = 0.05;
    TokenBucketLimiterImpl tbl = new TokenBucketLimiterImpl(rate, spin);
    long start = System.currentTimeMillis();
    long count = 0;
    final long measureTime = 5000;
    // Do some initial testing ..   to ramp up the calculations
    while (System.currentTimeMillis() - start < measureTime) {
        tbl.limit();
    }
    // wait some time
    Thread.sleep(2000);
    // start measuring again
    start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < measureTime) {
        tbl.limit();
        // when using a low rate (1 for instance), the very last could come after or very close to 5 seconds
        // what could give us a false negative
        count++;
    }
    long end = System.currentTimeMillis();
    if (rate == 1) {
        // in 5 seconds you may get exactly 6 buckets..
        // Count... 1, 2, 3, 4, 5, 6
        // Time.... 0, 1, 2, 3, 4, 5
        // any variation on 1 could make the test to fail, for that reason we make it this way
        Assert.assertTrue(count == 5 || count == 6);
    } else {
        double actualRate = (double) (1000 * count) / measureTime;
        Assert.assertTrue("actual rate = " + actualRate + " expected=" + rate, actualRate > rate * (1 - error));
        Assert.assertTrue("actual rate = " + actualRate + " expected=" + rate, actualRate < rate * (1 + error));
    }
}
Also used : TokenBucketLimiterImpl(org.apache.activemq.artemis.utils.TokenBucketLimiterImpl)

Example 3 with TokenBucketLimiterImpl

use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.

the class TokenBucketLimiterImplTest method testVerifyRate.

public void testVerifyRate(final int rate, final int window, final int timeRunning) throws Exception {
    // Allow for 5% error
    final double error = 1.05;
    final AtomicBoolean running = new AtomicBoolean(true);
    final AtomicBoolean rateError = new AtomicBoolean(false);
    final AtomicInteger iterations = new AtomicInteger(0);
    TokenBucketLimiterImpl tbl = new TokenBucketLimiterImpl(rate, false, TimeUnit.SECONDS, window);
    Thread t = new Thread() {

        @Override
        public void run() {
            int lastRun = 0;
            long lastTime = System.currentTimeMillis();
            while (running.get()) {
                int tmpValue = iterations.get();
                if (lastRun != 0) {
                    int consumed = tmpValue - lastRun;
                    double calculatedRate = consumed * window * 1000 / ((System.currentTimeMillis() - lastTime));
                    if (calculatedRate > rate * error) {
                        System.out.println("got more than " + rate + " tokens / second");
                        rateError.set(true);
                    } else if (calculatedRate > rate) {
                        System.out.println("got more than " + rate + " tokens / second but still on the error marging" + "make sure it's ok though, if you see to many of this message it's an issue");
                    }
                    System.out.println("Rate = " + calculatedRate + " consumed = " + consumed);
                }
                lastTime = System.currentTimeMillis();
                lastRun = tmpValue;
                try {
                    Thread.sleep(window * 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();
    long timeout;
    timeout = System.currentTimeMillis() + 3000;
    while (timeout > System.currentTimeMillis()) {
        tbl.limit();
        iterations.incrementAndGet();
    }
    Thread.sleep(3000);
    timeout = System.currentTimeMillis() + timeRunning;
    while (timeout > System.currentTimeMillis()) {
        tbl.limit();
        iterations.incrementAndGet();
    }
    running.set(false);
    t.join();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TokenBucketLimiterImpl(org.apache.activemq.artemis.utils.TokenBucketLimiterImpl)

Example 4 with TokenBucketLimiterImpl

use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.

the class PerfBase method sendMessages.

private void sendMessages(final int numberOfMessages, final int txBatchSize, final boolean durable, final boolean transacted, final boolean display, final int throttleRate, final int messageSize) throws Exception {
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(perfParams.isDurable() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
    producer.setDisableMessageID(perfParams.isDisableMessageID());
    producer.setDisableMessageTimestamp(perfParams.isDisableTimestamp());
    BytesMessage message = session.createBytesMessage();
    byte[] payload = PerfBase.randomByteArray(messageSize);
    message.writeBytes(payload);
    final int modulo = 2000;
    TokenBucketLimiter tbl = throttleRate != -1 ? new TokenBucketLimiterImpl(throttleRate, false) : null;
    boolean committed = false;
    for (int i = 1; i <= numberOfMessages; i++) {
        producer.send(message);
        if (transacted) {
            if (i % txBatchSize == 0) {
                session.commit();
                committed = true;
            } else {
                committed = false;
            }
        }
        if (display && i % modulo == 0) {
            double duration = (1.0 * System.currentTimeMillis() - start) / 1000;
            PerfBase.log.info(String.format("sent %6d messages in %2.2fs", i, duration));
        }
        if (tbl != null) {
            tbl.limit();
        }
    }
    if (transacted && !committed) {
        session.commit();
    }
}
Also used : TokenBucketLimiter(org.apache.activemq.artemis.utils.TokenBucketLimiter) TokenBucketLimiterImpl(org.apache.activemq.artemis.utils.TokenBucketLimiterImpl) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer)

Example 5 with TokenBucketLimiterImpl

use of org.apache.activemq.artemis.utils.TokenBucketLimiterImpl in project activemq-artemis by apache.

the class HornetQClientSessionContext method createConsumer.

@Override
public ClientConsumerInternal createConsumer(SimpleString queueName, SimpleString filterString, int windowSize, int maxRate, int ackBatchSize, boolean browseOnly, Executor executor, Executor flowControlExecutor) throws ActiveMQException {
    long consumerID = idGenerator.generateID();
    ActiveMQConsumerContext consumerContext = new ActiveMQConsumerContext(consumerID);
    SessionCreateConsumerMessage request = new SessionCreateConsumerMessage(consumerID, queueName, filterString, browseOnly, true);
    SessionQueueQueryResponseMessage queueInfo = (SessionQueueQueryResponseMessage) getSessionChannel().sendBlocking(request, PacketImpl.SESS_QUEUEQUERY_RESP);
    return new ClientConsumerImpl(session, consumerContext, queueName, filterString, browseOnly, calcWindowSize(windowSize), ackBatchSize, maxRate > 0 ? new TokenBucketLimiterImpl(maxRate, false) : null, executor, flowControlExecutor, this, queueInfo.toQueueQuery(), lookupTCCL());
}
Also used : ClientConsumerImpl(org.apache.activemq.artemis.core.client.impl.ClientConsumerImpl) ActiveMQConsumerContext(org.apache.activemq.artemis.core.protocol.core.impl.ActiveMQConsumerContext) SessionQueueQueryResponseMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionQueueQueryResponseMessage) TokenBucketLimiterImpl(org.apache.activemq.artemis.utils.TokenBucketLimiterImpl) SessionCreateConsumerMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionCreateConsumerMessage)

Aggregations

TokenBucketLimiterImpl (org.apache.activemq.artemis.utils.TokenBucketLimiterImpl)6 BytesMessage (javax.jms.BytesMessage)2 ClientConsumerImpl (org.apache.activemq.artemis.core.client.impl.ClientConsumerImpl)2 SessionCreateConsumerMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionCreateConsumerMessage)2 SessionQueueQueryResponseMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionQueueQueryResponseMessage)2 TokenBucketLimiter (org.apache.activemq.artemis.utils.TokenBucketLimiter)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 JMSException (javax.jms.JMSException)1 MessageProducer (javax.jms.MessageProducer)1 NamingException (javax.naming.NamingException)1 ActiveMQConsumerContext (org.apache.activemq.artemis.core.protocol.core.impl.ActiveMQConsumerContext)1