Search in sources :

Example 6 with LogReader

use of com.twitter.distributedlog.LogReader in project distributedlog by twitter.

the class SyncReaderBenchmark method benchmark.

@Override
protected void benchmark(DistributedLogNamespace namespace, String streamName, StatsLogger statsLogger) {
    DistributedLogManager dlm = null;
    while (null == dlm) {
        try {
            dlm = namespace.openLog(streamName);
        } catch (IOException ioe) {
            logger.warn("Failed to create dlm for stream {} : ", streamName, ioe);
        }
        if (null == dlm) {
            try {
                TimeUnit.MILLISECONDS.sleep(conf.getZKSessionTimeoutMilliseconds());
            } catch (InterruptedException e) {
            }
        }
    }
    OpStatsLogger openReaderStats = statsLogger.getOpStatsLogger("open_reader");
    OpStatsLogger nonBlockingReadStats = statsLogger.getOpStatsLogger("non_blocking_read");
    OpStatsLogger blockingReadStats = statsLogger.getOpStatsLogger("blocking_read");
    Counter nullReadCounter = statsLogger.getCounter("null_read");
    logger.info("Created dlm for stream {}.", streamName);
    LogReader reader = null;
    Long lastTxId = null;
    while (null == reader) {
        // initialize the last txid
        if (null == lastTxId) {
            switch(readMode) {
                case OLDEST:
                    lastTxId = 0L;
                    break;
                case LATEST:
                    try {
                        lastTxId = dlm.getLastTxId();
                    } catch (IOException ioe) {
                        continue;
                    }
                    break;
                case REWIND:
                    lastTxId = System.currentTimeMillis() - rewindMs;
                    break;
                case POSITION:
                    lastTxId = fromTxId;
                    break;
                default:
                    logger.warn("Unsupported mode {}", readMode);
                    printUsage();
                    System.exit(0);
                    break;
            }
            logger.info("Reading from transaction id {}", lastTxId);
        }
        // Open the reader
        Stopwatch stopwatch = Stopwatch.createStarted();
        try {
            reader = dlm.getInputStream(lastTxId);
            long elapsedMs = stopwatch.elapsed(TimeUnit.MICROSECONDS);
            openReaderStats.registerSuccessfulEvent(elapsedMs);
            logger.info("It took {} ms to position the reader to transaction id {}", lastTxId);
        } catch (IOException ioe) {
            openReaderStats.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
            logger.warn("Failed to create reader for stream {} reading from {}.", streamName, lastTxId);
        }
        if (null == reader) {
            try {
                TimeUnit.MILLISECONDS.sleep(conf.getZKSessionTimeoutMilliseconds());
            } catch (InterruptedException e) {
            }
            continue;
        }
        // read loop
        LogRecord record;
        boolean nonBlocking = false;
        stopwatch = Stopwatch.createUnstarted();
        while (true) {
            try {
                stopwatch.start();
                record = reader.readNext(nonBlocking);
                if (null != record) {
                    long elapsedMicros = stopwatch.stop().elapsed(TimeUnit.MICROSECONDS);
                    if (nonBlocking) {
                        nonBlockingReadStats.registerSuccessfulEvent(elapsedMicros);
                    } else {
                        blockingReadStats.registerSuccessfulEvent(elapsedMicros);
                    }
                    lastTxId = record.getTransactionId();
                } else {
                    nullReadCounter.inc();
                }
                if (null == record && !nonBlocking) {
                    nonBlocking = true;
                }
                stopwatch.reset();
            } catch (IOException e) {
                logger.warn("Encountered reading record from stream {} : ", streamName, e);
                reader = null;
                break;
            }
        }
        try {
            TimeUnit.MILLISECONDS.sleep(conf.getZKSessionTimeoutMilliseconds());
        } catch (InterruptedException e) {
        }
    }
}
Also used : Counter(org.apache.bookkeeper.stats.Counter) LogRecord(com.twitter.distributedlog.LogRecord) DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) LogReader(com.twitter.distributedlog.LogReader) Stopwatch(com.google.common.base.Stopwatch) OpStatsLogger(org.apache.bookkeeper.stats.OpStatsLogger) IOException(java.io.IOException)

Example 7 with LogReader

use of com.twitter.distributedlog.LogReader in project distributedlog by twitter.

the class TestDistributedLogServer method testHeartbeat.

@Test(timeout = 60000)
public void testHeartbeat() throws Exception {
    String name = "dlserver-heartbeat";
    dlClient.routingService.addHost(name, dlServer.getAddress());
    for (long i = 1; i <= 10; i++) {
        logger.debug("Send heartbeat {} to stream {}.", i, name);
        dlClient.dlClient.check(name).get();
    }
    logger.debug("Write entry one to stream {}.", name);
    dlClient.dlClient.write(name, ByteBuffer.wrap("1".getBytes())).get();
    Thread.sleep(1000);
    DistributedLogManager dlm = DLMTestUtil.createNewDLM(name, conf, getUri());
    LogReader reader = dlm.getInputStream(DLSN.InitialDLSN);
    int numRead = 0;
    // eid=0 => control records
    // other 9 heartbeats will not trigger writing any control records.
    // eid=1 => user entry
    long startEntryId = 1;
    LogRecordWithDLSN r = reader.readNext(false);
    while (null != r) {
        int i = Integer.parseInt(new String(r.getPayload()));
        assertEquals(numRead + 1, i);
        assertEquals(r.getDlsn().compareTo(new DLSN(1, startEntryId, 0)), 0);
        ++numRead;
        ++startEntryId;
        r = reader.readNext(false);
    }
    assertEquals(1, numRead);
}
Also used : LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) DLSN(com.twitter.distributedlog.DLSN) LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) AsyncLogReader(com.twitter.distributedlog.AsyncLogReader) LogReader(com.twitter.distributedlog.LogReader) Test(org.junit.Test)

Example 8 with LogReader

use of com.twitter.distributedlog.LogReader in project distributedlog by twitter.

the class TestDistributedLogServer method runSimpleBulkWriteTest.

private void runSimpleBulkWriteTest(int writeCount) throws Exception {
    String name = String.format("dlserver-bulk-write-%d", writeCount);
    dlClient.routingService.addHost(name, dlServer.getAddress());
    List<ByteBuffer> writes = new ArrayList<ByteBuffer>(writeCount);
    for (long i = 1; i <= writeCount; i++) {
        writes.add(ByteBuffer.wrap(("" + i).getBytes()));
    }
    logger.debug("Write {} entries to stream {}.", writeCount, name);
    List<Future<DLSN>> futures = dlClient.dlClient.writeBulk(name, writes);
    assertEquals(futures.size(), writeCount);
    for (Future<DLSN> future : futures) {
        // No throw == pass.
        DLSN dlsn = Await.result(future, Duration.fromSeconds(10));
    }
    DistributedLogManager dlm = DLMTestUtil.createNewDLM(name, conf, getUri());
    LogReader reader = dlm.getInputStream(1);
    int numRead = 0;
    LogRecord r = reader.readNext(false);
    while (null != r) {
        int i = Integer.parseInt(new String(r.getPayload()));
        assertEquals(numRead + 1, i);
        ++numRead;
        r = reader.readNext(false);
    }
    assertEquals(writeCount, numRead);
    reader.close();
    dlm.close();
}
Also used : DLSN(com.twitter.distributedlog.DLSN) LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) LogRecord(com.twitter.distributedlog.LogRecord) DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) AsyncLogReader(com.twitter.distributedlog.AsyncLogReader) LogReader(com.twitter.distributedlog.LogReader) Future(com.twitter.util.Future)

Aggregations

DistributedLogManager (com.twitter.distributedlog.DistributedLogManager)8 LogReader (com.twitter.distributedlog.LogReader)8 AsyncLogReader (com.twitter.distributedlog.AsyncLogReader)6 LogRecord (com.twitter.distributedlog.LogRecord)6 Test (org.junit.Test)6 DLSN (com.twitter.distributedlog.DLSN)5 LogRecordWithDLSN (com.twitter.distributedlog.LogRecordWithDLSN)5 Stopwatch (com.google.common.base.Stopwatch)1 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)1 HeartbeatOptions (com.twitter.distributedlog.thrift.service.HeartbeatOptions)1 Future (com.twitter.util.Future)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Counter (org.apache.bookkeeper.stats.Counter)1 OpStatsLogger (org.apache.bookkeeper.stats.OpStatsLogger)1 Ignore (org.junit.Ignore)1