Search in sources :

Example 16 with ContextState

use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.

the class CounterContextTest method runRemoveOldShardsNotAllExpiring.

private void runRemoveOldShardsNotAllExpiring(Allocator allocator) {
    NodeId id1 = NodeId.fromInt(1);
    NodeId id3 = NodeId.fromInt(3);
    NodeId id6 = NodeId.fromInt(6);
    List<NodeId.NodeIdRecord> records = new ArrayList<NodeId.NodeIdRecord>();
    records.add(new NodeId.NodeIdRecord(id1, 2L));
    records.add(new NodeId.NodeIdRecord(id3, 4L));
    records.add(new NodeId.NodeIdRecord(id6, 10L));
    ContextState ctx = ContextState.allocate(6, 3, allocator);
    ctx.writeElement(id1, 0L, 1L, true);
    ctx.writeElement(NodeId.fromInt(2), 0L, 2L);
    ctx.writeElement(id3, 0L, 3L, true);
    ctx.writeElement(NodeId.fromInt(4), 0L, 3L);
    ctx.writeElement(NodeId.fromInt(5), 0L, 3L, true);
    ctx.writeElement(id6, 0L, 6L);
    int timeFirstMerge = (int) (System.currentTimeMillis() / 1000);
    // First, only merge the first id
    ByteBuffer merger = cc.computeOldShardMerger(ctx.context, records, 3L);
    ByteBuffer merged = cc.merge(ctx.context, merger, allocator);
    assert cc.total(ctx.context) == cc.total(merged);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        throw new AssertionError();
    }
    // merge the second one
    ByteBuffer merger2 = cc.computeOldShardMerger(merged, records, 7L);
    ByteBuffer merged2 = cc.merge(merged, merger2, allocator);
    assert cc.total(ctx.context) == cc.total(merged2);
    ByteBuffer cleaned = cc.removeOldShards(merged2, timeFirstMerge + 1);
    assert cc.total(ctx.context) == cc.total(cleaned);
    assert cleaned.remaining() == ctx.context.remaining();
    // We should have cleaned id1 but not id3
    ContextState m = new ContextState(cleaned);
    m.moveToNext();
    assert m.getNodeId().equals(id3);
}
Also used : ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 17 with ContextState

use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.

the class CounterColumnTest method testDiff.

@Test
public void testDiff() throws UnknownHostException {
    Allocator allocator = HeapAllocator.instance;
    ContextState left;
    ContextState right;
    CounterColumn leftCol;
    CounterColumn rightCol;
    // timestamp
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), 0, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), 0, 2L);
    assert rightCol == leftCol.diff(rightCol);
    assert null == rightCol.diff(leftCol);
    // timestampOfLastDelete
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), 0, 1L, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), 0, 1L, 2L);
    assert rightCol == leftCol.diff(rightCol);
    assert null == rightCol.diff(leftCol);
    // equality: equal nodes, all counts same
    left = ContextState.allocate(3, 0, allocator);
    left.writeElement(NodeId.fromInt(3), 3L, 0L);
    left.writeElement(NodeId.fromInt(6), 2L, 0L);
    left.writeElement(NodeId.fromInt(9), 1L, 0L);
    right = new ContextState(ByteBufferUtil.clone(left.context), 2);
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), left.context, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), right.context, 1L);
    assert null == leftCol.diff(rightCol);
    // greater than: left has superset of nodes (counts equal)
    left = ContextState.allocate(4, 0, allocator);
    left.writeElement(NodeId.fromInt(3), 3L, 0L);
    left.writeElement(NodeId.fromInt(6), 2L, 0L);
    left.writeElement(NodeId.fromInt(9), 1L, 0L);
    left.writeElement(NodeId.fromInt(12), 0L, 0L);
    right = ContextState.allocate(3, 0, allocator);
    right.writeElement(NodeId.fromInt(3), 3L, 0L);
    right.writeElement(NodeId.fromInt(6), 2L, 0L);
    right.writeElement(NodeId.fromInt(9), 1L, 0L);
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), left.context, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), right.context, 1L);
    assert null == leftCol.diff(rightCol);
    // less than: right has subset of nodes (counts equal)
    assert leftCol == rightCol.diff(leftCol);
    // disjoint: right and left have disjoint node sets
    left = ContextState.allocate(3, 0, allocator);
    left.writeElement(NodeId.fromInt(3), 1L, 0L);
    left.writeElement(NodeId.fromInt(4), 1L, 0L);
    left.writeElement(NodeId.fromInt(9), 1L, 0L);
    right = ContextState.allocate(3, 0, allocator);
    right.writeElement(NodeId.fromInt(3), 1L, 0L);
    right.writeElement(NodeId.fromInt(6), 1L, 0L);
    right.writeElement(NodeId.fromInt(9), 1L, 0L);
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), left.context, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), right.context, 1L);
    assert rightCol == leftCol.diff(rightCol);
    assert leftCol == rightCol.diff(leftCol);
}
Also used : HeapAllocator(org.apache.cassandra.utils.HeapAllocator) Allocator(org.apache.cassandra.utils.Allocator) ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) Test(org.junit.Test)

Example 18 with ContextState

use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.

the class CounterMutationTest method testRemoveOldShardFixCorrupted.

@Test
public void testRemoveOldShardFixCorrupted() throws IOException {
    CounterContext ctx = CounterContext.instance();
    int now = (int) (System.currentTimeMillis() / 1000);
    // Check that corrupted context created prior to #2968 are fixed by removeOldShards
    NodeId id1 = NodeId.getLocalId();
    NodeId.renewLocalId();
    NodeId id2 = NodeId.getLocalId();
    ContextState state = ContextState.allocate(3, 2);
    state.writeElement(NodeId.fromInt(1), 1, 4, false);
    state.writeElement(id1, 3, 2, true);
    // corrupted!
    state.writeElement(id2, -100, 5, true);
    assert ctx.total(state.context) == 11;
    try {
        ByteBuffer merger = ctx.computeOldShardMerger(state.context, Collections.<NodeId.NodeIdRecord>emptyList(), 0);
        ctx.removeOldShards(ctx.merge(state.context, merger, HeapAllocator.instance), now);
        fail("RemoveOldShards should throw an exception if the current id is non-sensical");
    } catch (RuntimeException e) {
    }
    NodeId.renewLocalId();
    ByteBuffer merger = ctx.computeOldShardMerger(state.context, Collections.<NodeId.NodeIdRecord>emptyList(), 0);
    ByteBuffer cleaned = ctx.removeOldShards(ctx.merge(state.context, merger, HeapAllocator.instance), now);
    assert ctx.total(cleaned) == 11;
    // Check it is not corrupted anymore
    ContextState state2 = new ContextState(cleaned);
    while (state2.hasRemaining()) {
        assert state2.getClock() >= 0 || state2.getCount() == 0;
        state2.moveToNext();
    }
    // Check that if we merge old and clean on another node, we keep the right count
    ByteBuffer onRemote = ctx.merge(ctx.clearAllDelta(state.context), ctx.clearAllDelta(cleaned), HeapAllocator.instance);
    assert ctx.total(onRemote) == 11;
}
Also used : ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) ByteBuffer(java.nio.ByteBuffer) CounterContext(org.apache.cassandra.db.context.CounterContext) Test(org.junit.Test)

Example 19 with ContextState

use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.

the class CounterMutationTest method testMergeOldShards.

@Test
public void testMergeOldShards() throws IOException {
    RowMutation rm;
    CounterMutation cm;
    NodeId id1 = NodeId.getLocalId();
    rm = new RowMutation("Keyspace1", ByteBufferUtil.bytes("key1"));
    rm.addCounter(new QueryPath("Counter1", null, ByteBufferUtil.bytes("Column1")), 3);
    cm = new CounterMutation(rm, ConsistencyLevel.ONE);
    cm.apply();
    // faking time of renewal for test
    NodeId.renewLocalId(2L);
    NodeId id2 = NodeId.getLocalId();
    rm = new RowMutation("Keyspace1", ByteBufferUtil.bytes("key1"));
    rm.addCounter(new QueryPath("Counter1", null, ByteBufferUtil.bytes("Column1")), 4);
    cm = new CounterMutation(rm, ConsistencyLevel.ONE);
    cm.apply();
    // faking time of renewal for test
    NodeId.renewLocalId(4L);
    NodeId id3 = NodeId.getLocalId();
    rm = new RowMutation("Keyspace1", ByteBufferUtil.bytes("key1"));
    rm.addCounter(new QueryPath("Counter1", null, ByteBufferUtil.bytes("Column1")), 5);
    rm.addCounter(new QueryPath("Counter1", null, ByteBufferUtil.bytes("Column2")), 1);
    cm = new CounterMutation(rm, ConsistencyLevel.ONE);
    cm.apply();
    DecoratedKey dk = Util.dk("key1");
    ColumnFamily cf = Util.getColumnFamily(Table.open("Keyspace1"), dk, "Counter1");
    // First merges old shards
    CounterColumn.mergeAndRemoveOldShards(dk, cf, Integer.MIN_VALUE, Integer.MAX_VALUE, false);
    long now = System.currentTimeMillis();
    IColumn c = cf.getColumn(ByteBufferUtil.bytes("Column1"));
    assert c != null;
    assert c instanceof CounterColumn;
    assert ((CounterColumn) c).total() == 12L;
    ContextState s = new ContextState(c.value());
    assert s.getNodeId().equals(id1);
    assert s.getCount() == 0L;
    assert -s.getClock() > now - 1000 : " >";
    assert -s.getClock() <= now;
    s.moveToNext();
    assert s.getNodeId().equals(id2);
    assert s.getCount() == 0L;
    assert -s.getClock() > now - 1000;
    assert -s.getClock() <= now;
    s.moveToNext();
    assert s.getNodeId().equals(id3);
    assert s.getCount() == 12L;
    // Then collect old shards
    CounterColumn.mergeAndRemoveOldShards(dk, cf, Integer.MAX_VALUE, Integer.MIN_VALUE, false);
    c = cf.getColumn(ByteBufferUtil.bytes("Column1"));
    assert c != null;
    assert c instanceof CounterColumn;
    assert ((CounterColumn) c).total() == 12L;
    s = new ContextState(c.value());
    assert s.getNodeId().equals(id3);
    assert s.getCount() == 12L;
}
Also used : ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) Test(org.junit.Test)

Aggregations

ContextState (org.apache.cassandra.db.context.CounterContext.ContextState)19 Test (org.junit.Test)12 ByteBuffer (java.nio.ByteBuffer)10 ArrayList (java.util.ArrayList)3 CounterContext (org.apache.cassandra.db.context.CounterContext)1 BufferCell (org.apache.cassandra.db.rows.BufferCell)1 Cell (org.apache.cassandra.db.rows.Cell)1 Allocator (org.apache.cassandra.utils.Allocator)1 HeapAllocator (org.apache.cassandra.utils.HeapAllocator)1