Search in sources :

Example 1 with Lock

use of org.apache.solr.cloud.OverseerMessageHandler.Lock 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

ImmutableSet (com.google.common.collect.ImmutableSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Lock (org.apache.solr.cloud.OverseerMessageHandler.Lock)1 CollectionAction (org.apache.solr.common.params.CollectionParams.CollectionAction)1 Pair (org.apache.solr.common.util.Pair)1