Skip to content

Instantly share code, notes, and snippets.

@akalin
Created June 19, 2017 18:34
Show Gist options
  • Save akalin/fe573a4476980f57ff5025c6df007e40 to your computer and use it in GitHub Desktop.
Save akalin/fe573a4476980f57ff5025c6df007e40 to your computer and use it in GitHub Desktop.
Test demonstrating singularity with 257 shards
/**
* Try encoding and decoding with too many shards.
*/
@Test
public void testTooManyEncodeDecode() {
final Random random = new Random(0);
final int dataCount = 240;
// Have just enough parity shards to go over the maximum of
// 256.
final int parityCount = 17;
final int shardSize = 20;
byte [] [] dataShards = new byte [dataCount] [shardSize];
for (byte [] shard : dataShards) {
for (int i = 0; i < shard.length; i++) {
shard[i] = (byte) random.nextInt(256);
}
}
final int totalCount = dataCount + parityCount;
final int shardLength = dataShards[0].length;
// Make the list of data and parity shards.
assertEquals(dataCount, dataShards.length);
final int dataLength = dataShards[0].length;
byte [] [] allShards = new byte [totalCount] [];
for (int i = 0; i < dataCount; i++) {
allShards[i] = Arrays.copyOf(dataShards[i], dataLength);
}
for (int i = dataCount; i < totalCount; i++) {
allShards[i] = new byte [dataLength];
}
// Encode. The built matrix will have as its last row
// [ 1 0 0 ... ], which is of course identical to the first row,
// and thus any square subset of the rows including the first and
// last row will be singular.
//
// Requires the shard count check in ReedSolomon.create to be
// commented out.
ReedSolomon codec = ReedSolomon.create(dataCount, parityCount);
codec.encodeParity(allShards, 0, dataLength);
// Make a copy to decode with.
byte [] [] testShards = new byte [totalCount] [];
boolean [] shardPresent = new boolean [totalCount];
for (int i = 0; i < totalCount; i++) {
testShards[i] = Arrays.copyOf(allShards[i], shardLength);
shardPresent[i] = true;
}
// Drop the second data shard...
clearBytes(testShards[1]);
shardPresent[1] = false;
// ... and try to recover it with the last parity shard,
// i.e. drop all other parity shards.
for (int i = 0; i < 16; i++) {
clearBytes(testShards[240+i]);
shardPresent[240+i] = false;
}
// Try to reconstruct the missing shards, which should lead to
// a singular matrix exception.
codec.decodeMissing(testShards, shardPresent, 0, shardLength);
checkShards(allShards, testShards);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment