use of io.pravega.controller.store.stream.records.StreamSegmentRecord in project pravega by pravega.
the class StreamTestBase method predecessorAndSuccessorTest.
@Test(timeout = 30000L)
public void predecessorAndSuccessorTest() {
// multiple rows in history table, find predecessor
// - more than one predecessor
// - one predecessor
// - no predecessor
// - immediate predecessor
// - predecessor few rows behind
OperationContext context = getContext();
int startingSegmentNumber = new Random().nextInt(2000);
Stream stream = createStream("scope", "stream" + startingSegmentNumber, System.currentTimeMillis(), 5, startingSegmentNumber);
long timestamp = System.currentTimeMillis();
List<StreamSegmentRecord> segments = stream.getEpochRecord(0, context).join().getSegments();
StreamSegmentRecord zero = segments.stream().filter(x -> x.segmentId() == startingSegmentNumber + 0L).findAny().get();
StreamSegmentRecord one = segments.stream().filter(x -> x.segmentId() == startingSegmentNumber + 1L).findAny().get();
StreamSegmentRecord two = segments.stream().filter(x -> x.segmentId() == startingSegmentNumber + 2L).findAny().get();
StreamSegmentRecord three = segments.stream().filter(x -> x.segmentId() == startingSegmentNumber + 3L).findAny().get();
StreamSegmentRecord four = segments.stream().filter(x -> x.segmentId() == startingSegmentNumber + 4L).findAny().get();
// 3, 4 -> 5
timestamp = timestamp + 1;
List<Map.Entry<Double, Double>> newRanges = new ArrayList<>();
newRanges.add(new AbstractMap.SimpleEntry<>(three.getKeyStart(), four.getKeyEnd()));
scaleStream(stream, timestamp, Lists.newArrayList(three.segmentId(), four.segmentId()), newRanges, Collections.emptyMap());
segments = stream.getEpochRecord(1, context).join().getSegments();
StreamSegmentRecord five = segments.stream().filter(x -> x.segmentId() == computeSegmentId(5 + startingSegmentNumber, 1)).findAny().get();
// 1 -> 6,7.. 2,5 -> 8
timestamp = timestamp + 10;
newRanges = new ArrayList<>();
newRanges.add(new AbstractMap.SimpleEntry<>(one.getKeyStart(), 0.3));
newRanges.add(new AbstractMap.SimpleEntry<>(0.3, one.getKeyEnd()));
newRanges.add(new AbstractMap.SimpleEntry<>(two.getKeyStart(), five.getKeyEnd()));
scaleStream(stream, timestamp, Lists.newArrayList(one.segmentId(), two.segmentId(), five.segmentId()), newRanges, Collections.emptyMap());
segments = stream.getEpochRecord(2, context).join().getSegments();
StreamSegmentRecord six = segments.stream().filter(x -> x.segmentId() == computeSegmentId(6 + startingSegmentNumber, 2)).findAny().get();
StreamSegmentRecord seven = segments.stream().filter(x -> x.segmentId() == computeSegmentId(7 + startingSegmentNumber, 2)).findAny().get();
StreamSegmentRecord eight = segments.stream().filter(x -> x.segmentId() == computeSegmentId(8 + startingSegmentNumber, 2)).findAny().get();
// 7 -> 9,10.. 8 -> 10, 11
timestamp = timestamp + 10;
newRanges = new ArrayList<>();
newRanges.add(new AbstractMap.SimpleEntry<>(seven.getKeyStart(), 0.35));
newRanges.add(new AbstractMap.SimpleEntry<>(0.35, 0.6));
newRanges.add(new AbstractMap.SimpleEntry<>(0.6, 1.0));
scaleStream(stream, timestamp, Lists.newArrayList(seven.segmentId(), eight.segmentId()), newRanges, Collections.emptyMap());
segments = stream.getEpochRecord(3, context).join().getSegments();
StreamSegmentRecord nine = segments.stream().filter(x -> x.segmentId() == computeSegmentId(9 + startingSegmentNumber, 3)).findAny().get();
StreamSegmentRecord ten = segments.stream().filter(x -> x.segmentId() == computeSegmentId(10 + startingSegmentNumber, 3)).findAny().get();
StreamSegmentRecord eleven = segments.stream().filter(x -> x.segmentId() == computeSegmentId(11 + startingSegmentNumber, 3)).findAny().get();
// 9, 10, 11 -> 12
timestamp = timestamp + 10;
newRanges = new ArrayList<>();
newRanges.add(new AbstractMap.SimpleEntry<>(0.3, 1.0));
scaleStream(stream, timestamp, Lists.newArrayList(nine.segmentId(), ten.segmentId(), eleven.segmentId()), newRanges, Collections.emptyMap());
segments = stream.getEpochRecord(4, context).join().getSegments();
StreamSegmentRecord twelve = segments.stream().filter(x -> x.segmentId() == computeSegmentId(12 + startingSegmentNumber, 4)).findAny().get();
// verification
Map<StreamSegmentRecord, List<Long>> successorsWithPredecessors;
Set<StreamSegmentRecord> successors;
Set<Long> predecessors;
// StreamSegmentRecord 0
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(zero.segmentId(), context).join();
assertTrue(successorsWithPredecessors.isEmpty());
// segment 1 --> (6, <1>), (7, <1>)
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(one.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(2, successors.size());
assertTrue(successors.contains(six));
predecessors = new HashSet<>(successorsWithPredecessors.get(six));
assertEquals(1, predecessors.size());
assertTrue(predecessors.contains(one.segmentId()));
assertTrue(successors.contains(seven));
predecessors = new HashSet<>(successorsWithPredecessors.get(seven));
assertEquals(1, predecessors.size());
assertTrue(predecessors.contains(one.segmentId()));
// segment 3 --> (5, <3, 4>)
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(three.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(1, successors.size());
assertTrue(successors.contains(five));
predecessors = new HashSet<>(successorsWithPredecessors.get(five));
assertEquals(2, predecessors.size());
assertTrue(predecessors.contains(three.segmentId()));
assertTrue(predecessors.contains(four.segmentId()));
// segment 4 --> (5, <3, 4>)
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(four.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(1, successors.size());
assertTrue(successors.contains(five));
predecessors = new HashSet<>(successorsWithPredecessors.get(five));
assertEquals(2, predecessors.size());
assertTrue(predecessors.contains(three.segmentId()));
assertTrue(predecessors.contains(four.segmentId()));
// segment 5 --> (8, <2, 5>)
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(five.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(1, successors.size());
assertTrue(successors.contains(eight));
predecessors = new HashSet<>(successorsWithPredecessors.get(eight));
assertEquals(2, predecessors.size());
assertTrue(predecessors.contains(two.segmentId()));
assertTrue(predecessors.contains(five.segmentId()));
// segment 6 --> empty
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(six.segmentId(), context).join();
assertTrue(successorsWithPredecessors.isEmpty());
// segment 7 --> 9 <7>, 10 <7, 8>
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(seven.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(2, successors.size());
assertTrue(successors.contains(nine));
predecessors = new HashSet<>(successorsWithPredecessors.get(nine));
assertEquals(1, predecessors.size());
assertTrue(predecessors.contains(seven.segmentId()));
assertTrue(successors.contains(ten));
predecessors = new HashSet<>(successorsWithPredecessors.get(ten));
assertEquals(2, predecessors.size());
assertTrue(predecessors.contains(seven.segmentId()));
assertTrue(predecessors.contains(eight.segmentId()));
// segment 8 --> 10 <7, 8>, 11 <8>
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(eight.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(2, successors.size());
assertTrue(successors.contains(ten));
predecessors = new HashSet<>(successorsWithPredecessors.get(ten));
assertEquals(2, predecessors.size());
assertTrue(predecessors.contains(seven.segmentId()));
assertTrue(predecessors.contains(eight.segmentId()));
assertTrue(successors.contains(eleven));
predecessors = new HashSet<>(successorsWithPredecessors.get(eleven));
assertEquals(1, predecessors.size());
assertTrue(predecessors.contains(eight.segmentId()));
// segment 9 --> 12 <9, 10, 11>
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(nine.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(1, successors.size());
assertTrue(successors.contains(twelve));
predecessors = new HashSet<>(successorsWithPredecessors.get(twelve));
assertEquals(3, predecessors.size());
assertTrue(predecessors.contains(nine.segmentId()));
assertTrue(predecessors.contains(ten.segmentId()));
assertTrue(predecessors.contains(eleven.segmentId()));
// segment 10 --> 12 <9, 10, 11>
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(ten.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(1, successors.size());
assertTrue(successors.contains(twelve));
predecessors = new HashSet<>(successorsWithPredecessors.get(twelve));
assertEquals(3, predecessors.size());
assertTrue(predecessors.contains(nine.segmentId()));
assertTrue(predecessors.contains(ten.segmentId()));
assertTrue(predecessors.contains(eleven.segmentId()));
// segment 11 --> 12 <9, 10, 11>
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(eleven.segmentId(), context).join();
successors = successorsWithPredecessors.keySet();
assertEquals(1, successors.size());
assertTrue(successors.contains(twelve));
predecessors = new HashSet<>(successorsWithPredecessors.get(twelve));
assertEquals(3, predecessors.size());
assertTrue(predecessors.contains(nine.segmentId()));
assertTrue(predecessors.contains(ten.segmentId()));
assertTrue(predecessors.contains(eleven.segmentId()));
// segment 12 --> empty
successorsWithPredecessors = stream.getSuccessorsWithPredecessors(twelve.segmentId(), context).join();
assertTrue(successorsWithPredecessors.isEmpty());
}
Aggregations