use of org.apache.cassandra.db.context.CounterContext.ContextState in project cassandra by apache.
the class CounterContextTest method testAllocate.
@Test
public void testAllocate() {
ContextState allGlobal = ContextState.allocate(3, 0, 0);
assertEquals(headerSizeLength + 3 * headerEltLength + 3 * stepLength, allGlobal.context.remaining());
ContextState allLocal = ContextState.allocate(0, 3, 0);
assertEquals(headerSizeLength + 3 * headerEltLength + 3 * stepLength, allLocal.context.remaining());
ContextState allRemote = ContextState.allocate(0, 0, 3);
assertEquals(headerSizeLength + 3 * stepLength, allRemote.context.remaining());
ContextState mixed = ContextState.allocate(1, 1, 1);
assertEquals(headerSizeLength + 2 * headerEltLength + 3 * stepLength, mixed.context.remaining());
}
use of org.apache.cassandra.db.context.CounterContext.ContextState in project cassandra by apache.
the class CounterContextTest method testMerge.
@Test
public void testMerge() {
// note: local counts aggregated; remote counts are reconciled (i.e. take max)
ContextState left = ContextState.allocate(0, 1, 3);
left.writeRemote(CounterId.fromInt(1), 1L, 1L);
left.writeRemote(CounterId.fromInt(2), 2L, 2L);
left.writeRemote(CounterId.fromInt(4), 6L, 3L);
left.writeLocal(CounterId.getLocalId(), 7L, 3L);
ContextState right = ContextState.allocate(0, 1, 2);
right.writeRemote(CounterId.fromInt(4), 4L, 4L);
right.writeRemote(CounterId.fromInt(5), 5L, 5L);
right.writeLocal(CounterId.getLocalId(), 2L, 9L);
ByteBuffer merged = cc.merge(left.context, right.context);
int hd = 4;
assertEquals(hd + 5 * stepLength, merged.remaining());
// local node id's counts are aggregated
assertTrue(Util.equalsCounterId(CounterId.getLocalId(), merged, hd + 4 * stepLength));
assertEquals(9L, merged.getLong(merged.position() + hd + 4 * stepLength + idLength));
assertEquals(12L, merged.getLong(merged.position() + hd + 4 * stepLength + idLength + clockLength));
// remote node id counts are reconciled (i.e. take max)
assertTrue(Util.equalsCounterId(CounterId.fromInt(4), merged, hd + 2 * stepLength));
assertEquals(6L, merged.getLong(merged.position() + hd + 2 * stepLength + idLength));
assertEquals(3L, merged.getLong(merged.position() + hd + 2 * stepLength + idLength + clockLength));
assertTrue(Util.equalsCounterId(CounterId.fromInt(5), merged, hd + 3 * stepLength));
assertEquals(5L, merged.getLong(merged.position() + hd + 3 * stepLength + idLength));
assertEquals(5L, merged.getLong(merged.position() + hd + 3 * stepLength + idLength + clockLength));
assertTrue(Util.equalsCounterId(CounterId.fromInt(2), merged, hd + stepLength));
assertEquals(2L, merged.getLong(merged.position() + hd + stepLength + idLength));
assertEquals(2L, merged.getLong(merged.position() + hd + stepLength + idLength + clockLength));
assertTrue(Util.equalsCounterId(CounterId.fromInt(1), merged, hd));
assertEquals(1L, merged.getLong(merged.position() + hd + idLength));
assertEquals(1L, merged.getLong(merged.position() + hd + idLength + clockLength));
//
// Test merging two exclusively global contexts
//
left = ContextState.allocate(3, 0, 0);
left.writeGlobal(CounterId.fromInt(1), 1L, 1L);
left.writeGlobal(CounterId.fromInt(2), 2L, 2L);
left.writeGlobal(CounterId.fromInt(3), 3L, 3L);
right = ContextState.allocate(3, 0, 0);
right.writeGlobal(CounterId.fromInt(3), 6L, 6L);
right.writeGlobal(CounterId.fromInt(4), 4L, 4L);
right.writeGlobal(CounterId.fromInt(5), 5L, 5L);
merged = cc.merge(left.context, right.context);
assertEquals(headerSizeLength + 5 * headerEltLength + 5 * stepLength, merged.remaining());
assertEquals(18L, cc.total(merged));
assertEquals(5, merged.getShort(merged.position()));
int headerLength = headerSizeLength + 5 * headerEltLength;
assertTrue(Util.equalsCounterId(CounterId.fromInt(1), merged, headerLength));
assertEquals(1L, merged.getLong(merged.position() + headerLength + idLength));
assertEquals(1L, merged.getLong(merged.position() + headerLength + idLength + clockLength));
assertTrue(Util.equalsCounterId(CounterId.fromInt(2), merged, headerLength + stepLength));
assertEquals(2L, merged.getLong(merged.position() + headerLength + stepLength + idLength));
assertEquals(2L, merged.getLong(merged.position() + headerLength + stepLength + idLength + clockLength));
// pick the global shard with the largest clock
assertTrue(Util.equalsCounterId(CounterId.fromInt(3), merged, headerLength + 2 * stepLength));
assertEquals(6L, merged.getLong(merged.position() + headerLength + 2 * stepLength + idLength));
assertEquals(6L, merged.getLong(merged.position() + headerLength + 2 * stepLength + idLength + clockLength));
assertTrue(Util.equalsCounterId(CounterId.fromInt(4), merged, headerLength + 3 * stepLength));
assertEquals(4L, merged.getLong(merged.position() + headerLength + 3 * stepLength + idLength));
assertEquals(4L, merged.getLong(merged.position() + headerLength + 3 * stepLength + idLength + clockLength));
assertTrue(Util.equalsCounterId(CounterId.fromInt(5), merged, headerLength + 4 * stepLength));
assertEquals(5L, merged.getLong(merged.position() + headerLength + 4 * stepLength + idLength));
assertEquals(5L, merged.getLong(merged.position() + headerLength + 4 * stepLength + idLength + clockLength));
//
// Test merging two global contexts w/ 'invalid shards'
//
left = ContextState.allocate(1, 0, 0);
left.writeGlobal(CounterId.fromInt(1), 10L, 20L);
right = ContextState.allocate(1, 0, 0);
right.writeGlobal(CounterId.fromInt(1), 10L, 30L);
merged = cc.merge(left.context, right.context);
headerLength = headerSizeLength + headerEltLength;
assertEquals(headerLength + stepLength, merged.remaining());
assertEquals(30L, cc.total(merged));
assertEquals(1, merged.getShort(merged.position()));
assertTrue(Util.equalsCounterId(CounterId.fromInt(1), merged, headerLength));
assertEquals(10L, merged.getLong(merged.position() + headerLength + idLength));
// with equal clock, we should pick the largest value
assertEquals(30L, merged.getLong(merged.position() + headerLength + idLength + clockLength));
//
// Test merging global w/ mixed contexts
//
left = ContextState.allocate(2, 0, 0);
left.writeGlobal(CounterId.fromInt(1), 1L, 1L);
left.writeGlobal(CounterId.fromInt(2), 1L, 1L);
right = ContextState.allocate(0, 1, 1);
right.writeLocal(CounterId.fromInt(1), 100L, 100L);
right.writeRemote(CounterId.fromInt(2), 100L, 100L);
// global shards should dominate local/remote, even with lower clock and value
merged = cc.merge(left.context, right.context);
headerLength = headerSizeLength + 2 * headerEltLength;
assertEquals(headerLength + 2 * stepLength, merged.remaining());
assertEquals(2L, cc.total(merged));
assertEquals(2, merged.getShort(merged.position()));
assertTrue(Util.equalsCounterId(CounterId.fromInt(1), merged, headerLength));
assertEquals(1L, merged.getLong(merged.position() + headerLength + idLength));
assertEquals(1L, merged.getLong(merged.position() + headerLength + idLength + clockLength));
assertTrue(Util.equalsCounterId(CounterId.fromInt(2), merged, headerLength + stepLength));
assertEquals(1L, merged.getLong(merged.position() + headerLength + stepLength + idLength));
assertEquals(1L, merged.getLong(merged.position() + headerLength + stepLength + idLength + clockLength));
}
use of org.apache.cassandra.db.context.CounterContext.ContextState in project cassandra by apache.
the class CounterCellTest method testDiff.
@Test
public void testDiff() {
ColumnFamilyStore cfs = Keyspace.open(KEYSPACE1).getColumnFamilyStore(COUNTER1);
ByteBuffer col = ByteBufferUtil.bytes("val");
Cell leftCell;
Cell rightCell;
// Equal count
leftCell = createLegacyCounterCell(cfs, col, 2, 2);
rightCell = createLegacyCounterCell(cfs, col, 2, 1);
assertEquals(CounterContext.Relationship.EQUAL, CounterContext.instance().diff(leftCell.value(), rightCell.value()));
// Non-equal count
leftCell = createLegacyCounterCell(cfs, col, 1, 2);
rightCell = createLegacyCounterCell(cfs, col, 2, 1);
assertEquals(CounterContext.Relationship.DISJOINT, CounterContext.instance().diff(leftCell.value(), rightCell.value()));
// timestamp
CounterId id = CounterId.generate();
leftCell = createCounterCell(cfs, col, id, 2, 2);
rightCell = createCounterCell(cfs, col, id, 2, 1);
assertEquals(CounterContext.Relationship.GREATER_THAN, CounterContext.instance().diff(leftCell.value(), rightCell.value()));
ContextState leftContext;
ContextState rightContext;
// Equal based on context w/shards etc
leftContext = ContextState.allocate(0, 0, 3);
leftContext.writeRemote(CounterId.fromInt(3), 3L, 0L);
leftContext.writeRemote(CounterId.fromInt(6), 2L, 0L);
leftContext.writeRemote(CounterId.fromInt(9), 1L, 0L);
rightContext = ContextState.wrap(ByteBufferUtil.clone(leftContext.context));
leftCell = createCounterCellFromContext(cfs, col, leftContext, 1);
rightCell = createCounterCellFromContext(cfs, col, rightContext, 1);
assertEquals(CounterContext.Relationship.EQUAL, CounterContext.instance().diff(leftCell.value(), rightCell.value()));
// greater than: left has superset of nodes (counts equal)
leftContext = ContextState.allocate(0, 0, 4);
leftContext.writeRemote(CounterId.fromInt(3), 3L, 0L);
leftContext.writeRemote(CounterId.fromInt(6), 2L, 0L);
leftContext.writeRemote(CounterId.fromInt(9), 1L, 0L);
leftContext.writeRemote(CounterId.fromInt(12), 0L, 0L);
rightContext = ContextState.allocate(0, 0, 3);
rightContext.writeRemote(CounterId.fromInt(3), 3L, 0L);
rightContext.writeRemote(CounterId.fromInt(6), 2L, 0L);
rightContext.writeRemote(CounterId.fromInt(9), 1L, 0L);
leftCell = createCounterCellFromContext(cfs, col, leftContext, 1);
rightCell = createCounterCellFromContext(cfs, col, rightContext, 1);
assertEquals(CounterContext.Relationship.GREATER_THAN, CounterContext.instance().diff(leftCell.value(), rightCell.value()));
assertEquals(CounterContext.Relationship.LESS_THAN, CounterContext.instance().diff(rightCell.value(), leftCell.value()));
// disjoint: right and left have disjoint node sets
leftContext = ContextState.allocate(0, 0, 3);
leftContext.writeRemote(CounterId.fromInt(3), 1L, 0L);
leftContext.writeRemote(CounterId.fromInt(4), 1L, 0L);
leftContext.writeRemote(CounterId.fromInt(9), 1L, 0L);
rightContext = ContextState.allocate(0, 0, 3);
rightContext.writeRemote(CounterId.fromInt(3), 1L, 0L);
rightContext.writeRemote(CounterId.fromInt(6), 1L, 0L);
rightContext.writeRemote(CounterId.fromInt(9), 1L, 0L);
leftCell = createCounterCellFromContext(cfs, col, leftContext, 1);
rightCell = createCounterCellFromContext(cfs, col, rightContext, 1);
assertEquals(CounterContext.Relationship.DISJOINT, CounterContext.instance().diff(leftCell.value(), rightCell.value()));
assertEquals(CounterContext.Relationship.DISJOINT, CounterContext.instance().diff(rightCell.value(), leftCell.value()));
}
use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.
the class SuperColumnTest method testAddColumnIncrementCounter.
@Test
public void testAddColumnIncrementCounter() {
ContextState state;
SuperColumn sc = new SuperColumn(ByteBufferUtil.bytes("sc1"), LongType.instance);
state = ContextState.allocate(4, 1);
state.writeElement(NodeId.fromInt(1), 7L, 0L);
state.writeElement(NodeId.fromInt(2), 5L, 7L);
state.writeElement(NodeId.fromInt(4), 2L, 9L);
state.writeElement(NodeId.getLocalId(), 3L, 3L, true);
sc.addColumn(new CounterColumn(getBytes(1), state.context, 3L, 0L));
state = ContextState.allocate(4, 1);
state.writeElement(NodeId.fromInt(2), 3L, 4L);
state.writeElement(NodeId.fromInt(4), 4L, 1L);
state.writeElement(NodeId.fromInt(8), 9L, 0L);
state.writeElement(NodeId.getLocalId(), 9L, 5L, true);
sc.addColumn(new CounterColumn(getBytes(1), state.context, 10L, 0L));
state = ContextState.allocate(3, 0);
state.writeElement(NodeId.fromInt(2), 1L, 0L);
state.writeElement(NodeId.fromInt(3), 6L, 0L);
state.writeElement(NodeId.fromInt(7), 3L, 0L);
sc.addColumn(new CounterColumn(getBytes(2), state.context, 9L, 0L));
assertNotNull(sc.getSubColumn(getBytes(1)));
assertNull(sc.getSubColumn(getBytes(3)));
// column: 1
ContextState c1 = ContextState.allocate(5, 1);
c1.writeElement(NodeId.fromInt(1), 7L, 0L);
c1.writeElement(NodeId.fromInt(2), 5L, 7L);
c1.writeElement(NodeId.fromInt(4), 4L, 1L);
c1.writeElement(NodeId.fromInt(8), 9L, 0L);
c1.writeElement(NodeId.getLocalId(), 12L, 8L, true);
assert 0 == ByteBufferUtil.compareSubArrays(((CounterColumn) sc.getSubColumn(getBytes(1))).value(), 0, c1.context, 0, c1.context.remaining());
// column: 2
ContextState c2 = ContextState.allocate(3, 0);
c2.writeElement(NodeId.fromInt(2), 1L, 0L);
c2.writeElement(NodeId.fromInt(3), 6L, 0L);
c2.writeElement(NodeId.fromInt(7), 3L, 0L);
assert 0 == ByteBufferUtil.compareSubArrays(((CounterColumn) sc.getSubColumn(getBytes(2))).value(), 0, c2.context, 0, c2.context.remaining());
assertNotNull(sc.getSubColumn(getBytes(1)));
assertNotNull(sc.getSubColumn(getBytes(2)));
assertNull(sc.getSubColumn(getBytes(3)));
}
use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.
the class CounterContextTest method runMergeOldShards.
private void runMergeOldShards(Allocator allocator) {
long now = System.currentTimeMillis();
NodeId id1 = NodeId.fromInt(1);
NodeId id3 = NodeId.fromInt(3);
List<NodeId.NodeIdRecord> records = new ArrayList<NodeId.NodeIdRecord>();
records.add(new NodeId.NodeIdRecord(id1, 2L));
records.add(new NodeId.NodeIdRecord(id3, 4L));
ContextState ctx = ContextState.allocate(5, 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);
ByteBuffer merger = cc.computeOldShardMerger(ctx.context, records, Integer.MAX_VALUE);
ContextState m = new ContextState(merger);
assert m.getNodeId().equals(id1);
assert m.getClock() <= -now;
assert m.getCount() == -1L;
assert m.isDelta();
m.moveToNext();
assert m.getNodeId().equals(id3);
assert m.getClock() <= -now;
assert m.getCount() == -3L;
assert m.isDelta();
m.moveToNext();
assert m.getNodeId().equals(NodeId.getLocalId());
assert m.getClock() == 1L;
assert m.getCount() == 4L;
assert m.isDelta();
assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger, allocator));
}
Aggregations