Search in sources :

Example 26 with LongHashSet

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

the class IDAuthorityTest method testMultiIDAcquisition.

@ParameterizedTest
@MethodSource("configs")
public void testMultiIDAcquisition(WriteConfiguration baseConfig) throws Throwable {
    setUp(baseConfig);
    boolean localStore = Arrays.stream(manager).noneMatch(m -> m.getFeatures().isDistributed());
    // On local mode ids acquired sequentially
    if (localStore) {
        return;
    }
    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) ArrayList(java.util.ArrayList) 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) HashSet(java.util.HashSet) LongHashSet(com.carrotsearch.hppc.LongHashSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 27 with LongHashSet

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

the class VertexIDAssignerTest method testCustomIdAssignment.

private void testCustomIdAssignment(VertexIDAssigner idAssigner, CustomIdStrategy idStrategy, int numPartitionsBits) {
    LongSet vertexIds = new LongHashSet();
    final long maxCount = idAssigner.getIDManager().getVertexCountBound();
    long count = 1;
    for (int trial = 0; trial < 10; trial++) {
        final JanusGraph graph = getInMemoryGraph(true, true, numPartitionsBits);
        int numVertices = 1000;
        final List<JanusGraphVertex> vertices = new ArrayList<>(numVertices);
        try {
            for (int i = 0; i < numVertices; i++, count++) {
                final long userVertexId;
                switch(idStrategy) {
                    case LOW:
                        userVertexId = count;
                        break;
                    case HIGH:
                        userVertexId = maxCount - count;
                        break;
                    default:
                        throw new RuntimeException("Unsupported custom id strategy: " + idStrategy);
                }
                final long id = idAssigner.getIDManager().toVertexId(userVertexId);
                JanusGraphVertex next = graph.addVertex(T.id, id, "user_id", userVertexId);
                vertices.add(next);
            }
            // Verify that ids are set, unique and consistent with user id basis
            for (JanusGraphVertex v : vertices) {
                assertTrue(v.hasId());
                long id = v.longId();
                assertTrue(id > 0 && id < Long.MAX_VALUE);
                assertTrue(vertexIds.add(id));
                assertEquals((long) v.value("user_id"), idAssigner.getIDManager().fromVertexId(id));
            }
        } finally {
            graph.tx().rollback();
            graph.close();
        }
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) LongSet(com.carrotsearch.hppc.LongSet) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ArrayList(java.util.ArrayList)

Example 28 with LongHashSet

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

the class VertexIDAssignerTest method testIDAssignment.

@ParameterizedTest
@MethodSource("configs")
public void testIDAssignment(VertexIDAssigner idAssigner, long maxIDAssignments, int numPartitionsBits) {
    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 }) {
            final JanusGraph graph = getInMemoryGraph(false, false, numPartitionsBits);
            int numVertices = 1000;
            final List<JanusGraphVertex> vertices = new ArrayList<>(numVertices);
            final List<InternalRelation> relations = new ArrayList<>();
            JanusGraphVertex old = null;
            totalRelations += 2 * numVertices;
            totalVertices += numVertices;
            try {
                for (int i = 0; i < numVertices; i++) {
                    JanusGraphVertex 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 (JanusGraphVertex 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(totalRelations >= maxIDAssignments / 3 * 2 || totalVertices >= maxIDAssignments / 3 * 2, "Max Avail: " + maxIDAssignments + " vs. [" + totalVertices + "," + totalRelations + "]");
            } finally {
                graph.tx().rollback();
                graph.close();
            }
        }
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) LongSet(com.carrotsearch.hppc.LongSet) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ArrayList(java.util.ArrayList) IDPoolExhaustedException(org.janusgraph.graphdb.database.idassigner.IDPoolExhaustedException) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 29 with LongHashSet

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

the class DynamicTokenTreeBuilder method add.

public void add(Long token, long keyPosition) {
    LongSet found = tokens.get(token);
    if (found == null)
        tokens.put(token, (found = new LongHashSet(2)));
    found.add(keyPosition);
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) LongSet(com.carrotsearch.hppc.LongSet)

Example 30 with LongHashSet

use of com.carrotsearch.hppc.LongHashSet 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 LongHashSet();
                offsetSet.add(i);
                put(i, offsetSet);
            }
        }
    };
    final TokenTreeBuilder builder = isStatic ? new StaticTokenTreeBuilder(new FakeCombinedTerm(toks)) : new DynamicTokenTreeBuilder(toks);
    builder.finish();
    final File treeFile = FileUtils.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) LongHashSet(com.carrotsearch.hppc.LongHashSet) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) File(org.apache.cassandra.io.util.File) MappedBuffer(org.apache.cassandra.index.sasi.utils.MappedBuffer)

Aggregations

LongHashSet (com.carrotsearch.hppc.LongHashSet)30 LongSet (com.carrotsearch.hppc.LongSet)20 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)7 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)6 ExecutorService (java.util.concurrent.ExecutorService)6 Future (java.util.concurrent.Future)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)5 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 HashSet (java.util.HashSet)4 ExecutionException (java.util.concurrent.ExecutionException)4 ConsistentKeyIDAuthority (org.janusgraph.diskstorage.idmanagement.ConsistentKeyIDAuthority)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 Callable (java.util.concurrent.Callable)3 BytesRef (org.apache.lucene.util.BytesRef)3 JanusGraph (org.janusgraph.core.JanusGraph)3 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)3