Search in sources :

Example 1 with ContextState

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

the class CounterContextTest method testTotal.

@Test
public void testTotal() {
    ContextState mixed = ContextState.allocate(0, 1, 4);
    mixed.writeRemote(CounterId.fromInt(1), 1L, 1L);
    mixed.writeRemote(CounterId.fromInt(2), 2L, 2L);
    mixed.writeRemote(CounterId.fromInt(4), 4L, 4L);
    mixed.writeRemote(CounterId.fromInt(5), 5L, 5L);
    mixed.writeLocal(CounterId.getLocalId(), 12L, 12L);
    assertEquals(24L, cc.total(mixed.context));
    ContextState global = ContextState.allocate(3, 0, 0);
    global.writeGlobal(CounterId.fromInt(1), 1L, 1L);
    global.writeGlobal(CounterId.fromInt(2), 2L, 2L);
    global.writeGlobal(CounterId.fromInt(3), 3L, 3L);
    assertEquals(6L, cc.total(global.context));
}
Also used : ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) Test(org.junit.Test)

Example 2 with ContextState

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

the class CounterContextTest method testClearLocal.

@Test
public void testClearLocal() {
    ContextState state;
    ByteBuffer marked;
    ByteBuffer cleared;
    // mark/clear for remote-only contexts is a no-op
    state = ContextState.allocate(0, 0, 1);
    state.writeRemote(CounterId.fromInt(1), 1L, 1L);
    assertFalse(cc.shouldClearLocal(state.context));
    marked = cc.markLocalToBeCleared(state.context);
    assertEquals(0, marked.getShort(marked.position()));
    // should return the original context
    assertSame(state.context, marked);
    cleared = cc.clearAllLocal(marked);
    // shouldn't alter anything either
    assertSame(cleared, marked);
    // a single local shard
    state = ContextState.allocate(0, 1, 0);
    state.writeLocal(CounterId.fromInt(1), 1L, 1L);
    assertFalse(cc.shouldClearLocal(state.context));
    marked = cc.markLocalToBeCleared(state.context);
    assertTrue(cc.shouldClearLocal(marked));
    assertEquals(-1, marked.getShort(marked.position()));
    // shouldn't alter in place, as it used to do
    assertNotSame(state.context, marked);
    cleared = cc.clearAllLocal(marked);
    assertFalse(cc.shouldClearLocal(cleared));
    assertEquals(0, cleared.getShort(cleared.position()));
    // 2 global + 1 local shard
    state = ContextState.allocate(2, 1, 0);
    state.writeLocal(CounterId.fromInt(1), 1L, 1L);
    state.writeGlobal(CounterId.fromInt(2), 2L, 2L);
    state.writeGlobal(CounterId.fromInt(3), 3L, 3L);
    assertFalse(cc.shouldClearLocal(state.context));
    marked = cc.markLocalToBeCleared(state.context);
    assertTrue(cc.shouldClearLocal(marked));
    assertEquals(-3, marked.getShort(marked.position()));
    assertEquals(0, marked.getShort(marked.position() + headerSizeLength));
    assertEquals(Short.MIN_VALUE + 1, marked.getShort(marked.position() + headerSizeLength + headerEltLength));
    assertEquals(Short.MIN_VALUE + 2, marked.getShort(marked.position() + headerSizeLength + 2 * headerEltLength));
    int headerLength = headerSizeLength + 3 * headerEltLength;
    assertTrue(Util.equalsCounterId(CounterId.fromInt(1), marked, headerLength));
    assertEquals(1L, marked.getLong(marked.position() + headerLength + idLength));
    assertEquals(1L, marked.getLong(marked.position() + headerLength + idLength + clockLength));
    assertTrue(Util.equalsCounterId(CounterId.fromInt(2), marked, headerLength + stepLength));
    assertEquals(2L, marked.getLong(marked.position() + headerLength + stepLength + idLength));
    assertEquals(2L, marked.getLong(marked.position() + headerLength + stepLength + idLength + clockLength));
    assertTrue(Util.equalsCounterId(CounterId.fromInt(3), marked, headerLength + 2 * stepLength));
    assertEquals(3L, marked.getLong(marked.position() + headerLength + 2 * stepLength + idLength));
    assertEquals(3L, marked.getLong(marked.position() + headerLength + 2 * stepLength + idLength + clockLength));
    cleared = cc.clearAllLocal(marked);
    assertFalse(cc.shouldClearLocal(cleared));
    // 2 global shards
    assertEquals(2, cleared.getShort(cleared.position()));
    assertEquals(Short.MIN_VALUE + 1, cleared.getShort(marked.position() + headerEltLength));
    assertEquals(Short.MIN_VALUE + 2, cleared.getShort(marked.position() + headerSizeLength + headerEltLength));
    headerLength = headerSizeLength + 2 * headerEltLength;
    assertTrue(Util.equalsCounterId(CounterId.fromInt(1), cleared, headerLength));
    assertEquals(1L, cleared.getLong(cleared.position() + headerLength + idLength));
    assertEquals(1L, cleared.getLong(cleared.position() + headerLength + idLength + clockLength));
    assertTrue(Util.equalsCounterId(CounterId.fromInt(2), cleared, headerLength + stepLength));
    assertEquals(2L, cleared.getLong(cleared.position() + headerLength + stepLength + idLength));
    assertEquals(2L, cleared.getLong(cleared.position() + headerLength + stepLength + idLength + clockLength));
    assertTrue(Util.equalsCounterId(CounterId.fromInt(3), cleared, headerLength + 2 * stepLength));
    assertEquals(3L, cleared.getLong(cleared.position() + headerLength + 2 * stepLength + idLength));
    assertEquals(3L, cleared.getLong(cleared.position() + headerLength + 2 * stepLength + idLength + clockLength));
    // a single global shard - no-op
    state = ContextState.allocate(1, 0, 0);
    state.writeGlobal(CounterId.fromInt(1), 1L, 1L);
    assertFalse(cc.shouldClearLocal(state.context));
    marked = cc.markLocalToBeCleared(state.context);
    assertEquals(1, marked.getShort(marked.position()));
    assertSame(state.context, marked);
    cleared = cc.clearAllLocal(marked);
    assertSame(cleared, marked);
}
Also used : ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with ContextState

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

the class CounterContextTest method testGetGlockAndCountOf.

@Test
public void testGetGlockAndCountOf() {
    ContextState state = ContextState.allocate(3, 3, 3);
    state.writeGlobal(CounterId.fromInt(1), 1L, 1L);
    state.writeRemote(CounterId.fromInt(2), 2L, 2L);
    state.writeLocal(CounterId.fromInt(3), 3L, 3L);
    state.writeGlobal(CounterId.fromInt(4), 4L, 4L);
    state.writeRemote(CounterId.fromInt(5), 5L, 5L);
    state.writeLocal(CounterId.fromInt(6), 6L, 6L);
    state.writeGlobal(CounterId.fromInt(7), 7L, 7L);
    state.writeRemote(CounterId.fromInt(8), 8L, 8L);
    state.writeLocal(CounterId.fromInt(9), 9L, 9L);
    assertEquals(ClockAndCount.create(1L, 1L), cc.getClockAndCountOf(state.context, CounterId.fromInt(1)));
    assertEquals(ClockAndCount.create(2L, 2L), cc.getClockAndCountOf(state.context, CounterId.fromInt(2)));
    assertEquals(ClockAndCount.create(3L, 3L), cc.getClockAndCountOf(state.context, CounterId.fromInt(3)));
    assertEquals(ClockAndCount.create(4L, 4L), cc.getClockAndCountOf(state.context, CounterId.fromInt(4)));
    assertEquals(ClockAndCount.create(5L, 5L), cc.getClockAndCountOf(state.context, CounterId.fromInt(5)));
    assertEquals(ClockAndCount.create(6L, 6L), cc.getClockAndCountOf(state.context, CounterId.fromInt(6)));
    assertEquals(ClockAndCount.create(7L, 7L), cc.getClockAndCountOf(state.context, CounterId.fromInt(7)));
    assertEquals(ClockAndCount.create(8L, 8L), cc.getClockAndCountOf(state.context, CounterId.fromInt(8)));
    assertEquals(ClockAndCount.create(9L, 9L), cc.getClockAndCountOf(state.context, CounterId.fromInt(9)));
    assertEquals(ClockAndCount.create(0L, 0L), cc.getClockAndCountOf(state.context, CounterId.fromInt(0)));
    assertEquals(ClockAndCount.create(0L, 0L), cc.getClockAndCountOf(state.context, CounterId.fromInt(10)));
    assertEquals(ClockAndCount.create(0L, 0L), cc.getClockAndCountOf(state.context, CounterId.fromInt(15)));
    assertEquals(ClockAndCount.create(0L, 0L), cc.getClockAndCountOf(state.context, CounterId.fromInt(20)));
}
Also used : ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) Test(org.junit.Test)

Example 4 with ContextState

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

the class CounterContextTest method runTotal.

private void runTotal(Allocator allocator) {
    ContextState left = ContextState.allocate(4, 1, allocator);
    left.writeElement(NodeId.fromInt(1), 1L, 1L);
    left.writeElement(NodeId.fromInt(2), 2L, 2L);
    left.writeElement(NodeId.fromInt(4), 3L, 3L);
    left.writeElement(NodeId.getLocalId(), 3L, 3L, true);
    ContextState right = ContextState.allocate(3, 1, allocator);
    right.writeElement(NodeId.fromInt(4), 4L, 4L);
    right.writeElement(NodeId.fromInt(5), 5L, 5L);
    right.writeElement(NodeId.getLocalId(), 9L, 9L, true);
    ByteBuffer merged = cc.merge(left.context, right.context, allocator);
    // 127.0.0.1: 12 (3+9)
    // 0.0.0.1:    1
    // 0.0.0.2:    2
    // 0.0.0.4:    4
    // 0.0.0.5:    5
    assertEquals(24L, cc.total(merged));
}
Also used : ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) ByteBuffer(java.nio.ByteBuffer)

Example 5 with ContextState

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

the class CounterContextTest method runRemoveOldShards.

private void runRemoveOldShards(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, 1L, 1L, true);
    ctx.writeElement(NodeId.fromInt(2), 2L, 2L);
    ctx.writeElement(id3, 3L, 3L, true);
    ctx.writeElement(NodeId.fromInt(4), 6L, 3L);
    ctx.writeElement(NodeId.fromInt(5), 7L, 3L, true);
    ctx.writeElement(id6, 5L, 6L);
    ByteBuffer merger = cc.computeOldShardMerger(ctx.context, records, Integer.MAX_VALUE);
    ByteBuffer merged = cc.merge(ctx.context, merger, allocator);
    assert cc.total(ctx.context) == cc.total(merged);
    ByteBuffer cleaned = cc.removeOldShards(merged, (int) (System.currentTimeMillis() / 1000) + 1);
    assert cc.total(ctx.context) == cc.total(cleaned);
    assert cleaned.remaining() == ctx.context.remaining() - stepLength - 2;
}
Also used : ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

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