Search in sources :

Example 1 with IDBlockSizer

use of com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer in project titan by thinkaurelius.

the class IDAuthorityTest method testSimpleIDAcquisition.

@Test
public void testSimpleIDAcquisition() throws BackendException {
    final IDBlockSizer blockSizer = new InnerIDBlockSizer();
    idAuthorities[0].setIDBlockSizer(blockSizer);
    int numTrials = 100;
    LongSet ids = new LongHashSet((int) blockSize * numTrials);
    long previous = 0;
    for (int i = 0; i < numTrials; i++) {
        IDBlock block = idAuthorities[0].getIDBlock(0, 0, GET_ID_BLOCK_TIMEOUT);
        checkBlock(block, ids);
        if (hasEmptyUid) {
            if (previous != 0)
                assertEquals(previous + 1, block.getId(0));
            previous = block.getId(block.numIds() - 1);
        }
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) IDBlockSizer(com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer) LongSet(com.carrotsearch.hppc.LongSet) Test(org.junit.Test)

Example 2 with IDBlockSizer

use of com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer in project titan by thinkaurelius.

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<ConcurrentLinkedQueue<IDBlock>>(numPartitions);
    for (int i = 0; i < numPartitions; i++) {
        ids.add(new ConcurrentLinkedQueue<IDBlock>());
    }
    final int maxIterations = numAcquisitionsPerThreadPartition * numPartitions * 2;
    final Collection<Future<?>> futures = new ArrayList<Future<?>>(CONCURRENCY);
    ExecutorService es = Executors.newFixedThreadPool(CONCURRENCY);
    Set<String> uids = new HashSet<String>(CONCURRENCY);
    for (int i = 0; i < CONCURRENCY; i++) {
        final IDAuthority idAuthority = idAuthorities[i];
        final IDStressor stressRunnable = new IDStressor(numAcquisitionsPerThreadPartition, numPartitions, maxIterations, idAuthority, ids);
        uids.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, uids.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(com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer) LongSet(com.carrotsearch.hppc.LongSet) LongHashSet(com.carrotsearch.hppc.LongHashSet) ConsistentKeyIDAuthority(com.thinkaurelius.titan.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)

Example 3 with IDBlockSizer

use of com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer in project titan by thinkaurelius.

the class IDPoolTest method testAllocationTimeoutAndRecovery.

@Test
public void testAllocationTimeoutAndRecovery() throws BackendException {
    IMocksControl ctrl = EasyMock.createStrictControl();
    final int partition = 42;
    final int idNamespace = 777;
    final Duration timeout = Duration.ofSeconds(1L);
    final IDAuthority mockAuthority = ctrl.createMock(IDAuthority.class);
    // Sleep for two seconds, then throw a backendexception
    // this whole delegate could be deleted if we abstracted StandardIDPool's internal executor and stopwatches
    expect(mockAuthority.getIDBlock(partition, idNamespace, timeout)).andDelegateTo(new IDAuthority() {

        @Override
        public IDBlock getIDBlock(int partition, int idNamespace, Duration timeout) throws BackendException {
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                fail();
            }
            throw new TemporaryBackendException("slow backend");
        }

        @Override
        public List<KeyRange> getLocalIDPartition() throws BackendException {
            throw new IllegalArgumentException();
        }

        @Override
        public void setIDBlockSizer(IDBlockSizer sizer) {
            throw new IllegalArgumentException();
        }

        @Override
        public void close() throws BackendException {
            throw new IllegalArgumentException();
        }

        @Override
        public String getUniqueID() {
            throw new IllegalArgumentException();
        }

        @Override
        public boolean supportsInterruption() {
            return true;
        }
    });
    expect(mockAuthority.getIDBlock(partition, idNamespace, timeout)).andReturn(new IDBlock() {

        @Override
        public long numIds() {
            return 2;
        }

        @Override
        public long getId(long index) {
            return 200;
        }
    });
    expect(mockAuthority.supportsInterruption()).andStubReturn(true);
    ctrl.replay();
    StandardIDPool pool = new StandardIDPool(mockAuthority, partition, idNamespace, Integer.MAX_VALUE, timeout, 0.1);
    try {
        pool.nextID();
        fail();
    } catch (TitanException e) {
    }
    long nextID = pool.nextID();
    assertEquals(200, nextID);
    ctrl.verify();
}
Also used : IDBlockSizer(com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer) Duration(java.time.Duration) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) IMocksControl(org.easymock.IMocksControl) StandardIDPool(com.thinkaurelius.titan.graphdb.database.idassigner.StandardIDPool) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) IDAuthority(com.thinkaurelius.titan.diskstorage.IDAuthority) IDBlock(com.thinkaurelius.titan.diskstorage.IDBlock) TitanException(com.thinkaurelius.titan.core.TitanException) List(java.util.List) Test(org.junit.Test)

Example 4 with IDBlockSizer

use of com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer in project titan by thinkaurelius.

the class IDAuthorityTest method testIDExhaustion.

@Test
public void testIDExhaustion() throws BackendException {
    final int chunks = 30;
    final IDBlockSizer blockSizer = new IDBlockSizer() {

        @Override
        public long getBlockSize(int idNamespace) {
            return ((1l << (idUpperBoundBitWidth - uidBitWidth)) - 1) / chunks;
        }

        @Override
        public long getIdUpperBound(int idNamespace) {
            return idUpperBound;
        }
    };
    idAuthorities[0].setIDBlockSizer(blockSizer);
    if (hasFixedUid) {
        for (int i = 0; i < chunks; i++) {
            idAuthorities[0].getIDBlock(0, 0, GET_ID_BLOCK_TIMEOUT);
        }
        try {
            idAuthorities[0].getIDBlock(0, 0, GET_ID_BLOCK_TIMEOUT);
            Assert.fail();
        } catch (IDPoolExhaustedException e) {
        }
    } else {
        for (int i = 0; i < (chunks * Math.max(1, (1 << uidBitWidth) / 10)); i++) {
            idAuthorities[0].getIDBlock(0, 0, GET_ID_BLOCK_TIMEOUT);
        }
        try {
            for (int i = 0; i < (chunks * Math.max(1, (1 << uidBitWidth) * 9 / 10)); i++) {
                idAuthorities[0].getIDBlock(0, 0, GET_ID_BLOCK_TIMEOUT);
            }
            Assert.fail();
        } catch (IDPoolExhaustedException e) {
        }
    }
}
Also used : IDBlockSizer(com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer) IDPoolExhaustedException(com.thinkaurelius.titan.graphdb.database.idassigner.IDPoolExhaustedException) Test(org.junit.Test)

Aggregations

IDBlockSizer (com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer)4 Test (org.junit.Test)4 LongHashSet (com.carrotsearch.hppc.LongHashSet)2 LongSet (com.carrotsearch.hppc.LongSet)2 TitanException (com.thinkaurelius.titan.core.TitanException)1 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)1 IDAuthority (com.thinkaurelius.titan.diskstorage.IDAuthority)1 IDBlock (com.thinkaurelius.titan.diskstorage.IDBlock)1 TemporaryBackendException (com.thinkaurelius.titan.diskstorage.TemporaryBackendException)1 ConsistentKeyIDAuthority (com.thinkaurelius.titan.diskstorage.idmanagement.ConsistentKeyIDAuthority)1 IDPoolExhaustedException (com.thinkaurelius.titan.graphdb.database.idassigner.IDPoolExhaustedException)1 StandardIDPool (com.thinkaurelius.titan.graphdb.database.idassigner.StandardIDPool)1 Duration (java.time.Duration)1 List (java.util.List)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 IMocksControl (org.easymock.IMocksControl)1