use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.
the class CounterContextTest method runDiff.
private void runDiff(Allocator allocator) {
ContextState left = ContextState.allocate(3, 0, allocator);
ContextState right;
// equality: equal nodes, all counts same
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), left.headerLength);
assert ContextRelationship.EQUAL == cc.diff(left.context, right.context);
// 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);
assert ContextRelationship.GREATER_THAN == cc.diff(left.context, right.context);
// less than: left has subset of nodes (counts equal)
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 = ContextState.allocate(4, 0, allocator);
right.writeElement(NodeId.fromInt(3), 3L, 0L);
right.writeElement(NodeId.fromInt(6), 2L, 0L);
right.writeElement(NodeId.fromInt(9), 1L, 0L);
right.writeElement(NodeId.fromInt(12), 0L, 0L);
assert ContextRelationship.LESS_THAN == cc.diff(left.context, right.context);
// greater than: equal nodes, but left has higher counts
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), 3L, 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);
assert ContextRelationship.GREATER_THAN == cc.diff(left.context, right.context);
// less than: equal nodes, but right has higher counts
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), 3L, 0L);
right = ContextState.allocate(3, 0, allocator);
right.writeElement(NodeId.fromInt(3), 3L, 0L);
right.writeElement(NodeId.fromInt(6), 9L, 0L);
right.writeElement(NodeId.fromInt(9), 3L, 0L);
assert ContextRelationship.LESS_THAN == cc.diff(left.context, right.context);
// 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);
assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context);
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(2), 1L, 0L);
right.writeElement(NodeId.fromInt(6), 1L, 0L);
right.writeElement(NodeId.fromInt(12), 1L, 0L);
assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context);
// disjoint: equal nodes, but right and left have higher counts in differing nodes
left = ContextState.allocate(3, 0, allocator);
left.writeElement(NodeId.fromInt(3), 1L, 0L);
left.writeElement(NodeId.fromInt(6), 3L, 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), 5L, 0L);
assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context);
left = ContextState.allocate(3, 0, allocator);
left.writeElement(NodeId.fromInt(3), 2L, 0L);
left.writeElement(NodeId.fromInt(6), 3L, 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), 9L, 0L);
right.writeElement(NodeId.fromInt(9), 5L, 0L);
assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context);
// disjoint: left has more nodes, but lower counts
left = ContextState.allocate(4, 0, allocator);
left.writeElement(NodeId.fromInt(3), 2L, 0L);
left.writeElement(NodeId.fromInt(6), 3L, 0L);
left.writeElement(NodeId.fromInt(9), 1L, 0L);
left.writeElement(NodeId.fromInt(12), 1L, 0L);
right = ContextState.allocate(3, 0, allocator);
right.writeElement(NodeId.fromInt(3), 4L, 0L);
right.writeElement(NodeId.fromInt(6), 9L, 0L);
right.writeElement(NodeId.fromInt(9), 5L, 0L);
assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context);
// disjoint: left has less nodes, but higher counts
left = ContextState.allocate(3, 0, allocator);
left.writeElement(NodeId.fromInt(3), 5L, 0L);
left.writeElement(NodeId.fromInt(6), 3L, 0L);
left.writeElement(NodeId.fromInt(9), 2L, 0L);
right = ContextState.allocate(4, 0, allocator);
right.writeElement(NodeId.fromInt(3), 4L, 0L);
right.writeElement(NodeId.fromInt(6), 3L, 0L);
right.writeElement(NodeId.fromInt(9), 2L, 0L);
right.writeElement(NodeId.fromInt(12), 1L, 0L);
assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context);
// disjoint: mixed nodes and counts
left = ContextState.allocate(3, 0, allocator);
left.writeElement(NodeId.fromInt(3), 5L, 0L);
left.writeElement(NodeId.fromInt(6), 2L, 0L);
left.writeElement(NodeId.fromInt(9), 2L, 0L);
right = ContextState.allocate(4, 0, allocator);
right.writeElement(NodeId.fromInt(3), 4L, 0L);
right.writeElement(NodeId.fromInt(6), 3L, 0L);
right.writeElement(NodeId.fromInt(9), 2L, 0L);
right.writeElement(NodeId.fromInt(12), 1L, 0L);
assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context);
left = ContextState.allocate(4, 0, allocator);
left.writeElement(NodeId.fromInt(3), 5L, 0L);
left.writeElement(NodeId.fromInt(6), 2L, 0L);
left.writeElement(NodeId.fromInt(7), 2L, 0L);
left.writeElement(NodeId.fromInt(9), 2L, 0L);
right = ContextState.allocate(3, 0, allocator);
right.writeElement(NodeId.fromInt(3), 4L, 0L);
right.writeElement(NodeId.fromInt(6), 3L, 0L);
right.writeElement(NodeId.fromInt(9), 2L, 0L);
assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context);
}
use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.
the class CounterContextTest method runRemoveNotDeltaOldShards.
private void runRemoveNotDeltaOldShards(Allocator allocator) {
ContextState ctx = ContextState.allocate(4, 1, allocator);
ctx.writeElement(NodeId.fromInt(1), 1L, 1L, true);
ctx.writeElement(NodeId.fromInt(2), -System.currentTimeMillis(), 0L);
ctx.writeElement(NodeId.fromInt(3), -System.currentTimeMillis(), 0L);
ctx.writeElement(NodeId.fromInt(4), -System.currentTimeMillis(), 0L);
ByteBuffer cleaned = cc.removeOldShards(ctx.context, (int) (System.currentTimeMillis() / 1000) + 1);
assert cc.total(ctx.context) == cc.total(cleaned);
assert cleaned.remaining() == ctx.context.remaining() - 3 * stepLength;
}
use of org.apache.cassandra.db.context.CounterContext.ContextState in project eiger by wlloyd.
the class CounterContextTest method runMerge.
private void runMerge(Allocator allocator) {
// note: local counts aggregated; remote counts are reconciled (i.e. take max)
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), 6L, 3L);
left.writeElement(NodeId.getLocalId(), 7L, 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(), 2L, 9L, true);
ByteBuffer merged = cc.merge(left.context, right.context, allocator);
int hd = 4;
assertEquals(hd + 5 * stepLength, merged.remaining());
// local node id's counts are aggregated
assert Util.equalsNodeId(NodeId.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)
assert Util.equalsNodeId(NodeId.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));
assert Util.equalsNodeId(NodeId.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));
assert Util.equalsNodeId(NodeId.fromInt(2), merged, hd + 1 * stepLength);
assertEquals(2L, merged.getLong(merged.position() + hd + 1 * stepLength + idLength));
assertEquals(2L, merged.getLong(merged.position() + hd + 1 * stepLength + idLength + clockLength));
assert Util.equalsNodeId(NodeId.fromInt(1), merged, hd + 0 * stepLength);
assertEquals(1L, merged.getLong(merged.position() + hd + 0 * stepLength + idLength));
assertEquals(1L, merged.getLong(merged.position() + hd + 0 * stepLength + idLength + clockLength));
}
use of org.apache.cassandra.db.context.CounterContext.ContextState in project cassandra by apache.
the class CounterContextTest method testFindPositionOf.
@Test
public void testFindPositionOf() {
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);
int headerLength = headerSizeLength + 6 * headerEltLength;
assertEquals(headerLength, cc.findPositionOf(state.context, CounterId.fromInt(1)));
assertEquals(headerLength + stepLength, cc.findPositionOf(state.context, CounterId.fromInt(2)));
assertEquals(headerLength + 2 * stepLength, cc.findPositionOf(state.context, CounterId.fromInt(3)));
assertEquals(headerLength + 3 * stepLength, cc.findPositionOf(state.context, CounterId.fromInt(4)));
assertEquals(headerLength + 4 * stepLength, cc.findPositionOf(state.context, CounterId.fromInt(5)));
assertEquals(headerLength + 5 * stepLength, cc.findPositionOf(state.context, CounterId.fromInt(6)));
assertEquals(headerLength + 6 * stepLength, cc.findPositionOf(state.context, CounterId.fromInt(7)));
assertEquals(headerLength + 7 * stepLength, cc.findPositionOf(state.context, CounterId.fromInt(8)));
assertEquals(headerLength + 8 * stepLength, cc.findPositionOf(state.context, CounterId.fromInt(9)));
assertEquals(-1, cc.findPositionOf(state.context, CounterId.fromInt(0)));
assertEquals(-1, cc.findPositionOf(state.context, CounterId.fromInt(10)));
assertEquals(-1, cc.findPositionOf(state.context, CounterId.fromInt(15)));
assertEquals(-1, cc.findPositionOf(state.context, CounterId.fromInt(20)));
}
use of org.apache.cassandra.db.context.CounterContext.ContextState in project cassandra by apache.
the class CounterContextTest method testDiff.
@Test
public void testDiff() {
ContextState left;
ContextState right;
// equality: equal nodes, all counts same
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 3L, 0L);
left.writeRemote(CounterId.fromInt(6), 2L, 0L);
left.writeRemote(CounterId.fromInt(9), 1L, 0L);
right = ContextState.wrap(ByteBufferUtil.clone(left.context));
assertEquals(Relationship.EQUAL, cc.diff(left.context, right.context));
// greater than: left has superset of nodes (counts equal)
left = ContextState.allocate(0, 0, 4);
left.writeRemote(CounterId.fromInt(3), 3L, 0L);
left.writeRemote(CounterId.fromInt(6), 2L, 0L);
left.writeRemote(CounterId.fromInt(9), 1L, 0L);
left.writeRemote(CounterId.fromInt(12), 0L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(3), 3L, 0L);
right.writeRemote(CounterId.fromInt(6), 2L, 0L);
right.writeRemote(CounterId.fromInt(9), 1L, 0L);
assertEquals(Relationship.GREATER_THAN, cc.diff(left.context, right.context));
// less than: left has subset of nodes (counts equal)
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 3L, 0L);
left.writeRemote(CounterId.fromInt(6), 2L, 0L);
left.writeRemote(CounterId.fromInt(9), 1L, 0L);
right = ContextState.allocate(0, 0, 4);
right.writeRemote(CounterId.fromInt(3), 3L, 0L);
right.writeRemote(CounterId.fromInt(6), 2L, 0L);
right.writeRemote(CounterId.fromInt(9), 1L, 0L);
right.writeRemote(CounterId.fromInt(12), 0L, 0L);
assertEquals(Relationship.LESS_THAN, cc.diff(left.context, right.context));
// greater than: equal nodes, but left has higher counts
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 3L, 0L);
left.writeRemote(CounterId.fromInt(6), 2L, 0L);
left.writeRemote(CounterId.fromInt(9), 3L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(3), 3L, 0L);
right.writeRemote(CounterId.fromInt(6), 2L, 0L);
right.writeRemote(CounterId.fromInt(9), 1L, 0L);
assertEquals(Relationship.GREATER_THAN, cc.diff(left.context, right.context));
// less than: equal nodes, but right has higher counts
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 3L, 0L);
left.writeRemote(CounterId.fromInt(6), 2L, 0L);
left.writeRemote(CounterId.fromInt(9), 3L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(3), 3L, 0L);
right.writeRemote(CounterId.fromInt(6), 9L, 0L);
right.writeRemote(CounterId.fromInt(9), 3L, 0L);
assertEquals(Relationship.LESS_THAN, cc.diff(left.context, right.context));
// disjoint: right and left have disjoint node sets
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 1L, 0L);
left.writeRemote(CounterId.fromInt(4), 1L, 0L);
left.writeRemote(CounterId.fromInt(9), 1L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(3), 1L, 0L);
right.writeRemote(CounterId.fromInt(6), 1L, 0L);
right.writeRemote(CounterId.fromInt(9), 1L, 0L);
assertEquals(Relationship.DISJOINT, cc.diff(left.context, right.context));
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 1L, 0L);
left.writeRemote(CounterId.fromInt(4), 1L, 0L);
left.writeRemote(CounterId.fromInt(9), 1L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(2), 1L, 0L);
right.writeRemote(CounterId.fromInt(6), 1L, 0L);
right.writeRemote(CounterId.fromInt(12), 1L, 0L);
assertEquals(Relationship.DISJOINT, cc.diff(left.context, right.context));
// disjoint: equal nodes, but right and left have higher counts in differing nodes
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 1L, 0L);
left.writeRemote(CounterId.fromInt(6), 3L, 0L);
left.writeRemote(CounterId.fromInt(9), 1L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(3), 1L, 0L);
right.writeRemote(CounterId.fromInt(6), 1L, 0L);
right.writeRemote(CounterId.fromInt(9), 5L, 0L);
assertEquals(Relationship.DISJOINT, cc.diff(left.context, right.context));
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 2L, 0L);
left.writeRemote(CounterId.fromInt(6), 3L, 0L);
left.writeRemote(CounterId.fromInt(9), 1L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(3), 1L, 0L);
right.writeRemote(CounterId.fromInt(6), 9L, 0L);
right.writeRemote(CounterId.fromInt(9), 5L, 0L);
assertEquals(Relationship.DISJOINT, cc.diff(left.context, right.context));
// disjoint: left has more nodes, but lower counts
left = ContextState.allocate(0, 0, 4);
left.writeRemote(CounterId.fromInt(3), 2L, 0L);
left.writeRemote(CounterId.fromInt(6), 3L, 0L);
left.writeRemote(CounterId.fromInt(9), 1L, 0L);
left.writeRemote(CounterId.fromInt(12), 1L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(3), 4L, 0L);
right.writeRemote(CounterId.fromInt(6), 9L, 0L);
right.writeRemote(CounterId.fromInt(9), 5L, 0L);
assertEquals(Relationship.DISJOINT, cc.diff(left.context, right.context));
// disjoint: left has less nodes, but higher counts
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 5L, 0L);
left.writeRemote(CounterId.fromInt(6), 3L, 0L);
left.writeRemote(CounterId.fromInt(9), 2L, 0L);
right = ContextState.allocate(0, 0, 4);
right.writeRemote(CounterId.fromInt(3), 4L, 0L);
right.writeRemote(CounterId.fromInt(6), 3L, 0L);
right.writeRemote(CounterId.fromInt(9), 2L, 0L);
right.writeRemote(CounterId.fromInt(12), 1L, 0L);
assertEquals(Relationship.DISJOINT, cc.diff(left.context, right.context));
// disjoint: mixed nodes and counts
left = ContextState.allocate(0, 0, 3);
left.writeRemote(CounterId.fromInt(3), 5L, 0L);
left.writeRemote(CounterId.fromInt(6), 2L, 0L);
left.writeRemote(CounterId.fromInt(9), 2L, 0L);
right = ContextState.allocate(0, 0, 4);
right.writeRemote(CounterId.fromInt(3), 4L, 0L);
right.writeRemote(CounterId.fromInt(6), 3L, 0L);
right.writeRemote(CounterId.fromInt(9), 2L, 0L);
right.writeRemote(CounterId.fromInt(12), 1L, 0L);
assertEquals(Relationship.DISJOINT, cc.diff(left.context, right.context));
left = ContextState.allocate(0, 0, 4);
left.writeRemote(CounterId.fromInt(3), 5L, 0L);
left.writeRemote(CounterId.fromInt(6), 2L, 0L);
left.writeRemote(CounterId.fromInt(7), 2L, 0L);
left.writeRemote(CounterId.fromInt(9), 2L, 0L);
right = ContextState.allocate(0, 0, 3);
right.writeRemote(CounterId.fromInt(3), 4L, 0L);
right.writeRemote(CounterId.fromInt(6), 3L, 0L);
right.writeRemote(CounterId.fromInt(9), 2L, 0L);
assertEquals(Relationship.DISJOINT, cc.diff(left.context, right.context));
}
Aggregations