use of com.ms.silverking.cloud.ring.RingRegion in project SilverKing by Morgan-Stanley.
the class RegionTreePruner method pruneLeafNode.
private static ChecksumNode pruneLeafNode(LeafChecksumNode node, RingRegion pruneRegion) {
IntersectionResult iResult;
RingRegion oRegion;
// long[] _kvc;
List<KeyAndVersionChecksum> kvcList;
List<KeyAndVersionChecksum> prunedKVCList;
if (!node.getRegion().overlaps(pruneRegion)) {
throw new RuntimeException("panic");
}
iResult = RingRegion.intersect(node.getRegion(), pruneRegion);
if (iResult.getIntersectionType() == IntersectionType.wrappedPartial) {
// FUTURE - handle this
throw new RuntimeException("wrappedPartial pruning not yet handled");
}
oRegion = iResult.getOverlapping().get(0);
// _kvc = node.getKeyAndVersionChecksumsAsArray();
// FUTURE - improve the efficiency of this approach
// This is n log n, and each step is heavy
// Should be able to do at least log n with very light steps
kvcList = node.getKeyAndVersionChecksums();
prunedKVCList = new ArrayList<>(kvcList.size());
for (KeyAndVersionChecksum kvc : kvcList) {
if (pruneRegion.contains(KeyUtil.keyToCoordinate(kvc.getKey()))) {
prunedKVCList.add(kvc);
}
}
return new LeafChecksumNode(oRegion, prunedKVCList);
}
use of com.ms.silverking.cloud.ring.RingRegion in project SilverKing by Morgan-Stanley.
the class RegionTreeBuilder method main.
public static void main(String[] args) {
if (args.length != 4) {
System.out.println("numKeys keysPerNode regionStart regionEnd");
} else {
RegionTreeBuilder rtb;
int numKeys;
int keysPerNode;
long regionStart;
long regionEnd;
RingRegion region;
numKeys = Integer.parseInt(args[0]);
keysPerNode = Integer.parseInt(args[1]);
regionStart = Long.parseLong(args[2]);
regionEnd = Long.parseLong(args[3]);
region = new RingRegion(regionStart, regionEnd);
rtb = new RegionTreeBuilder(region, keysPerNode, numKeys);
for (int i = 0; i < numKeys; i++) {
rtb.addChecksum(new KeyAndVersionChecksum(KeyUtil.randomRegionKey(region), i));
}
System.out.println(rtb);
}
}
use of com.ms.silverking.cloud.ring.RingRegion in project SilverKing by Morgan-Stanley.
the class RegionTreeBuilder method createInternalNodes.
private static ChecksumNode createInternalNodes(LeafChecksumNode[] leaves, int height, int entriesPerNode, int estimatedKeys) {
ChecksumNode[][] nodes;
nodes = new ChecksumNode[height][];
/*
* 0 => root
* height - 1 => leaves
*
* e.g. with 128 keys, 8 per node we have
* row numMembers children or content per member
* 0: 1 2
* 1: 2 8
* 2: 16 8 (content in this case)
*
*/
int nextRowChildren;
nextRowChildren = estimatedKeys;
for (int i = height - 1; i >= 0; i--) {
// System.out.printf("\n%d **********************************\n", i);
if (i < height - 1) {
int rowSize;
int childrenPerNode;
assert nextRowChildren > 0;
rowSize = (nextRowChildren - 1) / entriesPerNode + 1;
nodes[i] = new NonLeafChecksumNode[rowSize];
// childrenPerNode = Math.round(nextRowChildren / rowSize);
childrenPerNode = entriesPerNode;
for (int j = 0; j < nodes[i].length; j++) {
RingRegion region;
List<ChecksumNode> children;
int i0;
int i1;
i0 = j * childrenPerNode;
i1 = Math.min(i0 + childrenPerNode, nextRowChildren);
children = ImmutableList.copyOf(Arrays.copyOfRange(nodes[i + 1], i0, i1));
region = new RingRegion(children.get(0).getRegion().getStart(), children.get(children.size() - 1).getRegion().getEnd());
nodes[i][j] = new NonLeafChecksumNode(region, children);
if (debug) {
System.out.printf("%d %d\t%d %d\t%s\n", i, j, i0, i1, nodes[i][j].getRegion());
}
}
nextRowChildren = rowSize;
} else {
nodes[i] = leaves;
nextRowChildren = leaves.length;
}
if (debug) {
System.out.println();
}
}
if (nodes[0].length != 1) {
throw new RuntimeException("panic");
}
return nodes[0][0];
}
use of com.ms.silverking.cloud.ring.RingRegion in project SilverKing by Morgan-Stanley.
the class ProtoChecksumTreeMessageGroup method deserialize.
public static ChecksumNode deserialize(ByteBuffer buffer) {
NodeType type;
RingRegion region;
int numChildren;
AbstractChecksumNode root;
type = NodeType.values()[buffer.get()];
if (type != NodeType.NULL) {
region = deserializeRegion(buffer);
numChildren = buffer.getInt();
} else {
region = null;
numChildren = 0;
}
switch(type) {
case NON_LEAF:
List<ChecksumNode> children;
children = new ArrayList<>(numChildren);
for (int i = 0; i < numChildren; i++) {
children.add(deserialize(buffer));
}
root = new NonLeafChecksumNode(region, children);
break;
case LEAF:
List<KeyAndVersionChecksum> keyAndVersionChecksums;
keyAndVersionChecksums = deserializeKeyAndVersionChecksums(buffer);
root = new LeafChecksumNode(region, keyAndVersionChecksums);
break;
case NULL:
root = null;
break;
default:
throw new RuntimeException("panic");
}
if (root != null) {
root.freezeIfNotFrozen();
}
return root;
}
use of com.ms.silverking.cloud.ring.RingRegion in project SilverKing by Morgan-Stanley.
the class RingTree method cleanupList.
private List<RingEntry> cleanupList(RingRegion ringspace, List<RingEntry> dirty) {
if (Math.abs(RingRegion.getTotalSize(RingEntry.getRegions(dirty)) - ringspace.getSize()) > allowedError) {
System.err.println("ringspace");
System.err.println(ringspace);
System.err.println("RingEntry.toString(dirty)");
System.err.println(RingEntry.toString(dirty));
System.err.printf("RingRegion.getTotalSize(RingEntry.getRegions(dirty)) %d\n", RingRegion.getTotalSize(RingEntry.getRegions(dirty)));
System.err.printf("ringspace.getSize() %d\n", ringspace.getSize());
throw new RuntimeException("Size error exceeds allowed limit");
} else {
List<RingEntry> clean;
clean = new ArrayList<>(dirty.size());
if (dirty.size() == 0) {
// no action required
} else if (dirty.size() == 1) {
clean.add(dirty.get(0).replaceRegion(ringspace));
} else {
for (int i = 0; i < dirty.size() - 1; i++) {
RingRegion r1;
RingRegion r2;
long error;
int j;
j = (i + 1) % dirty.size();
r1 = dirty.get(i).getRegion();
r2 = dirty.get(j).getRegion();
error = Math.abs(LongRingspace.nextPoint(r1.getEnd()) - r2.getStart());
if (error != 0) {
if (error > allowedContiquityError) {
System.err.println(r1);
System.err.println(r2);
throw new RuntimeException("Contiguity error exceeds allowed limit");
} else {
clean.add(dirty.get(i).replaceRegion(new RingRegion(r1.getStart(), LongRingspace.prevPoint(r2.getStart()))));
/*
if (r1.getSize() >= r2.getSize()) {
clean.add(dirty.get(i).replaceRegion(new RingRegion(r1.getStart(), LongRingspace.prevPoint(r2.getStart()))));
} else {
//clean.add(dirty.get(j).replaceRegion(new RingRegion(LongRingspace.nextPoint(r1.getEnd()), r2.getEnd())));
}
*/
}
} else {
clean.add(dirty.get(i));
}
}
clean.add(dirty.get(dirty.size() - 1));
}
return RingEntry.simplify(clean);
}
}
Aggregations