Search in sources :

Example 26 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project azure-sdk-for-java by Azure.

the class TaskOperations method createTasks.

/**
     * Adds multiple tasks to a job.
     *
     * @param jobId The ID of the job to which to add the task.
     * @param taskList A collection of {@link CloudTask tasks} to add.
     * @param additionalBehaviors A collection of {@link BatchClientBehavior} instances that are applied to the Batch service request.
     * @throws BatchErrorException Exception thrown from REST call
     * @throws IOException Exception thrown from serialization/deserialization
     * @throws InterruptedException exception thrown if any thread has interrupted the current thread.
     */
public void createTasks(String jobId, List<TaskAddParameter> taskList, Iterable<BatchClientBehavior> additionalBehaviors) throws BatchErrorException, IOException, InterruptedException {
    BehaviorManager bhMgr = new BehaviorManager(this.customBehaviors(), additionalBehaviors);
    // Default thread number is 1
    int threadNumber = 1;
    // Get user defined thread number
    for (BatchClientBehavior op : bhMgr.getMasterListOfBehaviors()) {
        if (op instanceof BatchClientParallelOptions) {
            threadNumber = ((BatchClientParallelOptions) op).maxDegreeOfParallelism();
            break;
        }
    }
    final Object lock = new Object();
    ConcurrentLinkedQueue<TaskAddParameter> pendingList = new ConcurrentLinkedQueue<>(taskList);
    CopyOnWriteArrayList<TaskAddResult> failures = new CopyOnWriteArrayList<>();
    Map<Thread, WorkingThread> threads = new HashMap<>();
    Exception innerException = null;
    while (!pendingList.isEmpty()) {
        if (threads.size() < threadNumber) {
            // Kick as many as possible add tasks requests by max allowed threads
            WorkingThread worker = new WorkingThread(this._parentBatchClient, bhMgr, jobId, pendingList, failures, lock);
            Thread thread = new Thread(worker);
            thread.start();
            threads.put(thread, worker);
        } else {
            // Wait any thread finished
            synchronized (lock) {
                lock.wait();
            }
            List<Thread> finishedThreads = new ArrayList<>();
            for (Thread t : threads.keySet()) {
                if (t.getState() == Thread.State.TERMINATED) {
                    finishedThreads.add(t);
                    // If any exception happened, do not care the left requests
                    innerException = threads.get(t).getException();
                    if (innerException != null) {
                        break;
                    }
                }
            }
            // Free thread pool, so we can start more threads to send the request
            threads.keySet().removeAll(finishedThreads);
            // Any errors happened, we stop
            if (innerException != null || !failures.isEmpty()) {
                break;
            }
        }
    }
    // May sure all the left threads finished
    for (Thread t : threads.keySet()) {
        t.join();
    }
    if (innerException == null) {
        // Anything bad happened at the left threads?
        for (Thread t : threads.keySet()) {
            innerException = threads.get(t).getException();
            if (innerException != null) {
                break;
            }
        }
    }
    if (innerException != null) {
        // We throw any exception happened in sub thread
        if (innerException instanceof BatchErrorException) {
            throw (BatchErrorException) innerException;
        } else {
            throw (IOException) innerException;
        }
    }
    if (!failures.isEmpty()) {
        // Report any client error with leftover request
        List<TaskAddParameter> notFinished = new ArrayList<>();
        for (TaskAddParameter param : pendingList) {
            notFinished.add(param);
        }
        throw new CreateTasksTerminatedException("At least one task failed to be added.", failures, notFinished);
    }
// We succeed here
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BatchClientParallelOptions(com.microsoft.azure.batch.interceptor.BatchClientParallelOptions) IOException(java.io.IOException) IOException(java.io.IOException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 27 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project intellij-community by JetBrains.

the class RendererConfiguration method clone.

public RendererConfiguration clone() {
    RendererConfiguration result = null;
    try {
        result = (RendererConfiguration) super.clone();
    } catch (CloneNotSupportedException e) {
        LOG.error(e);
    }
    result.myRepresentationNodes = new CopyOnWriteArrayList<>();
    final ArrayList<NodeRenderer> cloned = new ArrayList<>();
    for (NodeRenderer renderer : myRepresentationNodes) {
        cloned.add((NodeRenderer) renderer.clone());
    }
    result.setRenderers(cloned);
    return result;
}
Also used : NodeRenderer(com.intellij.debugger.ui.tree.render.NodeRenderer) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 28 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project AntennaPod by AntennaPod.

the class CastManager method onQueueUpdated.

/*
    * This is called by onQueueStatusUpdated() of RemoteMediaPlayer
    */
private void onQueueUpdated(List<MediaQueueItem> queueItems, MediaQueueItem item, int repeatMode, boolean shuffle) {
    Log.d(TAG, "onQueueUpdated() reached");
    Log.d(TAG, String.format("Queue Items size: %d, Item: %s, Repeat Mode: %d, Shuffle: %s", queueItems == null ? 0 : queueItems.size(), item, repeatMode, shuffle));
    if (queueItems != null) {
        mediaQueue = new MediaQueue(new CopyOnWriteArrayList<>(queueItems), item, shuffle, repeatMode);
    } else {
        mediaQueue = new MediaQueue(new CopyOnWriteArrayList<>(), null, false, MediaStatus.REPEAT_MODE_REPEAT_OFF);
    }
    for (CastConsumer consumer : castConsumers) {
        consumer.onMediaQueueUpdated(queueItems, item, repeatMode, shuffle);
    }
}
Also used : MediaQueue(com.google.android.libraries.cast.companionlibrary.cast.MediaQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 29 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project lucene-solr by apache.

the class FastLRUCache method initializeMetrics.

@Override
public void initializeMetrics(SolrMetricManager manager, String registryName, String scope) {
    registry = manager.registry(registryName);
    cacheMap = new MetricsMap((detailed, map) -> {
        if (cache != null) {
            ConcurrentLRUCache.Stats stats = cache.getStats();
            long lookups = stats.getCumulativeLookups();
            long hits = stats.getCumulativeHits();
            long inserts = stats.getCumulativePuts();
            long evictions = stats.getCumulativeEvictions();
            long size = stats.getCurrentSize();
            long clookups = 0;
            long chits = 0;
            long cinserts = 0;
            long cevictions = 0;
            // NOTE: It is safe to iterate on a CopyOnWriteArrayList
            for (ConcurrentLRUCache.Stats statistiscs : statsList) {
                clookups += statistiscs.getCumulativeLookups();
                chits += statistiscs.getCumulativeHits();
                cinserts += statistiscs.getCumulativePuts();
                cevictions += statistiscs.getCumulativeEvictions();
            }
            map.put("lookups", lookups);
            map.put("hits", hits);
            map.put("hitratio", calcHitRatio(lookups, hits));
            map.put("inserts", inserts);
            map.put("evictions", evictions);
            map.put("size", size);
            map.put("warmupTime", warmupTime);
            map.put("cumulative_lookups", clookups);
            map.put("cumulative_hits", chits);
            map.put("cumulative_hitratio", calcHitRatio(clookups, chits));
            map.put("cumulative_inserts", cinserts);
            map.put("cumulative_evictions", cevictions);
            if (detailed && showItems != 0) {
                Map items = cache.getLatestAccessedItems(showItems == -1 ? Integer.MAX_VALUE : showItems);
                for (Map.Entry e : (Set<Map.Entry>) items.entrySet()) {
                    Object k = e.getKey();
                    Object v = e.getValue();
                    String ks = "item_" + k;
                    String vs = v.toString();
                    map.put(ks, vs);
                }
            }
        }
    });
    manager.registerGauge(this, registryName, cacheMap, true, scope, getCategory().toString());
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) MethodHandles(java.lang.invoke.MethodHandles) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) MetricsMap(org.apache.solr.metrics.MetricsMap) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) SolrException(org.apache.solr.common.SolrException) List(java.util.List) ConcurrentLRUCache(org.apache.solr.util.ConcurrentLRUCache) Map(java.util.Map) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MetricsMap(org.apache.solr.metrics.MetricsMap) MetricsMap(org.apache.solr.metrics.MetricsMap) Map(java.util.Map)

Example 30 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project lucene-solr by apache.

the class TestLockTree method testLocks.

public void testLocks() throws Exception {
    LockTree lockTree = new LockTree();
    Lock coll1Lock = lockTree.getSession().lock(CollectionAction.CREATE, Arrays.asList("coll1"));
    assertNotNull(coll1Lock);
    assertNull("Should not be able to lock coll1/shard1", lockTree.getSession().lock(CollectionAction.BALANCESHARDUNIQUE, Arrays.asList("coll1", "shard1")));
    assertNull(lockTree.getSession().lock(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2")));
    coll1Lock.unlock();
    Lock shard1Lock = lockTree.getSession().lock(CollectionAction.BALANCESHARDUNIQUE, Arrays.asList("coll1", "shard1"));
    assertNotNull(shard1Lock);
    shard1Lock.unlock();
    Lock replica1Lock = lockTree.getSession().lock(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2"));
    assertNotNull(replica1Lock);
    List<Pair<CollectionAction, List<String>>> operations = new ArrayList<>();
    operations.add(new Pair<>(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2")));
    operations.add(new Pair<>(MODIFYCOLLECTION, Arrays.asList("coll1")));
    operations.add(new Pair<>(SPLITSHARD, Arrays.asList("coll1", "shard1")));
    operations.add(new Pair<>(SPLITSHARD, Arrays.asList("coll2", "shard2")));
    operations.add(new Pair<>(MODIFYCOLLECTION, Arrays.asList("coll2")));
    operations.add(new Pair<>(DELETEREPLICA, Arrays.asList("coll2", "shard1")));
    List<Set<String>> orderOfExecution = Arrays.asList(ImmutableSet.of("coll1/shard1/core_node2", "coll2/shard2"), ImmutableSet.of("coll1", "coll2"), ImmutableSet.of("coll1/shard1", "coll2/shard1"));
    lockTree = new LockTree();
    for (int counter = 0; counter < orderOfExecution.size(); counter++) {
        LockTree.Session session = lockTree.getSession();
        List<Pair<CollectionAction, List<String>>> completedOps = new CopyOnWriteArrayList<>();
        List<Lock> locks = new CopyOnWriteArrayList<>();
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < operations.size(); i++) {
            Pair<CollectionAction, List<String>> operation = operations.get(i);
            final Lock lock = session.lock(operation.first(), operation.second());
            if (lock != null) {
                Thread thread = new Thread(getRunnable(completedOps, operation, locks, lock));
                threads.add(thread);
                thread.start();
            }
        }
        for (Thread thread : threads) thread.join();
        if (locks.isEmpty())
            throw new RuntimeException("Could not attain lock for anything " + operations);
        Set<String> expectedOps = orderOfExecution.get(counter);
        log.info("counter : {} , expected : {}, actual : {}", counter, expectedOps, locks);
        assertEquals(expectedOps.size(), locks.size());
        for (Lock lock : locks) assertTrue("locks : " + locks + " expectedOps : " + expectedOps, expectedOps.contains(lock.toString()));
        locks.clear();
        for (Pair<CollectionAction, List<String>> completedOp : completedOps) {
            operations.remove(completedOp);
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Lock(org.apache.solr.cloud.OverseerMessageHandler.Lock) CollectionAction(org.apache.solr.common.params.CollectionParams.CollectionAction) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Pair(org.apache.solr.common.util.Pair) 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