Search in sources :

Example 6 with Checkpoint

use of co.cask.cdap.logging.meta.Checkpoint in project cdap by caskdata.

the class DistributedLogFrameworkTest method testFramework.

@Test
public void testFramework() throws Exception {
    DistributedLogFramework framework = injector.getInstance(DistributedLogFramework.class);
    CConfiguration cConf = injector.getInstance(CConfiguration.class);
    framework.startAndWait();
    // Send some logs to Kafka.
    LoggingContext context = new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, "test");
    // Make sure all events get flushed in the same batch
    long eventTimeBase = System.currentTimeMillis() + cConf.getInt(Constants.Logging.PIPELINE_EVENT_DELAY_MS);
    final int msgCount = 50;
    for (int i = 0; i < msgCount; i++) {
        // Publish logs in descending timestamp order
        publishLog(cConf.get(Constants.Logging.KAFKA_TOPIC), context, ImmutableList.of(createLoggingEvent("co.cask.test." + i, Level.INFO, "Testing " + i, eventTimeBase - i)));
    }
    // Read the logs back. They should be sorted by timestamp order.
    final FileMetaDataReader metaDataReader = injector.getInstance(FileMetaDataReader.class);
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            List<LogLocation> locations = metaDataReader.listFiles(new LogPathIdentifier(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, "test"), 0, Long.MAX_VALUE);
            if (locations.size() != 1) {
                return false;
            }
            LogLocation location = locations.get(0);
            int i = 0;
            try {
                try (CloseableIterator<LogEvent> iter = location.readLog(Filter.EMPTY_FILTER, 0, Long.MAX_VALUE, msgCount)) {
                    while (iter.hasNext()) {
                        String expectedMsg = "Testing " + (msgCount - i - 1);
                        LogEvent event = iter.next();
                        if (!expectedMsg.equals(event.getLoggingEvent().getMessage())) {
                            return false;
                        }
                        i++;
                    }
                    return i == msgCount;
                }
            } catch (Exception e) {
                // and the time when actual content are flushed to the file
                return false;
            }
        }
    }, 10, TimeUnit.SECONDS, msgCount, TimeUnit.MILLISECONDS);
    framework.stopAndWait();
    // Check the checkpoint is persisted correctly. Since all messages are processed,
    // the checkpoint should be the same as the message count.
    Checkpoint checkpoint = injector.getInstance(CheckpointManagerFactory.class).create(cConf.get(Constants.Logging.KAFKA_TOPIC), Bytes.toBytes(100)).getCheckpoint(0);
    Assert.assertEquals(msgCount, checkpoint.getNextOffset());
}
Also used : CloseableIterator(co.cask.cdap.api.dataset.lib.CloseableIterator) LoggingContext(co.cask.cdap.common.logging.LoggingContext) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext) LogEvent(co.cask.cdap.logging.read.LogEvent) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Checkpoint(co.cask.cdap.logging.meta.Checkpoint) IOException(java.io.IOException) Checkpoint(co.cask.cdap.logging.meta.Checkpoint) LogLocation(co.cask.cdap.logging.write.LogLocation) FileMetaDataReader(co.cask.cdap.logging.meta.FileMetaDataReader) LogPathIdentifier(co.cask.cdap.logging.appender.system.LogPathIdentifier) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 7 with Checkpoint

use of co.cask.cdap.logging.meta.Checkpoint in project cdap by caskdata.

the class TestDistributedLogReader method generateCheckpointTime.

private static void generateCheckpointTime(LoggingContext loggingContext, int numExpectedEvents, String kafkaTopic) throws Exception {
    FileLogReader logReader = injector.getInstance(FileLogReader.class);
    List<LogEvent> events = Lists.newArrayList(logReader.getLog(loggingContext, 0, Long.MAX_VALUE, Filter.EMPTY_FILTER));
    Assert.assertEquals(numExpectedEvents, events.size());
    // Save checkpoint (time of last event)
    CheckpointManagerFactory checkpointManagerFactory = injector.getInstance(CheckpointManagerFactory.class);
    CheckpointManager checkpointManager = checkpointManagerFactory.create(kafkaTopic, Constants.Logging.SYSTEM_PIPELINE_CHECKPOINT_PREFIX);
    long checkpointTime = events.get(numExpectedEvents - 1).getLoggingEvent().getTimeStamp();
    checkpointManager.saveCheckpoints(ImmutableMap.of(stringPartitioner.partition(loggingContext.getLogPartition(), -1), new Checkpoint(numExpectedEvents, checkpointTime, checkpointTime)));
}
Also used : Checkpoint(co.cask.cdap.logging.meta.Checkpoint) LogEvent(co.cask.cdap.logging.read.LogEvent) CheckpointManager(co.cask.cdap.logging.meta.CheckpointManager) CheckpointManagerFactory(co.cask.cdap.logging.meta.CheckpointManagerFactory) FileLogReader(co.cask.cdap.logging.read.FileLogReader)

Aggregations

Checkpoint (co.cask.cdap.logging.meta.Checkpoint)7 IOException (java.io.IOException)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2 LogEvent (co.cask.cdap.logging.read.LogEvent)2 Int2LongMap (it.unimi.dsi.fastutil.ints.Int2LongMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 KafkaException (org.apache.kafka.common.KafkaException)2 LeaderNotAvailableException (org.apache.kafka.common.errors.LeaderNotAvailableException)2 NotLeaderForPartitionException (org.apache.kafka.common.errors.NotLeaderForPartitionException)2 OffsetOutOfRangeException (org.apache.kafka.common.errors.OffsetOutOfRangeException)2 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)2 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)2 Test (org.junit.Test)2 CloseableIterator (co.cask.cdap.api.dataset.lib.CloseableIterator)1 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1 LoggingContext (co.cask.cdap.common.logging.LoggingContext)1 ServiceLoggingContext (co.cask.cdap.common.logging.ServiceLoggingContext)1 LogPathIdentifier (co.cask.cdap.logging.appender.system.LogPathIdentifier)1 CheckpointManager (co.cask.cdap.logging.meta.CheckpointManager)1 CheckpointManagerFactory (co.cask.cdap.logging.meta.CheckpointManagerFactory)1