Search in sources :

Example 16 with LongSet

use of com.carrotsearch.hppc.LongSet in project cassandra by apache.

the class TokenTreeTest method generateTree.

private static TokenTree generateTree(final long minToken, final long maxToken, boolean isStatic) throws IOException {
    final SortedMap<Long, LongSet> toks = new TreeMap<Long, LongSet>() {

        {
            for (long i = minToken; i <= maxToken; i++) {
                LongSet offsetSet = new LongOpenHashSet();
                offsetSet.add(i);
                put(i, offsetSet);
            }
        }
    };
    final TokenTreeBuilder builder = isStatic ? new StaticTokenTreeBuilder(new FakeCombinedTerm(toks)) : new DynamicTokenTreeBuilder(toks);
    builder.finish();
    final File treeFile = File.createTempFile("token-tree-get-test", "tt");
    treeFile.deleteOnExit();
    try (SequentialWriter writer = new SequentialWriter(treeFile, DEFAULT_OPT)) {
        builder.write(writer);
        writer.sync();
    }
    RandomAccessReader reader = null;
    try {
        reader = RandomAccessReader.open(treeFile);
        return new TokenTree(new MappedBuffer(reader));
    } finally {
        FileUtils.closeQuietly(reader);
    }
}
Also used : LongSet(com.carrotsearch.hppc.LongSet) SequentialWriter(org.apache.cassandra.io.util.SequentialWriter) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) LongOpenHashSet(com.carrotsearch.hppc.LongOpenHashSet) File(java.io.File) MappedBuffer(org.apache.cassandra.index.sasi.utils.MappedBuffer)

Example 17 with LongSet

use of com.carrotsearch.hppc.LongSet in project cassandra by apache.

the class TokenTreeTest method testMergingOfEqualTokenTrees.

public void testMergingOfEqualTokenTrees(SortedMap<Long, LongSet> tokensMap) throws Exception {
    TokenTreeBuilder tokensA = new DynamicTokenTreeBuilder(tokensMap);
    TokenTreeBuilder tokensB = new DynamicTokenTreeBuilder(tokensMap);
    TokenTree a = buildTree(tokensA);
    TokenTree b = buildTree(tokensB);
    TokenTreeBuilder tokensC = new StaticTokenTreeBuilder(new CombinedTerm(null, null) {

        public RangeIterator<Long, Token> getTokenIterator() {
            RangeIterator.Builder<Long, Token> union = RangeUnionIterator.builder();
            union.add(a.iterator(new KeyConverter()));
            union.add(b.iterator(new KeyConverter()));
            return union.build();
        }
    });
    TokenTree c = buildTree(tokensC);
    Assert.assertEquals(tokensMap.size(), c.getCount());
    Iterator<Token> tokenIterator = c.iterator(KEY_CONVERTER);
    Iterator<Map.Entry<Long, LongSet>> listIterator = tokensMap.entrySet().iterator();
    while (tokenIterator.hasNext() && listIterator.hasNext()) {
        Token treeNext = tokenIterator.next();
        Map.Entry<Long, LongSet> listNext = listIterator.next();
        Assert.assertEquals(listNext.getKey(), treeNext.get());
        Assert.assertEquals(convert(listNext.getValue()), convert(treeNext));
    }
    for (Map.Entry<Long, LongSet> entry : tokensMap.entrySet()) {
        TokenTree.OnDiskToken result = c.get(entry.getKey(), KEY_CONVERTER);
        Assert.assertNotNull("failed to find object for token " + entry.getKey(), result);
        LongSet found = result.getOffsets();
        Assert.assertEquals(entry.getValue(), found);
    }
}
Also used : RangeIterator(org.apache.cassandra.index.sasi.utils.RangeIterator) HashCodeBuilder(org.apache.commons.lang3.builder.HashCodeBuilder) LongSet(com.carrotsearch.hppc.LongSet) CombinedTerm(org.apache.cassandra.index.sasi.utils.CombinedTerm)

Example 18 with LongSet

use of com.carrotsearch.hppc.LongSet in project titan by thinkaurelius.

the class VertexIDAssignerTest method testIDAssignment.

@Test
public void testIDAssignment() {
    LongSet vertexIds = new LongHashSet();
    LongSet relationIds = new LongHashSet();
    int totalRelations = 0;
    int totalVertices = 0;
    for (int trial = 0; trial < 10; trial++) {
        for (boolean flush : new boolean[] { true, false }) {
            TitanGraph graph = getInMemoryGraph();
            int numVertices = 1000;
            List<TitanVertex> vertices = new ArrayList<TitanVertex>(numVertices);
            List<InternalRelation> relations = new ArrayList<InternalRelation>();
            TitanVertex old = null;
            totalRelations += 2 * numVertices;
            totalVertices += numVertices;
            try {
                for (int i = 0; i < numVertices; i++) {
                    TitanVertex next = graph.addVertex();
                    InternalRelation edge = null;
                    if (old != null) {
                        edge = (InternalRelation) old.addEdge("knows", next);
                    }
                    InternalRelation property = (InternalRelation) next.property("age", 25);
                    if (flush) {
                        idAssigner.assignID((InternalVertex) next, next.vertexLabel());
                        idAssigner.assignID(property);
                        if (edge != null)
                            idAssigner.assignID(edge);
                    }
                    relations.add(property);
                    if (edge != null)
                        relations.add(edge);
                    vertices.add(next);
                    old = next;
                }
                if (!flush)
                    idAssigner.assignIDs(relations);
                //Check if we should have exhausted the id pools
                if (totalRelations > maxIDAssignments || totalVertices > maxIDAssignments)
                    fail();
                //Verify that ids are set and unique
                for (TitanVertex v : vertices) {
                    assertTrue(v.hasId());
                    long id = v.longId();
                    assertTrue(id > 0 && id < Long.MAX_VALUE);
                    assertTrue(vertexIds.add(id));
                }
                for (InternalRelation r : relations) {
                    assertTrue(r.hasId());
                    long id = r.longId();
                    assertTrue(id > 0 && id < Long.MAX_VALUE);
                    assertTrue(relationIds.add(id));
                }
            } catch (IDPoolExhaustedException e) {
                //Since the id assignment process is randomized, we divide by 3/2 to account for minor variations
                assertTrue("Max Avail: " + maxIDAssignments + " vs. [" + totalVertices + "," + totalRelations + "]", totalRelations >= maxIDAssignments / 3 * 2 || totalVertices >= maxIDAssignments / 3 * 2);
            } finally {
                graph.tx().rollback();
                graph.close();
            }
        }
    }
}
Also used : TitanGraph(com.thinkaurelius.titan.core.TitanGraph) LongHashSet(com.carrotsearch.hppc.LongHashSet) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) LongSet(com.carrotsearch.hppc.LongSet) ArrayList(java.util.ArrayList) IDPoolExhaustedException(com.thinkaurelius.titan.graphdb.database.idassigner.IDPoolExhaustedException) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) Test(org.junit.Test)

Example 19 with LongSet

use of com.carrotsearch.hppc.LongSet in project janusgraph by JanusGraph.

the class IDAuthorityTest method testManyThreadsOneIDAuthority.

@Test
public void testManyThreadsOneIDAuthority() throws InterruptedException, ExecutionException {
    ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY);
    final IDAuthority targetAuthority = idAuthorities[0];
    targetAuthority.setIDBlockSizer(new InnerIDBlockSizer());
    final int targetPartition = 0;
    final int targetNamespace = 2;
    final ConcurrentLinkedQueue<IDBlock> blocks = new ConcurrentLinkedQueue<>();
    final int blocksPerThread = 40;
    final List<Future<Void>> futures = new ArrayList<>(CONCURRENCY);
    // Start some concurrent threads getting blocks the same ID authority and same partition in that authority
    for (int c = 0; c < CONCURRENCY; c++) {
        futures.add(es.submit(new Callable<Void>() {

            @Override
            public Void call() {
                try {
                    getBlock();
                } catch (BackendException e) {
                    throw new RuntimeException(e);
                }
                return null;
            }

            private void getBlock() throws BackendException {
                for (int i = 0; i < blocksPerThread; i++) {
                    IDBlock block = targetAuthority.getIDBlock(targetPartition, targetNamespace, GET_ID_BLOCK_TIMEOUT);
                    Assert.assertNotNull(block);
                    blocks.add(block);
                }
            }
        }));
    }
    for (Future<Void> f : futures) {
        f.get();
    }
    es.shutdownNow();
    assertEquals(blocksPerThread * CONCURRENCY, blocks.size());
    LongSet ids = new LongHashSet((int) blockSize * blocksPerThread * CONCURRENCY);
    for (IDBlock block : blocks) checkBlock(block, ids);
}
Also used : LongSet(com.carrotsearch.hppc.LongSet) Callable(java.util.concurrent.Callable) LongHashSet(com.carrotsearch.hppc.LongHashSet) ConsistentKeyIDAuthority(org.janusgraph.diskstorage.idmanagement.ConsistentKeyIDAuthority) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 20 with LongSet

use of com.carrotsearch.hppc.LongSet in project janusgraph by JanusGraph.

the class IDAuthorityTest method testMultiIDAcquisition.

@Test
public void testMultiIDAcquisition() throws Throwable {
    final int numPartitions = MAX_NUM_PARTITIONS;
    final int numAcquisitionsPerThreadPartition = 100;
    final IDBlockSizer blockSizer = new InnerIDBlockSizer();
    for (int i = 0; i < CONCURRENCY; i++) idAuthorities[i].setIDBlockSizer(blockSizer);
    final List<ConcurrentLinkedQueue<IDBlock>> ids = new ArrayList<>(numPartitions);
    for (int i = 0; i < numPartitions; i++) {
        ids.add(new ConcurrentLinkedQueue<>());
    }
    final int maxIterations = numAcquisitionsPerThreadPartition * numPartitions * 2;
    final Collection<Future<?>> futures = new ArrayList<>(CONCURRENCY);
    ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY);
    final Set<String> uniqueIds = new HashSet<>(CONCURRENCY);
    for (int i = 0; i < CONCURRENCY; i++) {
        final IDAuthority idAuthority = idAuthorities[i];
        final IDStressor stressRunnable = new IDStressor(numAcquisitionsPerThreadPartition, numPartitions, maxIterations, idAuthority, ids);
        uniqueIds.add(idAuthority.getUniqueID());
        futures.add(es.submit(stressRunnable));
    }
    // If this fails, it's likely to be a bug in the test rather than the
    // IDAuthority (the latter is technically possible, just less likely)
    assertEquals(CONCURRENCY, uniqueIds.size());
    for (Future<?> f : futures) {
        try {
            f.get();
        } catch (ExecutionException e) {
            throw e.getCause();
        }
    }
    for (int i = 0; i < numPartitions; i++) {
        ConcurrentLinkedQueue<IDBlock> list = ids.get(i);
        assertEquals(numAcquisitionsPerThreadPartition * CONCURRENCY, list.size());
        LongSet idSet = new LongHashSet((int) blockSize * list.size());
        for (IDBlock block : list) checkBlock(block, idSet);
    }
    es.shutdownNow();
}
Also used : IDBlockSizer(org.janusgraph.graphdb.database.idassigner.IDBlockSizer) LongSet(com.carrotsearch.hppc.LongSet) LongHashSet(com.carrotsearch.hppc.LongHashSet) ConsistentKeyIDAuthority(org.janusgraph.diskstorage.idmanagement.ConsistentKeyIDAuthority) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ExecutionException(java.util.concurrent.ExecutionException) LongHashSet(com.carrotsearch.hppc.LongHashSet) Test(org.junit.Test)

Aggregations

LongSet (com.carrotsearch.hppc.LongSet)21 LongHashSet (com.carrotsearch.hppc.LongHashSet)13 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)4 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 ExecutorService (java.util.concurrent.ExecutorService)4 Future (java.util.concurrent.Future)4 LongOpenHashSet (com.carrotsearch.hppc.LongOpenHashSet)3 File (java.io.File)3 ExecutionException (java.util.concurrent.ExecutionException)3 LongArrayList (com.carrotsearch.hppc.LongArrayList)2 ConsistentKeyIDAuthority (com.thinkaurelius.titan.diskstorage.idmanagement.ConsistentKeyIDAuthority)2 IDBlockSizer (com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer)2 Callable (java.util.concurrent.Callable)2 CombinedTerm (org.apache.cassandra.index.sasi.utils.CombinedTerm)2 MappedBuffer (org.apache.cassandra.index.sasi.utils.MappedBuffer)2 RandomAccessReader (org.apache.cassandra.io.util.RandomAccessReader)2 SequentialWriter (org.apache.cassandra.io.util.SequentialWriter)2 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 JanusGraph (org.janusgraph.core.JanusGraph)2