Search in sources :

Example 1 with BufferPositionParser

use of com.linkedin.databus.core.util.BufferPositionParser in project databus by linkedin.

the class TestScnIndex method testScnIndexOnEventWithLargeBatch.

/*
  @Test
  public void testScnIndexUtilities() {
    ScnIndex index = new ScnIndex(10240000, 20000000000L, 500 * ByteSizeConstants.ONE_MEGABYTE_IN_BYTES);
    assertTrue("blockNumber = "+index.getBlockNumber(17327387028L), index.getBlockNumber(17327387028L) > 0);
  }
 */
@Test
public void testScnIndexOnEventWithLargeBatch() {
    /**
     * Index is broken up into 3 entries for offsets
     * 0-1000, 1001-2000, 2001-3000
     */
    BufferPositionParser parser = new BufferPositionParser(3000, 3);
    ScnIndex index = new ScnIndex(3 * ScnIndex.SIZE_OF_SCN_OFFSET_RECORD, 3000, 10000, parser, AllocationPolicy.DIRECT_MEMORY, false, null, DEFAULT_ASSERT_LEVEL, true, /* enabled */
    ByteOrder.BIG_ENDIAN);
    DbusEvent eopEvent = EasyMock.createNiceMock(DbusEvent.class);
    EasyMock.expect(eopEvent.isEndOfPeriodMarker()).andReturn(true).anyTimes();
    EasyMock.expect(eopEvent.isControlMessage()).andReturn(true).anyTimes();
    EasyMock.replay(eopEvent);
    index.onEvent(eopEvent, 0, 76);
    //200
    // 1st block
    index.onEvent(createMockDataEvent(200L), 500, 0);
    // 1st block
    index.onEvent(eopEvent, 510, 0);
    //200
    // 2nd block
    index.onEvent(createMockDataEvent(250L), 1500, 0);
    // 2nd block
    index.onEvent(eopEvent, 1510, 0);
    //300
    // 3rd block
    index.onEvent(createMockDataEvent(300L), 2100, 0);
    // 3rd block
    index.onEvent(eopEvent, 2210, 0);
    // Index should look like: 200->500, 250->1500, 300->2100
    LOG.info("index=" + index);
    index.printVerboseString(LOG, Level.DEBUG);
    checkSuccess(index, 200, 500, 200);
    checkSuccess(index, 200, 500, 200);
    checkSuccess(index, 300, 2100, 300);
    assertEquals("Head Check", 0, index.getHead());
    assertEquals("Tail Check", 0, index.getTail());
    //400
    index.moveHead(2100);
    long ofs1_450 = index.getPositionParser().setGenId(450, 1);
    // Back to 1st block, this should erase the existence of 200
    index.onEvent(createMockDataEvent(400L), ofs1_450, 0);
    long ofs1_460 = index.getPositionParser().setGenId(460, 1);
    // Back to 1st block
    index.onEvent(eopEvent, ofs1_460, 0);
    // Index should look like: 400->[1:450], 250->1500, 300->2100
    checkSuccess(index, 300, 2100, 300);
    checkSuccess(index, 500, ofs1_450, 400);
    checkFailure(index, 200);
    assertEquals("Head Check", 32, index.getHead());
    assertEquals("Tail Check", 16, index.getTail());
    //500
    long ofs1_1100 = index.getPositionParser().setGenId(1100, 1);
    index.onEvent(createMockDataEvent(500L), ofs1_1100, 0);
    long ofs1_1110 = index.getPositionParser().setGenId(1110, 1);
    // 3rd block
    index.onEvent(eopEvent, ofs1_1110, 0);
    // Index should look like: 400->[1:450], 500->[1:1100], 300->2100
    checkFailure(index, 200);
    checkSuccess(index, 400, ofs1_450, 400);
    checkSuccess(index, 500, ofs1_1100, 500);
    checkSuccess(index, 300, 2100, 300);
    long ofs1_1010 = index.getPositionParser().setGenId(1010, 1);
    assertEquals(2100, index.getLargerOffset(ofs1_1010));
    assertEquals("Head Check", 32, index.getHead());
    assertEquals("Tail Check", 32, index.getTail());
    //600
    long ofs1_1800 = index.getPositionParser().setGenId(1800, 1);
    index.onEvent(createMockDataEvent(600L), ofs1_1800, 0);
    long ofs1_1805 = index.getPositionParser().setGenId(1805, 1);
    // 3rd block
    index.onEvent(eopEvent, ofs1_1805, 0);
    // Index should look like: 400->[1:450], 500->[1:1100], 600->[1:1800], 300->2100
    checkSuccess(index, 600, ofs1_1100, 500);
    checkSuccess(index, 400, ofs1_450, 400);
    checkSuccess(index, 500, ofs1_1100, 500);
    checkSuccess(index, 300, 2100, 300);
    assertEquals("Head Check", 32, index.getHead());
    assertEquals("Tail Check", 32, index.getTail());
    //700
    long ofs0_100 = index.getPositionParser().setGenId(100, 0);
    index.moveHead(ofs0_100);
    long ofs1_2010 = index.getPositionParser().setGenId(2010, 1);
    index.onEvent(createMockDataEvent(700L), ofs1_2010, 0);
    long ofs2_5 = index.getPositionParser().setGenId(5, 2);
    index.onEvent(createMockDataEvent(700L), ofs2_5, 0);
    long ofs2_10 = index.getPositionParser().setGenId(10, 0);
    // 3rd block
    index.onEvent(eopEvent, ofs2_10, 0);
    // Index should look like: 400->[1:450], 500->[1:1100], 600->[1:1800], 700->[1:2010]
    LOG.info("index=" + index);
    checkSuccess(index, 700, ofs1_2010, 700);
    assertEquals(400, index.getMinScn());
    assertEquals("Head Check", 0, index.getHead());
    assertEquals("Tail Check", 0, index.getTail());
    // let's pretend that the next event will end at 451
    long ofs1_451 = index.getPositionParser().setGenId(451, 1);
    long potentialHead = index.getLargerOffset(ofs1_451);
    LOG.info("Potential Head is:" + potentialHead);
    index.moveHead(potentialHead);
    assertEquals(ofs1_1100, potentialHead);
    long ofs1_2900 = index.getPositionParser().setGenId(2900, 1);
    index.moveHead(ofs1_2900);
    // let's pretend that the next event will end at 2500
    long ofs2_2500 = index.getPositionParser().setGenId(2500, 2);
    potentialHead = index.getLargerOffset(ofs2_2500);
    LOG.info("potentialHead=" + potentialHead);
    assertEquals(-1, potentialHead);
    long ofs2_1100 = index.getPositionParser().setGenId(1100, 2);
    index.onEvent(createMockDataEvent(800L), ofs2_1100, 0);
    LOG.info("index=" + index);
    long ofs2_1500 = index.getPositionParser().setGenId(1500, 2);
    index.onEvent(eopEvent, ofs2_1500, 0);
    // Index should look like: 700->2010, 800->1100, 700->2010
    checkSuccess(index, 800, ofs2_1100, 800);
    checkSuccess(index, 700, ofs1_2900, 700);
    checkFailure(index, 500);
    assertEquals(700, index.getMinScn());
    LOG.info("index=" + index);
    assertEquals("Head Check", 32, index.getHead());
    assertEquals("Tail Check", 32, index.getTail());
}
Also used : BufferPositionParser(com.linkedin.databus.core.util.BufferPositionParser) Test(org.testng.annotations.Test)

Example 2 with BufferPositionParser

use of com.linkedin.databus.core.util.BufferPositionParser in project databus by linkedin.

the class TestDbusEventBufferAppendEvents method testAppendBigEventsHeadScnOverlap.

@Test
public /**
   * Test the case where we have a big event that overlaps both the DbusEventBuffer head and the
   * limit of the current buffer.
   */
void testAppendBigEventsHeadScnOverlap() throws Exception {
    final Logger log = Logger.getLogger("TestDbusEventBuffer.testAppendBigEventsHeadScnOverlap");
    log.setLevel(Level.INFO);
    log.info("starting");
    final DbusEventBuffer dbusBuf = new DbusEventBuffer(TestDbusEventBuffer.getConfig(1200, 500, 100, 500, AllocationPolicy.HEAP_MEMORY, QueuePolicy.OVERWRITE_ON_WRITE, AssertLevel.ALL));
    BufferPositionParser parser = dbusBuf.getBufferPositionParser();
    log.info("append initial events");
    DbusEventGenerator generator = new DbusEventGenerator();
    Vector<DbusEvent> events = new Vector<DbusEvent>();
    generator.generateEvents(7, 1, 120, 39, events);
    // Add events to the EventBuffer. Now the buffer is full
    DbusEventAppender appender = new DbusEventAppender(events, dbusBuf, null);
    // running in the same thread
    appender.run();
    log.info("Head:" + parser.toString(dbusBuf.getHead()) + ", Tail:" + parser.toString(dbusBuf.getTail()));
    log.info("Num buffers: " + dbusBuf.getBuffer().length);
    log.info("Buffer: " + Arrays.toString(dbusBuf.getBuffer()));
    long headPos = dbusBuf.getHead();
    long tailPos = dbusBuf.getTail();
    long headGenId = parser.bufferGenId(headPos);
    long headIndexId = parser.bufferIndex(headPos);
    long headOffset = parser.bufferOffset(headPos);
    long tailGenId = parser.bufferGenId(tailPos);
    long tailIndexId = parser.bufferIndex(tailPos);
    long tailOffset = parser.bufferOffset(tailPos);
    assertEquals(0, headGenId);
    assertEquals(0, headIndexId);
    assertEquals(222, headOffset);
    assertEquals(1, tailGenId);
    assertEquals(0, tailIndexId);
    assertEquals(61, tailOffset);
    log.info("append windows with one small and one big event");
    generator = new DbusEventGenerator(100);
    events = new Vector<DbusEvent>();
    generator.generateEvents(1, 1, 280, 139, events);
    generator.generateEvents(1, 1, 480, 339, events);
    // Add events to the EventBuffer. Now the buffer is full
    appender = new DbusEventAppender(events, dbusBuf, null);
    // running in the same thread
    appender.run();
    log.info("Head:" + parser.toString(dbusBuf.getHead()) + ", Tail:" + parser.toString(dbusBuf.getTail()));
    log.info("Num buffers: " + dbusBuf.getBuffer().length);
    log.info("Buffer: " + Arrays.toString(dbusBuf.getBuffer()));
    headPos = dbusBuf.getHead();
    tailPos = dbusBuf.getTail();
    headGenId = parser.bufferGenId(headPos);
    headIndexId = parser.bufferIndex(headPos);
    headOffset = parser.bufferOffset(headPos);
    tailGenId = parser.bufferGenId(tailPos);
    tailIndexId = parser.bufferIndex(tailPos);
    tailOffset = parser.bufferOffset(tailPos);
    assertEquals(0, headGenId);
    assertEquals(2, headIndexId);
    assertEquals(61, headOffset);
    assertEquals(1, tailIndexId);
    assertEquals(461, tailOffset);
    assertEquals(322, dbusBuf.getBuffer()[0].limit());
    log.info("finished");
}
Also used : DbusEventAppender(com.linkedin.databus.core.test.DbusEventAppender) BufferPositionParser(com.linkedin.databus.core.util.BufferPositionParser) DbusEventGenerator(com.linkedin.databus.core.test.DbusEventGenerator) Logger(org.apache.log4j.Logger) Vector(java.util.Vector) Test(org.testng.annotations.Test)

Example 3 with BufferPositionParser

use of com.linkedin.databus.core.util.BufferPositionParser in project databus by linkedin.

the class ReadEventsTestParams method testBufferOverFlow.

/*
   * TestCase to recreate the BufferOverFlowException issue tracked in DDS-793
   */
@Test
public void testBufferOverFlow() throws Exception {
    //DbusEventBuffer.LOG.setLevel(Level.DEBUG);
    final Logger log = Logger.getLogger("TestDbusEventBuffer.testBufferOverflow");
    //log.setLevel(Level.INFO);
    log.info("starting");
    DbusEventBuffer dbusBuf = new DbusEventBuffer(getConfig(1000, 1000, 100, 500, AllocationPolicy.HEAP_MEMORY, QueuePolicy.BLOCK_ON_WRITE, AssertLevel.ALL));
    final DbusEventBuffer dbusBuf2 = new DbusEventBuffer(getConfig(2000, 2000, 100, 1000, AllocationPolicy.HEAP_MEMORY, QueuePolicy.BLOCK_ON_WRITE, AssertLevel.NONE));
    BufferPositionParser parser = dbusBuf.getBufferPositionParser();
    final BufferPositionParser parser2 = dbusBuf2.getBufferPositionParser();
    DbusEventGenerator generator = new DbusEventGenerator();
    Vector<DbusEvent> events = new Vector<DbusEvent>();
    generator.generateEvents(12, 12, 100, 10, events);
    log.info("generate sample events to the EventBuffer");
    DbusEventAppender appender = new DbusEventAppender(events, dbusBuf, null);
    appender.run();
    log.info("dbusBuf : Head:" + parser.toString(dbusBuf.getHead()) + ",Tail:" + parser.toString(dbusBuf.getTail()));
    ByteBuffer[] buf = dbusBuf.getBuffer();
    byte[] b = new byte[(int) dbusBuf.getTail()];
    buf[0].position(0);
    buf[0].get(b);
    log.info("copy data to the destination buffer: 1");
    ReadableByteChannel rChannel = Channels.newChannel(new ByteArrayInputStream(b));
    dbusBuf2.readEvents(rChannel);
    log.info("dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
    rChannel.close();
    log.info("copy data to the destination buffer: 2");
    rChannel = Channels.newChannel(new ByteArrayInputStream(b));
    dbusBuf2.readEvents(rChannel);
    log.info("dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
    log.info("Buffer Size is :" + dbusBuf2.getBuffer().length);
    rChannel.close();
    log.info("process data in destination buffer: 1");
    DbusEventBuffer.DbusEventIterator itr = dbusBuf2.acquireIterator("dummy1");
    for (int i = 0; i < 15; i++) {
        itr.next();
        itr.remove();
    }
    itr.close();
    log.info("dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
    log.info("copy data to the destination buffer: 3");
    rChannel = Channels.newChannel(new ByteArrayInputStream(b));
    dbusBuf2.readEvents(rChannel);
    ByteBuffer[] buf2 = dbusBuf2.getBuffer();
    log.info("dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
    log.info("dbusBuf2 : Buffer :" + buf2[0]);
    rChannel.close();
    log.info("process data in destination buffer: 2");
    itr = dbusBuf2.acquireIterator("dummy2");
    for (int i = 0; i < 15; i++) {
        itr.next();
        itr.remove();
    }
    itr.close();
    log.info("dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
    log.info("dbusBuf2 : Buffer :" + buf2[0]);
    log.info("generate more sample events to the EventBuffer");
    dbusBuf = new DbusEventBuffer(getConfig(2000, 2000, 100, 500, AllocationPolicy.HEAP_MEMORY, QueuePolicy.BLOCK_ON_WRITE, AssertLevel.ALL));
    events = new Vector<DbusEvent>();
    generator.generateEvents(8, 9, 150, 52, events);
    log.info("Events Size is :" + events.get(0).size());
    appender = new DbusEventAppender(events, dbusBuf, null);
    appender.run();
    final AtomicBoolean stopReader = new AtomicBoolean(false);
    Runnable reader = new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(5 * 1000);
            } catch (InterruptedException ie) {
            }
            DbusEventBuffer.DbusEventIterator itr = dbusBuf2.acquireIterator("dummy3");
            log.info("Reader iterator:" + itr);
            while (!stopReader.get() || itr.hasNext()) {
                while (itr.hasNext()) {
                    itr.next();
                    itr.remove();
                }
                itr.await(100, TimeUnit.MILLISECONDS);
            }
            itr.close();
            log.info("Reader Thread: dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
            ByteBuffer[] buf = dbusBuf2.getBuffer();
            log.info("Reader Tread : dbusBuf2 : Buffer :" + buf[0]);
            log.info("Reader iterator:" + itr);
        }
    };
    log.info("generate sample events to the EventBuffer");
    Thread t = new Thread(reader, "BufferOverflowReader");
    b = new byte[(int) dbusBuf.getTail()];
    buf = dbusBuf.getBuffer();
    buf[0].position(0);
    buf[0].get(b);
    log.info("copy data to the destination buffer: 4");
    log.info("Size is :" + b.length);
    rChannel = Channels.newChannel(new ByteArrayInputStream(b));
    // <=== Overflow happened at this point
    dbusBuf2.readEvents(rChannel);
    rChannel.close();
    log.info("dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
    log.info("dbusBuf2 : Buffer :" + buf2[0]);
    log.info("test if the readEvents can allow reader to proceed while it is blocked");
    rChannel = Channels.newChannel(new ByteArrayInputStream(b));
    log.info("start reader thread");
    t.start();
    log.info("copy data to the destination buffer: 5");
    dbusBuf2.readEvents(rChannel);
    rChannel.close();
    log.info("data copied to the destination buffer: 5");
    stopReader.set(true);
    t.join(20000);
    log.info("check if dbusBuf2 is empty");
    Assert.assertTrue(!t.isAlive());
    if (!dbusBuf2.empty()) {
        log.error("dbusBuf2 not empty: " + dbusBuf2);
    }
    Assert.assertTrue(dbusBuf2.toString(), dbusBuf2.empty());
    log.info("dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
    log.info("dbusBuf2 : Buffer :" + buf2[0]);
    log.info("done");
}
Also used : DbusEventAppender(com.linkedin.databus.core.test.DbusEventAppender) ReadableByteChannel(java.nio.channels.ReadableByteChannel) DbusEventGenerator(com.linkedin.databus.core.test.DbusEventGenerator) Logger(org.apache.log4j.Logger) ByteBuffer(java.nio.ByteBuffer) UncaughtExceptionTrackingThread(com.linkedin.databus.core.util.UncaughtExceptionTrackingThread) DbusEventIterator(com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BufferPositionParser(com.linkedin.databus.core.util.BufferPositionParser) ByteArrayInputStream(java.io.ByteArrayInputStream) Vector(java.util.Vector) Test(org.testng.annotations.Test)

Example 4 with BufferPositionParser

use of com.linkedin.databus.core.util.BufferPositionParser in project databus by linkedin.

the class ReadEventsTestParams method testAppendEventOverlapNgt0.

//Case when n> 0
@Test
public void testAppendEventOverlapNgt0() throws Exception {
    final DbusEventBuffer dbusBuf = new DbusEventBuffer(getConfig(1145, 5000, 100, 500, AllocationPolicy.HEAP_MEMORY, QueuePolicy.OVERWRITE_ON_WRITE, AssertLevel.ALL));
    BufferPositionParser parser = dbusBuf.getBufferPositionParser();
    DbusEventGenerator generator = new DbusEventGenerator();
    Vector<DbusEvent> events = new Vector<DbusEvent>();
    generator.generateEvents(9, 3, 120, 39, events);
    // Add events to the EventBuffer. Now the buffer is full
    DbusEventAppender appender = new DbusEventAppender(events, dbusBuf, null);
    // running in the same thread
    appender.run();
    LOG.info("Head:" + parser.toString(dbusBuf.getHead()) + ",Tail:" + parser.toString(dbusBuf.getTail()));
    long headPos = dbusBuf.getHead();
    long tailPos = dbusBuf.getTail();
    long scnIndexHead = dbusBuf.getScnIndex().getHead();
    long scnIndexTail = dbusBuf.getScnIndex().getTail();
    long headGenId = parser.bufferGenId(headPos);
    long headIndexId = parser.bufferIndex(headPos);
    long headOffset = parser.bufferOffset(headPos);
    long tailGenId = parser.bufferGenId(tailPos);
    long tailIndexId = parser.bufferIndex(tailPos);
    long tailOffset = parser.bufferOffset(tailPos);
    assertEquals("Head GenId", 0, headGenId);
    assertEquals("Head Index", 0, headIndexId);
    assertEquals("Head Offset", 0, headOffset);
    assertEquals("Tail GenId", 0, tailGenId);
    assertEquals("Tail Index", 0, tailIndexId);
    assertEquals("Tail Offset", 1144, tailOffset);
    assertEquals("SCNIndex Head", 0, scnIndexHead);
    assertEquals("SCNIndex Tail", 80, scnIndexTail);
    headPos = parser.setGenId(headPos, 300);
    tailPos = parser.setGenId(tailPos, 300);
    dbusBuf.setHead(headPos);
    dbusBuf.setTail(tailPos);
    dbusBuf.recreateIndex();
    events = new Vector<DbusEvent>();
    generator = new DbusEventGenerator(1000);
    /*
     * The event size is carefully created such that after adding 2nd
     * event CWP and tail points to the same location. Now the 3rd event corrupts the EVB and index (in the presence of bug).
     */
    generator.generateEvents(3, 2, 150, 89, events);
    appender = new DbusEventAppender(events, dbusBuf, null);
    LOG.info("1");
    //Logger.getRootLogger().setLevel(Level.ALL);
    // running in the same thread
    appender.run();
    LOG.info("2");
    LOG.info("Head:" + parser.toString(dbusBuf.getHead()) + ",Tail:" + parser.toString(dbusBuf.getTail()));
    headPos = dbusBuf.getHead();
    tailPos = dbusBuf.getTail();
    headGenId = parser.bufferGenId(headPos);
    headIndexId = parser.bufferIndex(headPos);
    headOffset = parser.bufferOffset(headPos);
    tailGenId = parser.bufferGenId(tailPos);
    tailIndexId = parser.bufferIndex(tailPos);
    tailOffset = parser.bufferOffset(tailPos);
    scnIndexHead = dbusBuf.getScnIndex().getHead();
    scnIndexTail = dbusBuf.getScnIndex().getTail();
    assertEquals("Head GenId", 300, headGenId);
    assertEquals("Head Index", 0, headIndexId);
    assertEquals("Head Offset", 783, headOffset);
    assertEquals("Tail GenId", 301, tailGenId);
    assertEquals("Tail Index", 0, tailIndexId);
    assertEquals("Tail Offset", 633, tailOffset);
    assertEquals("SCNIndex Head", 64, scnIndexHead);
    assertEquals("SCNIndex Tail", 48, scnIndexTail);
}
Also used : DbusEventAppender(com.linkedin.databus.core.test.DbusEventAppender) BufferPositionParser(com.linkedin.databus.core.util.BufferPositionParser) DbusEventGenerator(com.linkedin.databus.core.test.DbusEventGenerator) Vector(java.util.Vector) Test(org.testng.annotations.Test)

Example 5 with BufferPositionParser

use of com.linkedin.databus.core.util.BufferPositionParser in project databus by linkedin.

the class ReadEventsTestParams method testBigReadEventBuffer.

@Test
public /*
   * ReadBuffer Size is bigger than the overall EVB size.
   * A large read happens to EVB which is bigger than its size.
   */
void testBigReadEventBuffer() throws Exception {
    final Logger log = Logger.getLogger("TestDbusEventBuffer.testBigReadEventBuffer");
    //log.setLevel(Level.INFO);
    log.info("starting");
    final DbusEventBuffer dbusBuf = new DbusEventBuffer(getConfig(4000, 4000, 100, 500, AllocationPolicy.HEAP_MEMORY, QueuePolicy.BLOCK_ON_WRITE, AssertLevel.ALL));
    final DbusEventBuffer dbusBuf2 = new DbusEventBuffer(getConfig(1000, 1000, 100, 3000, AllocationPolicy.HEAP_MEMORY, QueuePolicy.BLOCK_ON_WRITE, AssertLevel.NONE));
    //dbusBuf2.getLog().setLevel(Level.DEBUG);
    BufferPositionParser parser = dbusBuf.getBufferPositionParser();
    final BufferPositionParser parser2 = dbusBuf2.getBufferPositionParser();
    DbusEventGenerator generator = new DbusEventGenerator();
    Vector<DbusEvent> events = new Vector<DbusEvent>();
    generator.generateEvents(24, 24, 100, 10, events);
    log.info("Num Events :" + events.size());
    // Add events to the EventBuffer
    DbusEventAppender appender = new DbusEventAppender(events, dbusBuf, null);
    appender.run();
    final AtomicBoolean stopReader = new AtomicBoolean(false);
    log.info("dbusBuf : Head:" + parser.toString(dbusBuf.getHead()) + ",Tail:" + parser.toString(dbusBuf.getTail()));
    class EvbReader implements Runnable {

        private int _count = 0;

        public EvbReader() {
            _count = 0;
        }

        public int getCount() {
            return _count;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(5 * 1000);
            } catch (InterruptedException ie) {
            }
            DbusEventBuffer.DbusEventIterator itr = dbusBuf2.acquireIterator("dummy");
            log.info("Reader iterator:" + itr);
            while (!stopReader.get() || itr.hasNext()) {
                while (itr.hasNext()) {
                    itr.next();
                    itr.remove();
                    _count++;
                }
                itr.await(100, TimeUnit.MILLISECONDS);
            }
            log.info("Reader Thread: dbusBuf2 : Head:" + parser2.toString(dbusBuf2.getHead()) + ",Tail:" + parser2.toString(dbusBuf2.getTail()));
            ByteBuffer[] buf = dbusBuf2.getBuffer();
            log.info("Reader Thread : dbusBuf2 : Buffer :" + buf[0]);
            log.info("Count is :" + _count);
            log.info("Reader iterator:" + itr);
        }
    }
    ;
    EvbReader reader = new EvbReader();
    Thread t = new Thread(reader, "BigReadEventReader");
    ByteBuffer[] buf = dbusBuf.getBuffer();
    byte[] b = new byte[(int) dbusBuf.getTail()];
    buf[0].position(0);
    buf[0].get(b);
    ReadableByteChannel rChannel = Channels.newChannel(new ByteArrayInputStream(b));
    t.start();
    dbusBuf2.readEvents(rChannel);
    stopReader.set(true);
    t.join(20000);
    Assert.assertTrue(!t.isAlive());
    DbusEventBuffer.DbusEventIterator itr2 = dbusBuf2.acquireIterator("dummy");
    int count = 0;
    while (itr2.hasNext()) {
        itr2.next();
        itr2.remove();
        count++;
    }
    log.info("Total Count :" + (count + reader.getCount()));
    log.info("Head :" + dbusBuf2.getHead() + ", Tail :" + dbusBuf2.getTail());
    assertEquals("Total Count", 26, (count + reader.getCount()));
    assertEquals("Head == Tail", dbusBuf2.getHead(), dbusBuf2.getTail());
    assertEquals("Head Check:", 2890, dbusBuf2.getHead());
    log.info("done");
}
Also used : DbusEventAppender(com.linkedin.databus.core.test.DbusEventAppender) ReadableByteChannel(java.nio.channels.ReadableByteChannel) DbusEventGenerator(com.linkedin.databus.core.test.DbusEventGenerator) Logger(org.apache.log4j.Logger) ByteBuffer(java.nio.ByteBuffer) UncaughtExceptionTrackingThread(com.linkedin.databus.core.util.UncaughtExceptionTrackingThread) DbusEventIterator(com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BufferPositionParser(com.linkedin.databus.core.util.BufferPositionParser) ByteArrayInputStream(java.io.ByteArrayInputStream) Vector(java.util.Vector) Test(org.testng.annotations.Test)

Aggregations

BufferPositionParser (com.linkedin.databus.core.util.BufferPositionParser)9 Test (org.testng.annotations.Test)9 DbusEventAppender (com.linkedin.databus.core.test.DbusEventAppender)8 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)8 Vector (java.util.Vector)8 Logger (org.apache.log4j.Logger)4 DbusEventIterator (com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)3 UncaughtExceptionTrackingThread (com.linkedin.databus.core.util.UncaughtExceptionTrackingThread)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ReadableByteChannel (java.nio.channels.ReadableByteChannel)3 ByteBuffer (java.nio.ByteBuffer)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1