Search in sources :

Example 46 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class ConsistentKeyLockerTest method testWriteLockThrowsExceptionAfterMaxStoreTimeouts.

/**
 * Test locker when all three attempts to write a lock succeed but take
 * longer than the wait limit. We expect the locker to delete all three
 * columns that it wrote and locally unlock the KeyColumn, then emit an
 * exception.
 *
 * @throws StorageException shouldn't happen
 */
@Test
public void testWriteLockThrowsExceptionAfterMaxStoreTimeouts() throws StorageException {
    expect(lockState.has(defaultTx, defaultLockID)).andReturn(false);
    recordSuccessfulLocalLock();
    StaticBuffer firstCol = recordSuccessfulLockWrite(5, TimeUnit.SECONDS, null).col;
    StaticBuffer secondCol = recordSuccessfulLockWrite(5, TimeUnit.SECONDS, firstCol).col;
    StaticBuffer thirdCol = recordSuccessfulLockWrite(5, TimeUnit.SECONDS, secondCol).col;
    recordSuccessfulLockDelete(1, TimeUnit.NANOSECONDS, thirdCol);
    recordSuccessfulLocalUnlock();
    ctrl.replay();
    StorageException expected = null;
    try {
        // SUT
        locker.writeLock(defaultLockID, defaultTx);
    } catch (TemporaryStorageException e) {
        expected = e;
    }
    assertNotNull(expected);
}
Also used : TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) Test(org.junit.Test)

Example 47 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class IDManagementTest method edgeTypeIDTest.

@Test
public void edgeTypeIDTest() {
    int partitionBits = 21;
    IDManager eid = new IDManager(partitionBits);
    int trails = 1000000;
    assertEquals(eid.getMaxPartitionCount(), (1 << partitionBits) - 1);
    KryoSerializer serializer = new KryoSerializer();
    for (int t = 0; t < trails; t++) {
        long count = RandomGenerator.randomLong(1, eid.getMaxTitanTypeCount());
        long id;
        int dirID;
        RelationType type;
        if (Math.random() < 0.5) {
            id = eid.getEdgeLabelID(count);
            assertTrue(eid.isEdgeLabelID(id));
            type = RelationType.EDGE;
            if (Math.random() < 0.5)
                dirID = IDHandler.EDGE_IN_DIR;
            else
                dirID = IDHandler.EDGE_OUT_DIR;
        } else {
            type = RelationType.PROPERTY;
            id = eid.getPropertyKeyID(count);
            assertTrue(eid.isPropertyKeyID(id));
            dirID = IDHandler.PROPERTY_DIR;
        }
        assertTrue(eid.isTypeID(id));
        StaticBuffer b = IDHandler.getEdgeType(id, dirID);
        // System.out.println(dirID);
        // System.out.println(getBinary(id));
        // System.out.println(getBuffer(b.asReadBuffer()));
        ReadBuffer rb = b.asReadBuffer();
        long[] vals = IDHandler.readEdgeType(rb);
        assertEquals(id, vals[0]);
        assertEquals(dirID, vals[1]);
        assertFalse(rb.hasRemaining());
        // Inline edge type
        WriteBuffer wb = new WriteByteBuffer(9);
        IDHandler.writeInlineEdgeType(wb, id);
        long newId = IDHandler.readInlineEdgeType(wb.getStaticBuffer().asReadBuffer());
        assertEquals(id, newId);
        // Compare to Kryo
        DataOutput out = serializer.getDataOutput(10, true);
        IDHandler.writeEdgeType(out, id, dirID);
        assertEquals(b, out.getStaticBuffer());
        // Make sure the bounds are right
        StaticBuffer[] bounds = IDHandler.getBounds(type);
        assertTrue(bounds[0].compareTo(b) < 0);
        assertTrue(bounds[1].compareTo(b) > 0);
        bounds = IDHandler.getBounds(RelationType.RELATION);
        assertTrue(bounds[0].compareTo(b) < 0);
        assertTrue(bounds[1].compareTo(b) > 0);
    }
}
Also used : ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) WriteBuffer(com.thinkaurelius.titan.diskstorage.WriteBuffer) WriteByteBuffer(com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer) RelationType(com.thinkaurelius.titan.graphdb.internal.RelationType) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) KryoSerializer(com.thinkaurelius.titan.graphdb.database.serialize.kryo.KryoSerializer) Test(org.junit.Test)

Example 48 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class MockIDAuthority method getLocalIDPartition.

@Override
public StaticBuffer[] getLocalIDPartition() throws StorageException {
    ByteBuffer lower = ByteBuffer.allocate(4);
    ByteBuffer upper = ByteBuffer.allocate(4);
    lower.putInt(localPartition[0]);
    upper.putInt(localPartition[1]);
    lower.rewind();
    upper.rewind();
    Preconditions.checkArgument(ByteBufferUtil.isSmallerThan(lower, upper), Arrays.toString(localPartition));
    return new StaticBuffer[] { new StaticByteBuffer(lower), new StaticByteBuffer(upper) };
}
Also used : StaticByteBuffer(com.thinkaurelius.titan.diskstorage.util.StaticByteBuffer) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) StaticByteBuffer(com.thinkaurelius.titan.diskstorage.util.StaticByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 49 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class CassandraHelper method transformRange.

public static KeyRange transformRange(Token leftKeyExclusive, Token rightKeyInclusive) {
    if (!(leftKeyExclusive instanceof BytesToken))
        throw new UnsupportedOperationException();
    // if left part is BytesToken, right part should be too, otherwise there is no sense in the ring
    assert rightKeyInclusive instanceof BytesToken;
    // l is exclusive, r is inclusive
    BytesToken l = (BytesToken) leftKeyExclusive;
    BytesToken r = (BytesToken) rightKeyInclusive;
    byte[] leftTokenValue = l.getTokenValue();
    byte[] rightTokenValue = r.getTokenValue();
    Preconditions.checkArgument(leftTokenValue.length == rightTokenValue.length, "Tokens have unequal length");
    int tokenLength = leftTokenValue.length;
    byte[][] tokens = new byte[][] { leftTokenValue, rightTokenValue };
    byte[][] plusOne = new byte[2][tokenLength];
    for (int j = 0; j < 2; j++) {
        boolean carry = true;
        for (int i = tokenLength - 1; i >= 0; i--) {
            byte b = tokens[j][i];
            if (carry) {
                b++;
                carry = false;
            }
            if (b == 0)
                carry = true;
            plusOne[j][i] = b;
        }
    }
    StaticBuffer lb = StaticArrayBuffer.of(plusOne[0]);
    StaticBuffer rb = StaticArrayBuffer.of(plusOne[1]);
    Preconditions.checkArgument(lb.length() == tokenLength, lb.length());
    Preconditions.checkArgument(rb.length() == tokenLength, rb.length());
    return new KeyRange(lb, rb);
}
Also used : KeyRange(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange) BytesToken(org.apache.cassandra.dht.BytesToken) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Example 50 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class StandardLockCleanerRunnable method runWithExceptions.

private void runWithExceptions() throws BackendException {
    StaticBuffer lockKey = serializer.toLockKey(target.getKey(), target.getColumn());
    // TODO reduce LOCK_COL_END based on cutoff
    List<Entry> locks = store.getSlice(new KeySliceQuery(lockKey, LOCK_COL_START, LOCK_COL_END), tx);
    ImmutableList.Builder<StaticBuffer> b = ImmutableList.builder();
    for (Entry lc : locks) {
        TimestampRid tr = serializer.fromLockColumn(lc.getColumn(), times);
        if (tr.getTimestamp().isBefore(cutoff)) {
            log.info("Deleting expired lock on {} by rid {} with timestamp {} (before or at cutoff {})", new Object[] { target, tr.getRid(), tr.getTimestamp(), cutoff });
            b.add(lc.getColumn());
        } else {
            log.debug("Ignoring lock on {} by rid {} with timestamp {} (timestamp is after cutoff {})", new Object[] { target, tr.getRid(), tr.getTimestamp(), cutoff });
        }
    }
    List<StaticBuffer> dels = b.build();
    if (!dels.isEmpty()) {
        store.mutate(lockKey, ImmutableList.<Entry>of(), dels, tx);
        log.info("Deleted {} expired locks (before or at cutoff {})", dels.size(), cutoff);
    }
}
Also used : Entry(com.thinkaurelius.titan.diskstorage.Entry) ImmutableList(com.google.common.collect.ImmutableList) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) KeySliceQuery(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)

Aggregations

StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)92 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)16 StaticBufferEntry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.StaticBufferEntry)15 Entry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry)14 TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)13 ConsistentKeyLockStatus (com.thinkaurelius.titan.diskstorage.locking.consistentkey.ConsistentKeyLockStatus)13 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)12 HashMap (java.util.HashMap)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 Map (java.util.Map)11 Entry (com.thinkaurelius.titan.diskstorage.Entry)10 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)10 StorageException (com.thinkaurelius.titan.diskstorage.StorageException)9 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)8 KeySliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)7 KCVMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation)6 KeyRange (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange)6 SliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)6 Instant (java.time.Instant)6