Search in sources :

Example 1 with ConcurrentHashSet

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

the class TimeAndCounterIDGeneratorTest method testCalculationOnMultiThread.

@Test
public void testCalculationOnMultiThread() throws Throwable {
    final ConcurrentHashSet<Long> hashSet = new ConcurrentHashSet<>();
    final TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
    System.out.println("Time = " + TimeAndCounterIDGeneratorTest.hex(System.currentTimeMillis()) + ", " + seq);
    final int NUMBER_OF_THREADS = 100;
    final int NUMBER_OF_IDS = 10;
    final CountDownLatch latchAlign = new CountDownLatch(NUMBER_OF_THREADS);
    final CountDownLatch latchStart = new CountDownLatch(1);
    class T1 extends Thread {

        Throwable e;

        @Override
        public void run() {
            try {
                latchAlign.countDown();
                assertTrue("Latch has got to return within a minute", latchStart.await(1, TimeUnit.MINUTES));
                long lastValue = 0L;
                for (int i = 0; i < NUMBER_OF_IDS; i++) {
                    long value = seq.generateID();
                    Assert.assertTrue(TimeAndCounterIDGeneratorTest.hex(value) + " should be greater than " + TimeAndCounterIDGeneratorTest.hex(lastValue) + " on seq " + seq.toString(), value > lastValue);
                    lastValue = value;
                    hashSet.add(value);
                }
            } catch (Throwable e) {
                this.e = e;
            }
        }
    }
    T1[] arrays = new T1[NUMBER_OF_THREADS];
    for (int i = 0; i < arrays.length; i++) {
        arrays[i] = new T1();
        arrays[i].start();
    }
    assertTrue("Latch has got to return within a minute", latchAlign.await(1, TimeUnit.MINUTES));
    latchStart.countDown();
    for (T1 t : arrays) {
        t.join();
        if (t.e != null) {
            throw t.e;
        }
    }
    Assert.assertEquals(NUMBER_OF_THREADS * NUMBER_OF_IDS, hashSet.size());
    hashSet.clear();
}
Also used : TimeAndCounterIDGenerator(org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator) ConcurrentHashSet(org.apache.activemq.artemis.utils.collections.ConcurrentHashSet) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ConcurrentHashSet

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

the class ConsumerTest method testReceiveAndResend.

@Test
public void testReceiveAndResend() throws Exception {
    final Set<Object> sessions = new ConcurrentHashSet<>();
    final AtomicInteger errors = new AtomicInteger(0);
    final SimpleString QUEUE_RESPONSE = SimpleString.toSimpleString("QUEUE_RESPONSE");
    final int numberOfSessions = 50;
    final int numberOfMessages = 10;
    final CountDownLatch latchReceive = new CountDownLatch(numberOfSessions * numberOfMessages);
    ClientSessionFactory sf = locator.createSessionFactory();
    for (int i = 0; i < numberOfSessions; i++) {
        ClientSession session = sf.createSession(false, true, true);
        sessions.add(session);
        session.createQueue(QUEUE, QUEUE.concat("" + i), null, true);
        if (i == 0) {
            session.createQueue(QUEUE_RESPONSE, QUEUE_RESPONSE);
        }
        ClientConsumer consumer = session.createConsumer(QUEUE.concat("" + i));
        sessions.add(consumer);
        {
            consumer.setMessageHandler(new MessageHandler() {

                @Override
                public void onMessage(final ClientMessage msg) {
                    try {
                        ServerLocator locatorSendx = createFactory(isNetty()).setReconnectAttempts(15);
                        ClientSessionFactory factoryx = locatorSendx.createSessionFactory();
                        ClientSession sessionSend = factoryx.createSession(true, true);
                        sessions.add(sessionSend);
                        sessions.add(locatorSendx);
                        sessions.add(factoryx);
                        final ClientProducer prod = sessionSend.createProducer(QUEUE_RESPONSE);
                        sessionSend.start();
                        sessions.add(prod);
                        msg.acknowledge();
                        prod.send(sessionSend.createMessage(true));
                        prod.close();
                        sessionSend.commit();
                        sessionSend.close();
                        factoryx.close();
                        if (Thread.currentThread().isInterrupted()) {
                            System.err.println("Netty has interrupted a thread!!!");
                            errors.incrementAndGet();
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                        errors.incrementAndGet();
                    } finally {
                        latchReceive.countDown();
                    }
                }
            });
        }
        session.start();
    }
    Thread tCons = new Thread() {

        @Override
        public void run() {
            try {
                final ServerLocator locatorSend = createFactory(isNetty());
                final ClientSessionFactory factory = locatorSend.createSessionFactory();
                final ClientSession sessionSend = factory.createSession(true, true);
                ClientConsumer cons = sessionSend.createConsumer(QUEUE_RESPONSE);
                sessionSend.start();
                for (int i = 0; i < numberOfMessages * numberOfSessions; i++) {
                    ClientMessage msg = cons.receive(5000);
                    if (msg == null) {
                        break;
                    }
                    msg.acknowledge();
                }
                if (cons.receiveImmediate() != null) {
                    System.out.println("ERROR: Received an extra message");
                    errors.incrementAndGet();
                }
                sessionSend.close();
                factory.close();
                locatorSend.close();
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            }
        }
    };
    tCons.start();
    ClientSession mainSessionSend = sf.createSession(true, true);
    ClientProducer mainProd = mainSessionSend.createProducer(QUEUE);
    for (int i = 0; i < numberOfMessages; i++) {
        mainProd.send(mainSessionSend.createMessage(true));
    }
    latchReceive.await(2, TimeUnit.MINUTES);
    tCons.join();
    sf.close();
    assertEquals("Had errors along the execution", 0, errors.get());
}
Also used : MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) JMSException(javax.jms.JMSException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashSet(org.apache.activemq.artemis.utils.collections.ConcurrentHashSet) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 3 with ConcurrentHashSet

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

the class SlowConsumerTest method testMultipleConsumersOneQueue.

/**
 * This test creates 3 consumers on one queue. A producer sends
 * messages at a rate of 2 messages per second. Each consumer
 * consumes messages at rate of 1 message per second. The slow
 * consumer threshold is 1 message per second.
 * Based on the above settings, slow consumer removal will not
 * be performed (2 < 3*1), so no consumer will be removed during the
 * test, and all messages will be received.
 */
@Test
public void testMultipleConsumersOneQueue() throws Exception {
    locator.setAckBatchSize(0);
    Queue queue = server.locateQueue(QUEUE);
    ClientSessionFactory sf1 = createSessionFactory(locator);
    ClientSessionFactory sf2 = createSessionFactory(locator);
    ClientSessionFactory sf3 = createSessionFactory(locator);
    ClientSessionFactory sf4 = createSessionFactory(locator);
    final int messages = 10 * threshold;
    FixedRateProducer producer = new FixedRateProducer(threshold * 2, sf1, QUEUE, messages);
    final Set<FixedRateConsumer> consumers = new ConcurrentHashSet<>();
    final Set<ClientMessage> receivedMessages = new ConcurrentHashSet<>();
    consumers.add(new FixedRateConsumer(threshold, receivedMessages, sf2, QUEUE, 1));
    consumers.add(new FixedRateConsumer(threshold, receivedMessages, sf3, QUEUE, 2));
    consumers.add(new FixedRateConsumer(threshold, receivedMessages, sf4, QUEUE, 3));
    try {
        producer.start();
        for (FixedRateConsumer consumer : consumers) {
            consumer.start();
        }
        producer.join(10000);
        assertTrue(TimeUtils.waitOnBoolean(true, 10000, () -> receivedMessages.size() == messages));
        Assert.assertEquals(3, queue.getConsumerCount());
    } finally {
        producer.stopRunning();
        Assert.assertFalse(producer.failed);
        for (FixedRateConsumer consumer : consumers) {
            consumer.stopRunning();
            Assert.assertFalse(consumer.failed);
        }
        logger.debug("***report messages received: " + receivedMessages.size());
        logger.debug("***consumers left: " + consumers.size());
    }
}
Also used : ConcurrentHashSet(org.apache.activemq.artemis.utils.collections.ConcurrentHashSet) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Queue(org.apache.activemq.artemis.core.server.Queue) Test(org.junit.Test)

Example 4 with ConcurrentHashSet

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

the class SecurityStoreImpl method check.

@Override
public void check(final SimpleString address, final SimpleString queue, final CheckType checkType, final SecurityAuth session) throws Exception {
    if (securityEnabled) {
        if (logger.isTraceEnabled()) {
            logger.trace("checking access permissions to " + address);
        }
        String user = session.getUsername();
        if (checkCached(address, user, checkType)) {
            // OK
            return;
        }
        String saddress = address.toString();
        Set<Role> roles = securityRepository.getMatch(saddress);
        // bypass permission checks for management cluster user
        if (managementClusterUser.equals(user) && session.getPassword().equals(managementClusterPassword)) {
            return;
        }
        final boolean validated;
        if (securityManager instanceof ActiveMQSecurityManager3) {
            final ActiveMQSecurityManager3 securityManager3 = (ActiveMQSecurityManager3) securityManager;
            validated = securityManager3.validateUserAndRole(user, session.getPassword(), roles, checkType, saddress, session.getRemotingConnection()) != null;
        } else if (securityManager instanceof ActiveMQSecurityManager2) {
            final ActiveMQSecurityManager2 securityManager2 = (ActiveMQSecurityManager2) securityManager;
            validated = securityManager2.validateUserAndRole(user, session.getPassword(), roles, checkType, saddress, session.getRemotingConnection());
        } else {
            validated = securityManager.validateUserAndRole(user, session.getPassword(), roles, checkType);
        }
        if (!validated) {
            if (notificationService != null) {
                TypedProperties props = new TypedProperties();
                props.putSimpleStringProperty(ManagementHelper.HDR_ADDRESS, address);
                props.putSimpleStringProperty(ManagementHelper.HDR_CHECK_TYPE, new SimpleString(checkType.toString()));
                props.putSimpleStringProperty(ManagementHelper.HDR_USER, SimpleString.toSimpleString(user));
                Notification notification = new Notification(null, CoreNotificationType.SECURITY_PERMISSION_VIOLATION, props);
                notificationService.sendNotification(notification);
            }
            if (queue == null) {
                throw ActiveMQMessageBundle.BUNDLE.userNoPermissions(session.getUsername(), checkType, saddress);
            } else {
                throw ActiveMQMessageBundle.BUNDLE.userNoPermissionsQueue(session.getUsername(), checkType, queue.toString(), saddress);
            }
        }
        // if we get here we're granted, add to the cache
        ConcurrentHashSet<SimpleString> set = new ConcurrentHashSet<>();
        ConcurrentHashSet<SimpleString> act = cache.putIfAbsent(user + "." + checkType.name(), set);
        if (act != null) {
            set = act;
        }
        set.add(address);
    }
}
Also used : Role(org.apache.activemq.artemis.core.security.Role) ActiveMQSecurityManager2(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager2) ConcurrentHashSet(org.apache.activemq.artemis.utils.collections.ConcurrentHashSet) ActiveMQSecurityManager3(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager3) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties) Notification(org.apache.activemq.artemis.core.server.management.Notification)

Aggregations

ConcurrentHashSet (org.apache.activemq.artemis.utils.collections.ConcurrentHashSet)4 Test (org.junit.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)2 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)2 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 JMSException (javax.jms.JMSException)1 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)1 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)1 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)1 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1 MessageHandler (org.apache.activemq.artemis.api.core.client.MessageHandler)1 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)1 Role (org.apache.activemq.artemis.core.security.Role)1 Queue (org.apache.activemq.artemis.core.server.Queue)1 Notification (org.apache.activemq.artemis.core.server.management.Notification)1 ActiveMQSecurityManager2 (org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager2)1