final Logger log = Logger.getLogger("TestDbusEventBufferMult.testSubscriptionStream");
log.info("start");
TestSetup t = new TestSetup();
PhysicalPartition pp100 = new PhysicalPartition(100, "multBufferTest1");
PhysicalPartitionKey pk1 = new PhysicalPartitionKey(pp100);
PhysicalPartition pp101 = new PhysicalPartition(101, "multBufferTest2");
PhysicalPartitionKey pk2 = new PhysicalPartitionKey(pp101);
//generate events in pp100
byte [] schema = "abcdefghijklmnop".getBytes(Charset.defaultCharset());
DbusEventBufferAppendable buf100 =
t._eventBuffer.getDbusEventBufferAppendable(pp100);
buf100.startEvents();
assertTrue(buf100.appendEvent(new DbusEventKey(1), (short)100, (short)0,
System.currentTimeMillis() * 1000000, (short)1,
schema, new byte[100], false, null));
assertTrue(buf100.appendEvent(new DbusEventKey(10), (short)100, (short)0,
System.currentTimeMillis() * 1000000, (short)1,
schema, new byte[100], false, null));
assertTrue(buf100.appendEvent(new DbusEventKey(11), (short)100, (short)0,
System.currentTimeMillis() * 1000000, (short)1,
schema, new byte[100], false, null));
assertTrue(buf100.appendEvent(new DbusEventKey(2), (short)100, (short)0,
System.currentTimeMillis() * 1000000, (short)2,
schema, new byte[100], false, null));
buf100.endEvents(100, null);
buf100.startEvents();
assertTrue(buf100.appendEvent(new DbusEventKey(3), (short)100, (short)0,
System.currentTimeMillis() * 1000000, (short)2,
schema, new byte[100], false, null));
assertTrue(buf100.appendEvent(new DbusEventKey(4), (short)100, (short)1,
System.currentTimeMillis() * 1000000, (short)2,
schema, new byte[100], false, null));
buf100.endEvents(200, null);
//generate events in pp100
DbusEventBufferAppendable buf101 =
t._eventBuffer.getDbusEventBufferAppendable(pp101);
buf101.startEvents();
assertTrue(buf101.appendEvent(new DbusEventKey(51), (short)101, (short)0,
System.currentTimeMillis() * 1000000, (short)11,
schema, new byte[100], false, null));
assertTrue(buf101.appendEvent(new DbusEventKey(52), (short)101, (short)0,
System.currentTimeMillis() * 1000000, (short)12,
schema, new byte[100], false, null));
assertTrue(buf101.appendEvent(new DbusEventKey(53), (short)101, (short)2,
System.currentTimeMillis() * 1000000, (short)2,
schema, new byte[100], false, null));
buf101.endEvents(120, null);
buf101.startEvents();
assertTrue(buf101.appendEvent(new DbusEventKey(54), (short)101, (short)2,
System.currentTimeMillis() * 1000000, (short)2,
schema, new byte[100], false, null));
assertTrue(buf101.appendEvent(new DbusEventKey(55), (short)101, (short)2,
System.currentTimeMillis() * 1000000, (short)2,
schema, new byte[100], false, null));
assertTrue(buf101.appendEvent(new DbusEventKey(56), (short)101, (short)2,
System.currentTimeMillis() * 1000000, (short)2,
schema, new byte[100], false, null));
buf101.endEvents(200, null);
//initialization
DatabusSubscription sub1 =
DatabusSubscription.createPhysicalPartitionReplicationSubscription(new PhysicalPartition(100, "multBufferTest1"));
DbusFilter filter1 = t._eventBuffer.constructFilters(Arrays.asList(sub1));
assertNotNull(filter1);
CheckpointMult cpMult1 = new CheckpointMult();
Checkpoint cp100 = new Checkpoint();
cp100.init();
cp100.setConsumptionMode(DbusClientMode.ONLINE_CONSUMPTION);
cp100.setWindowScn(10L);
cp100.setWindowOffset(-1);
cpMult1.addCheckpoint(pp100, cp100);
String[] pnames = {"multBufferTest1:100","multBufferTest2:101"};
StatsCollectors<DbusEventsStatisticsCollector> statsColls1 = createStats(pnames);
DbusEventsStatisticsCollector statsCol1 = statsColls1.getStatsCollector("multBufferTest1:100");
DbusEventsStatisticsCollector statsCol2 = statsColls1.getStatsCollector("multBufferTest2:101");
//read an entire buffer
DbusEventBufferBatchReadable reader1 =
t._eventBuffer.getDbusEventBufferBatchReadable(cpMult1, Arrays.asList(pk1), statsColls1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Try a call with 20 bytes of fetch size, we should see the event size in the first return with 0 events read.
StreamEventsResult result = reader1.streamEvents(false, 20, Channels.newChannel(baos),
Encoding.BINARY, filter1);
assertEquals(0, result.getNumEventsStreamed());
assertEquals(161, result.getSizeOfPendingEvent());
result = reader1.streamEvents(false, 1000000, Channels.newChannel(baos),
Encoding.BINARY, filter1);
int eventsRead = result.getNumEventsStreamed();
assertEquals(eventsRead, 8); //4 events + 1 eop + 2 events + 1 eop
assertEquals(statsColls1.getStatsCollector("multBufferTest1:100").getTotalStats().getNumSysEvents(), 2);
assertEquals(statsColls1.getStatsCollector("multBufferTest1:100").getTotalStats().getNumDataEvents(), 6);
assertEquals(result.getSizeOfPendingEvent(), 0, "Size of pending event not zero");
// Now that we have read all the events, we should not see a pending event even if we offer a small fetch size.
result = reader1.streamEvents(false, 20, Channels.newChannel(baos),
Encoding.BINARY, filter1);
assertEquals(0, result.getNumEventsStreamed(), "There should be no more events in the buffer now");
assertEquals(0, result.getSizeOfPendingEvent(), "We should not see pending event size since there are no events in buffer");
baos.reset();
statsCol1.reset(); statsCol2.reset();
//read from two buffers, filtering out one
cpMult1 = new CheckpointMult();
cp100.init();
cp100.setConsumptionMode(DbusClientMode.ONLINE_CONSUMPTION);
cp100.setWindowScn(10L);
cp100.setWindowOffset(-1);
cpMult1.addCheckpoint(pp100, cp100);
reader1 = t._eventBuffer.getDbusEventBufferBatchReadable(cpMult1, Arrays.asList(pk1, pk2),
statsColls1);
eventsRead = reader1.streamEvents(false, 1000000, Channels.newChannel(baos),
Encoding.BINARY, filter1).getNumEventsStreamed();
assertEquals(eventsRead, 10); //4 events + 1 eop + 1 eop from the other buffer + 2 events +
//1 eop + 1 eop from the other buffer
assertEquals(statsColls1.getStatsCollector("multBufferTest1:100").getTotalStats().getNumSysEvents(), 2);
assertEquals(statsColls1.getStatsCollector("multBufferTest1:100").getTotalStats().getNumDataEvents(), 6);
baos.reset();
statsCol1.reset();
//read from one buffer and one source partition
DatabusSubscription sub2 =
new DatabusSubscription(PhysicalSource.MASTER_PHISYCAL_SOURCE,
new PhysicalPartition(101, "multBufferTest2"),
new LogicalSourceId(new LogicalSource(2, "srcName2"), (short)2)
);
DbusFilter filter2 = t._eventBuffer.constructFilters(Arrays.asList(sub2));
assertNotNull(filter2);