public static ConsumerWorkingSet fromBytes(byte[] bytes) { ByteBuffer bb = ByteBuffer.wrap(bytes); byte serializationFormatVersion = bb.get(); if (serializationFormatVersion != VERSION) { throw new IllegalArgumentException("Unsupported serialization format: " + serializationFormatVersion); } int numPartitions = bb.getInt(); List<ConsumablePartition> partitions = new ArrayList<>(numPartitions); for (int i = 0; i < numPartitions; i++) { int consumablePartitionBytesLength = bb.getInt(); byte[] consumablePartitionBytes = new byte[consumablePartitionBytesLength]; bb.get(consumablePartitionBytes, 0, consumablePartitionBytesLength); partitions.add(DefaultConsumablePartition.fromBytes(consumablePartitionBytes)); } int sizeOfMarker = bb.getInt(); byte[] markerBytes = new byte[sizeOfMarker]; bb.get(markerBytes); return new ConsumerWorkingSet(PartitionConsumerState.fromBytes(markerBytes), partitions); }
private ConsumerWorkingSet readState() { byte[] bytes = statePersistor.readState(); return bytes == null ? new ConsumerWorkingSet() : ConsumerWorkingSet.fromBytes(bytes); } }
@Test public void testByteSerialization() { ConsumerWorkingSet workingSet = new ConsumerWorkingSet(); // test with empty partitions lists testSerDe(workingSet); // test with two elements in AVAILABLE and none in IN_PROGRESS workingSet.getPartitions().add(new DefaultConsumablePartition(generateUniqueKey())); workingSet.getPartitions().add(new DefaultConsumablePartition(generateUniqueKey())); testSerDe(workingSet); // test with three elements in partitions and none in inProgressPartitions workingSet.getPartitions().add(new DefaultConsumablePartition(generateUniqueKey())); testSerDe(workingSet); // mark the first element as IN_PROGRESS workingSet.getPartitions().get(0).take(); workingSet.getPartitions().get(0).setTimestamp(System.currentTimeMillis()); testSerDe(workingSet); }