Search in sources :

Example 1 with LogBufferWriter

use of io.cdap.cdap.logging.logbuffer.LogBufferWriter in project cdap by caskdata.

the class LogBufferCleanerTest method testLogBufferCleanerService.

@Test
public void testLogBufferCleanerService() throws Exception {
    String absolutePath = TMP_FOLDER.newFolder().getAbsolutePath();
    MockCheckpointManager checkpointManager = new MockCheckpointManager();
    // update checkpoints
    checkpointManager.saveCheckpoints(ImmutableMap.of(0, new TestCheckpoint(2L, 0L, 1L)));
    // write directly to log buffer, keep file size 10 bytes so that more files are created
    LogBufferWriter writer = new LogBufferWriter(absolutePath, 10, new LogBufferCleaner(ImmutableList.of(checkpointManager), absolutePath, new AtomicBoolean(true)));
    ImmutableList<byte[]> events = getLoggingEvents();
    List<byte[]> subset = new ArrayList<>();
    subset.add(events.get(0));
    subset.add(events.get(1));
    subset.add(events.get(2));
    writer.write(subset.iterator()).iterator();
    // should delete file 0 an1
    File file0 = new File(absolutePath, "0.buff");
    File file1 = new File(absolutePath, "1.buff");
    Tasks.waitFor(true, () -> !file0.exists() && !file1.exists(), 120, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // update checkpoints
    checkpointManager.saveCheckpoints(ImmutableMap.of(0, new TestCheckpoint(5L, 0L, 1L)));
    subset.add(events.get(3));
    subset.add(events.get(4));
    subset.add(events.get(5));
    writer.write(subset.iterator()).iterator();
    writer.close();
    // should delete file 2, 3 and 4
    File file2 = new File(absolutePath, "2.buff");
    File file3 = new File(absolutePath, "3.buff");
    File file4 = new File(absolutePath, "4.buff");
    Tasks.waitFor(true, () -> !file2.exists() && !file3.exists() && !file4.exists(), 120, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockCheckpointManager(io.cdap.cdap.logging.logbuffer.MockCheckpointManager) ArrayList(java.util.ArrayList) File(java.io.File) LogBufferWriter(io.cdap.cdap.logging.logbuffer.LogBufferWriter) Test(org.junit.Test)

Example 2 with LogBufferWriter

use of io.cdap.cdap.logging.logbuffer.LogBufferWriter in project cdap by caskdata.

the class LogBufferRecoveryServiceTest method testLogBufferRecoveryService.

@Test
public void testLogBufferRecoveryService() throws Exception {
    String absolutePath = TMP_FOLDER.newFolder().getAbsolutePath();
    // create and start pipeline
    LoggerContext loggerContext = LogPipelineTestUtil.createLoggerContext("WARN", ImmutableMap.of("test.logger", "INFO"), MockAppender.class.getName());
    final MockAppender appender = LogPipelineTestUtil.getAppender(loggerContext.getLogger(Logger.ROOT_LOGGER_NAME), "Test", MockAppender.class);
    MockCheckpointManager checkpointManager = new MockCheckpointManager();
    LogBufferPipelineConfig config = new LogBufferPipelineConfig(1024L, 300L, 500L, 4);
    loggerContext.start();
    LogBufferProcessorPipeline pipeline = new LogBufferProcessorPipeline(new LogProcessorPipelineContext(CConfiguration.create(), "test", loggerContext, NO_OP_METRICS_CONTEXT, 0), config, checkpointManager, 0);
    // start the pipeline
    pipeline.startAndWait();
    // write directly to log buffer
    LogBufferWriter writer = new LogBufferWriter(absolutePath, 250, () -> {
    });
    ImmutableList<byte[]> events = getLoggingEvents();
    writer.write(events.iterator()).iterator();
    writer.close();
    // start log buffer reader to read log events from files. keep the batch size as 2 so that there are more than 1
    // iterations
    LogBufferRecoveryService service = new LogBufferRecoveryService(ImmutableList.of(pipeline), ImmutableList.of(checkpointManager), absolutePath, 2, new AtomicBoolean(true));
    service.startAndWait();
    Tasks.waitFor(5, () -> appender.getEvents().size(), 120, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    service.stopAndWait();
    pipeline.stopAndWait();
    loggerContext.stop();
}
Also used : MockAppender(io.cdap.cdap.logging.pipeline.MockAppender) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockCheckpointManager(io.cdap.cdap.logging.logbuffer.MockCheckpointManager) LogBufferProcessorPipeline(io.cdap.cdap.logging.pipeline.logbuffer.LogBufferProcessorPipeline) LogBufferPipelineConfig(io.cdap.cdap.logging.pipeline.logbuffer.LogBufferPipelineConfig) LogProcessorPipelineContext(io.cdap.cdap.logging.pipeline.LogProcessorPipelineContext) LoggerContext(ch.qos.logback.classic.LoggerContext) LogBufferWriter(io.cdap.cdap.logging.logbuffer.LogBufferWriter) Test(org.junit.Test)

Example 3 with LogBufferWriter

use of io.cdap.cdap.logging.logbuffer.LogBufferWriter in project cdap by caskdata.

the class LogBufferReaderTest method testLogReader.

@Test
public void testLogReader() throws Exception {
    String absolutePath = TMP_FOLDER.newFolder().getAbsolutePath();
    LogBufferWriter writer = new LogBufferWriter(absolutePath, 250, () -> {
    });
    ImmutableList<byte[]> events = getLoggingEvents();
    Iterable<LogBufferEvent> writtenEvents = writer.write(events.iterator());
    writer.close();
    List<LogBufferEvent> logBufferEvents = new LinkedList<>();
    // read from start positions, tests case where no checkpoints are persisted
    LogBufferReader reader = new LogBufferReader(absolutePath, 2, 3, -1, -1);
    Iterator<LogBufferEvent> iterator = writtenEvents.iterator();
    verifyEvents(logBufferEvents, reader, iterator);
    reader.close();
    // this should skip first and second event, this is because log buffer offsets are offset for event that is
    // already stored. so in this case, skip first event and skip second event as second event is the last stored event
    reader = new LogBufferReader(absolutePath, 2, 3, 0, 145);
    iterator = writtenEvents.iterator();
    iterator.next();
    iterator.next();
    verifyEvents(logBufferEvents, reader, iterator);
    reader.close();
}
Also used : LogBufferReader(io.cdap.cdap.logging.logbuffer.recover.LogBufferReader) LogBufferEvent(io.cdap.cdap.logging.logbuffer.LogBufferEvent) LogBufferWriter(io.cdap.cdap.logging.logbuffer.LogBufferWriter) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

LogBufferWriter (io.cdap.cdap.logging.logbuffer.LogBufferWriter)3 Test (org.junit.Test)3 MockCheckpointManager (io.cdap.cdap.logging.logbuffer.MockCheckpointManager)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 LogBufferEvent (io.cdap.cdap.logging.logbuffer.LogBufferEvent)1 LogBufferReader (io.cdap.cdap.logging.logbuffer.recover.LogBufferReader)1 LogProcessorPipelineContext (io.cdap.cdap.logging.pipeline.LogProcessorPipelineContext)1 MockAppender (io.cdap.cdap.logging.pipeline.MockAppender)1 LogBufferPipelineConfig (io.cdap.cdap.logging.pipeline.logbuffer.LogBufferPipelineConfig)1 LogBufferProcessorPipeline (io.cdap.cdap.logging.pipeline.logbuffer.LogBufferProcessorPipeline)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1