Search in sources :

Example 21 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project archaius by Netflix.

the class ConcurrentMapConfiguration method addPropertyDirect.

protected void addPropertyDirect(String key, Object value) {
    ReentrantLock lock = locks[Math.abs(key.hashCode()) % NUM_LOCKS];
    lock.lock();
    try {
        Object previousValue = map.putIfAbsent(key, value);
        if (previousValue == null) {
            return;
        }
        if (previousValue instanceof List) {
            // the value is added to the existing list
            ((List) previousValue).add(value);
        } else {
            // the previous value is replaced by a list containing the previous value and the new value
            List<Object> list = new CopyOnWriteArrayList<Object>();
            list.add(previousValue);
            list.add(value);
            map.put(key, list);
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 22 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project archaius by Netflix.

the class DynamicURLConfigurationTestWithFileURL method testFileURLWithPropertiesUpdatedDynamically.

@Test
public void testFileURLWithPropertiesUpdatedDynamically() throws IOException, InterruptedException {
    File file = File.createTempFile("DynamicURLConfigurationTestWithFileURL", "testFileURLWithPropertiesUpdatedDynamically");
    populateFile(file, "test.host=12312,123213", "test.host1=13212");
    AbstractConfiguration.setDefaultListDelimiter(',');
    DynamicURLConfiguration config = new DynamicURLConfiguration(0, 500, false, file.toURI().toString());
    Thread.sleep(1000);
    Assert.assertEquals(13212, config.getInt("test.host1"));
    Thread.sleep(1000);
    populateFile(file, "test.host=12312,123213", "test.host1=13212");
    populateFile(file, "test.host=12312,123213", "test.host1=13212");
    CopyOnWriteArrayList writeList = new CopyOnWriteArrayList();
    writeList.add("12312");
    writeList.add("123213");
    config.setProperty("sample.domain", "google,yahoo");
    Assert.assertEquals(writeList, config.getProperty("test.host"));
}
Also used : DynamicURLConfiguration(com.netflix.config.DynamicURLConfiguration) File(java.io.File) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 23 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project pinpoint by naver.

the class ActiveMQClientITBase method testQueuePush.

@Test
public void testQueuePush() throws Exception {
    // Given
    final String testQueueName = "TestPushQueue";
    final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
    final String testMessage = "Hello World for Queue!";
    final CountDownLatch consumerLatch = new CountDownLatch(1);
    final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>();
    // create producer
    ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    MessageProducer producer = producerSession.createProducer(testQueue);
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // create consumer
    ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    MessageConsumer consumer = consumerSession.createConsumer(testQueue);
    consumer.setMessageListener(new AssertTextMessageListener(consumerLatch, consumerThrowables, expectedTextMessage));
    // When
    producer.send(expectedTextMessage);
    consumerLatch.await(1L, TimeUnit.SECONDS);
    // Then
    assertNoConsumerError(consumerThrowables);
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(2, 5000L);
    // trace count : 1
    verifyProducerSendEvent(testQueue);
    // trace count : 1
    verifyConsumerPushEvent(testQueue);
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQSession(org.apache.activemq.ActiveMQSession) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) AssertTextMessageListener(com.navercorp.pinpoint.plugin.jdk7.activemq.client.util.AssertTextMessageListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 24 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project pinpoint by naver.

the class ActiveMQClientITBase method testTopicPush.

@Test
public void testTopicPush() throws Exception {
    // Given
    final String testTopicName = "TestPushTopic";
    final ActiveMQTopic testTopic = new ActiveMQTopic(testTopicName);
    final String testMessage = "Hello World for Topic!";
    final int numMessageConsumers = 2;
    final CountDownLatch consumerConsumeLatch = new CountDownLatch(numMessageConsumers);
    final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>();
    // create producer
    ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    MessageProducer producer = new MessageProducerBuilder(producerSession, testTopic).waitTillStarted().build();
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // create 2 consumers
    ActiveMQSession consumer1Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    new MessageConsumerBuilder(consumer1Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build();
    ActiveMQSession consumer2Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    new MessageConsumerBuilder(consumer2Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build();
    // When
    producer.send(expectedTextMessage);
    consumerConsumeLatch.await(1L, TimeUnit.SECONDS);
    // Then
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(3, 1000L);
    // trace count : 1
    verifyProducerSendEvent(testTopic);
    // trace count : 1
    verifyConsumerPushEvent(testTopic);
    // trace count : 1
    verifyConsumerPushEvent(testTopic);
}
Also used : ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) MessageConsumerBuilder(com.navercorp.pinpoint.plugin.jdk7.activemq.client.util.MessageConsumerBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) MessageProducerBuilder(com.navercorp.pinpoint.plugin.jdk7.activemq.client.util.MessageProducerBuilder) ActiveMQSession(org.apache.activemq.ActiveMQSession) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) AssertTextMessageListener(com.navercorp.pinpoint.plugin.jdk7.activemq.client.util.AssertTextMessageListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 25 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project druid by druid-io.

the class ZkCoordinator method addSegments.

private void addSegments(Collection<DataSegment> segments, final DataSegmentChangeCallback callback) {
    ExecutorService loadingExecutor = null;
    try (final BackgroundSegmentAnnouncer backgroundSegmentAnnouncer = new BackgroundSegmentAnnouncer(announcer, exec, config.getAnnounceIntervalMillis())) {
        backgroundSegmentAnnouncer.startAnnouncing();
        loadingExecutor = Execs.multiThreaded(config.getNumBootstrapThreads(), "ZkCoordinator-loading-%s");
        final int numSegments = segments.size();
        final CountDownLatch latch = new CountDownLatch(numSegments);
        final AtomicInteger counter = new AtomicInteger(0);
        final CopyOnWriteArrayList<DataSegment> failedSegments = new CopyOnWriteArrayList<>();
        for (final DataSegment segment : segments) {
            loadingExecutor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        log.info("Loading segment[%d/%d][%s]", counter.getAndIncrement(), numSegments, segment.getIdentifier());
                        loadSegment(segment, callback);
                        if (!announcer.isAnnounced(segment)) {
                            try {
                                backgroundSegmentAnnouncer.announceSegment(segment);
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                                throw new SegmentLoadingException(e, "Loading Interrupted");
                            }
                        }
                    } catch (SegmentLoadingException e) {
                        log.error(e, "[%s] failed to load", segment.getIdentifier());
                        failedSegments.add(segment);
                    } finally {
                        latch.countDown();
                    }
                }
            });
        }
        try {
            latch.await();
            if (failedSegments.size() > 0) {
                log.makeAlert("%,d errors seen while loading segments", failedSegments.size()).addData("failedSegments", failedSegments);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            log.makeAlert(e, "LoadingInterrupted");
        }
        backgroundSegmentAnnouncer.finishAnnouncing();
    } catch (SegmentLoadingException e) {
        log.makeAlert(e, "Failed to load segments -- likely problem with announcing.").addData("numSegments", segments.size()).emit();
    } finally {
        callback.execute();
        if (loadingExecutor != null) {
            loadingExecutor.shutdownNow();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SegmentLoadingException(io.druid.segment.loading.SegmentLoadingException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) DataSegment(io.druid.timeline.DataSegment) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)304 CountDownLatch (java.util.concurrent.CountDownLatch)84 ArrayList (java.util.ArrayList)83 List (java.util.List)76 Test (org.junit.Test)71 IOException (java.io.IOException)53 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 HashMap (java.util.HashMap)24 Map (java.util.Map)24 ExecutionException (java.util.concurrent.ExecutionException)23 LinkedList (java.util.LinkedList)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 Set (java.util.Set)18 TimeUnit (java.util.concurrent.TimeUnit)18 ClientRequest (io.undertow.client.ClientRequest)17 Test (org.junit.jupiter.api.Test)17 HashSet (java.util.HashSet)16 ExecutorService (java.util.concurrent.ExecutorService)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)16 ClientConnection (io.undertow.client.ClientConnection)15