use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.
the class KafkaOffsetResolverTest method findExpectedOffsetByTime.
/**
* Finds the smallest next offset with corresponding timestamp equal to targetTime,
* or the largest next offset with corresponding timestamp smaller than (targetTime - EVENT_DELAY_MILLIS) if
* no event has timestamp equal to targetTime, or the smallest offset 0 if no event has timestamp smaller
* than (targetTime - EVENT_DELAY_MILLIS)
*/
private long findExpectedOffsetByTime(List<ILoggingEvent> events, long targetTime) {
long offset = 0;
// Use smallest offset 0 as default value in case no event has timestamp smaller
long latestOffsetBeforeMinTime = 0;
long minTime = targetTime - EVENT_DELAY_MILLIS;
for (ILoggingEvent event : events) {
long time = event.getTimeStamp();
if (time == targetTime) {
// Increment the offset to get the next offset
return offset + 1;
}
if (time < minTime) {
// Increment the offset to get the next offset
latestOffsetBeforeMinTime = offset + 1;
}
offset++;
}
// No event contains timestamp equal to targetTime, return latestOffsetBeforeMinTime
return latestOffsetBeforeMinTime;
}
use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.
the class KafkaOffsetResolverTest method testInOrderEvents.
@Test
public void testInOrderEvents() throws InterruptedException, IOException {
String topic = "testInOrderEvents";
KafkaPipelineConfig config = new KafkaPipelineConfig(topic, Collections.singleton(0), 1024L, EVENT_DELAY_MILLIS, 1048576, 200L);
KAFKA_TESTER.createTopic(topic, 1);
// Publish log messages to Kafka and wait for all messages to be published
long baseTime = System.currentTimeMillis() - EVENT_DELAY_MILLIS;
List<ILoggingEvent> inOrderEvents = new ArrayList<>();
for (int i = 0; i < 20; i++) {
inOrderEvents.add(createLoggingEvent("test.logger", Level.INFO, Integer.toString(i), baseTime + i));
}
publishLog(topic, inOrderEvents);
waitForAllLogsPublished(topic, inOrderEvents.size());
KafkaOffsetResolver offsetResolver = new KafkaOffsetResolver(KAFKA_TESTER.getBrokerService(), config);
// Use every event's timestamp as target time and assert that found offset is the next offset of the current offset
for (int i = 0; i < inOrderEvents.size(); i++) {
long targetTime = inOrderEvents.get(i).getTimeStamp();
long offset = offsetResolver.getStartOffset(new Checkpoint(Long.MAX_VALUE, targetTime, 0), 0);
Assert.assertEquals("Failed to find the expected event with the target time: " + targetTime, i + 1, offset);
}
}
use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.
the class LoggingEventTest method testEmptySerialize.
@Test
public void testEmptySerialize() throws Exception {
Logger logger = LoggerFactory.getLogger(LoggingEventTest.class);
ch.qos.logback.classic.spi.LoggingEvent iLoggingEvent = new ch.qos.logback.classic.spi.LoggingEvent(getClass().getName(), (ch.qos.logback.classic.Logger) logger, Level.ERROR, null, null, null);
LoggingEventSerializer serializer = new LoggingEventSerializer();
byte[] encoded = serializer.toBytes(new LogMessage(iLoggingEvent, LoggingContextAccessor.getLoggingContext()));
ILoggingEvent decodedEvent = serializer.fromBytes(ByteBuffer.wrap(encoded));
LoggingEventSerializerTest.assertLoggingEventEquals(iLoggingEvent, decodedEvent);
}
use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.
the class LoggingEventSerializerTest method testEmptySerialization.
@Test
public void testEmptySerialization() throws Exception {
Logger logger = LoggerFactory.getLogger(LoggingEventSerializerTest.class);
LoggingEventSerializer serializer = new LoggingEventSerializer();
ch.qos.logback.classic.spi.LoggingEvent iLoggingEvent = new ch.qos.logback.classic.spi.LoggingEvent(getClass().getName(), (ch.qos.logback.classic.Logger) logger, Level.ERROR, "message", null, null);
iLoggingEvent.setThreadName("thread-1");
iLoggingEvent.setTimeStamp(10000000L);
// Serialize
ILoggingEvent event = new LogMessage(iLoggingEvent, LoggingContextAccessor.getLoggingContext());
byte[] serializedBytes = serializer.toBytes(event);
// De-serialize
ILoggingEvent actualEvent = serializer.fromBytes(ByteBuffer.wrap(serializedBytes));
assertLoggingEventEquals(iLoggingEvent, actualEvent);
}
use of ch.qos.logback.classic.spi.ILoggingEvent in project cdap by caskdata.
the class LogLocation method readToEndSyncPosition.
/**
* Read current block in Avro file from current block sync marker to next block sync marker
*/
private List<LogEvent> readToEndSyncPosition(DataFileReader<GenericRecord> dataFileReader, Filter logFilter, long fromTimeMs, long endSyncPosition) throws IOException {
List<LogEvent> logSegment = new ArrayList<>();
long currentSyncPosition = dataFileReader.previousSync();
// or read until endSyncPosition has been reached
while (dataFileReader.hasNext() && (endSyncPosition == -1 || (currentSyncPosition < endSyncPosition))) {
ILoggingEvent loggingEvent = new LoggingEvent(dataFileReader.next());
loggingEvent.prepareForDeferredProcessing();
// Stop when reached fromTimeMs
if (loggingEvent.getTimeStamp() > fromTimeMs) {
break;
}
if (logFilter.match(loggingEvent)) {
logSegment.add(new LogEvent(loggingEvent, new LogOffset(LogOffset.INVALID_KAFKA_OFFSET, loggingEvent.getTimeStamp())));
}
currentSyncPosition = dataFileReader.previousSync();
}
return logSegment;
}
Aggregations