use of co.cask.cdap.logging.framework.LocalAppenderContext in project cdap by caskdata.
the class RollingLocationLogAppenderTest method testRollOver.
@Test
public void testRollOver() throws Exception {
// assume SLF4J is bound to logback in the current environment
AppenderContext appenderContext = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(appenderContext);
// Call context.reset() to clear any previous configuration, e.g. default
// configuration. For multi-step configuration, omit calling context.reset().
appenderContext.reset();
configurator.doConfigure(getClass().getResourceAsStream("/rolling-appender-logback-test.xml"));
StatusPrinter.printInCaseOfErrorsOrWarnings(appenderContext);
RollingLocationLogAppender rollingAppender = (RollingLocationLogAppender) appenderContext.getLogger(RollingLocationLogAppenderTest.class).getAppender("rollingAppender");
addTagsToMdc("testNs", "testApp");
Logger logger = appenderContext.getLogger(RollingLocationLogAppenderTest.class);
ingestLogs(logger, 20000);
Map<LocationIdentifier, LocationOutputStream> activeFiles = rollingAppender.getLocationManager().getActiveLocations();
Assert.assertEquals(1, activeFiles.size());
LocationOutputStream locationOutputStream = activeFiles.get(new LocationIdentifier("testNs", "testApp"));
Location parentDir = Locations.getParent(locationOutputStream.getLocation());
Assert.assertEquals(10, parentDir.list().size());
// different program should go to different directory
addTagsToMdc("testNs", "testApp1");
ingestLogs(logger, 20000);
activeFiles = rollingAppender.getLocationManager().getActiveLocations();
Assert.assertEquals(2, activeFiles.size());
locationOutputStream = activeFiles.get(new LocationIdentifier("testNs", "testApp1"));
parentDir = Locations.getParent(locationOutputStream.getLocation());
Assert.assertEquals(10, parentDir.list().size());
// different program should go to different directory because namespace is different
addTagsToMdc("testNs1", "testApp1");
ingestLogs(logger, 20000);
activeFiles = rollingAppender.getLocationManager().getActiveLocations();
Assert.assertEquals(3, activeFiles.size());
locationOutputStream = activeFiles.get(new LocationIdentifier("testNs1", "testApp1"));
parentDir = Locations.getParent(locationOutputStream.getLocation());
Assert.assertEquals(10, parentDir.list().size());
}
use of co.cask.cdap.logging.framework.LocalAppenderContext in project cdap by caskdata.
the class CDAPLogAppenderTest method testCDAPLogAppenderSizeBasedRotation.
@Test
public void testCDAPLogAppenderSizeBasedRotation() throws Exception {
int syncInterval = 1024 * 1024;
FileMetaDataReader fileMetaDataReader = injector.getInstance(FileMetaDataReader.class);
CDAPLogAppender cdapLogAppender = new CDAPLogAppender();
AppenderContext context = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
context.start();
cdapLogAppender.setSyncIntervalBytes(syncInterval);
cdapLogAppender.setMaxFileLifetimeMs(TimeUnit.DAYS.toMillis(1));
cdapLogAppender.setMaxFileSizeInBytes(500);
cdapLogAppender.setDirPermissions("750");
cdapLogAppender.setFilePermissions("640");
cdapLogAppender.setFileRetentionDurationDays(1);
cdapLogAppender.setLogCleanupIntervalMins(10);
cdapLogAppender.setFileCleanupTransactionTimeout(30);
cdapLogAppender.setContext(context);
cdapLogAppender.start();
Map<String, String> properties = new HashMap<>();
properties.put(NamespaceLoggingContext.TAG_NAMESPACE_ID, "testSizeRotation");
properties.put(ApplicationLoggingContext.TAG_APPLICATION_ID, "testApp");
properties.put(FlowletLoggingContext.TAG_FLOW_ID, "testFlow");
properties.put(FlowletLoggingContext.TAG_FLOWLET_ID, "testFlowlet");
long currentTimeMillisEvent1 = System.currentTimeMillis();
LoggingEvent event1 = getLoggingEvent("co.cask.Test1", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message 1", properties);
event1.setTimeStamp(currentTimeMillisEvent1);
cdapLogAppender.doAppend(event1);
// sync updates the file size
cdapLogAppender.sync();
long currentTimeMillisEvent2 = System.currentTimeMillis();
LoggingEvent event2 = getLoggingEvent("co.cask.Test2", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message 2", properties);
event2.setTimeStamp(currentTimeMillisEvent2);
// one new append, we will rotate to new file as the file size limit is very low and last append exceeded that.
cdapLogAppender.doAppend(event2);
cdapLogAppender.stop();
context.stop();
try {
List<LogLocation> files = fileMetaDataReader.listFiles(cdapLogAppender.getLoggingPath(properties), 0, Long.MAX_VALUE);
Assert.assertEquals(2, files.size());
assertLogEventDetails(event1, files.get(0));
assertLogEventDetails(event2, files.get(1));
Assert.assertEquals(currentTimeMillisEvent1, files.get(0).getEventTimeMs());
Assert.assertEquals(currentTimeMillisEvent2, files.get(1).getEventTimeMs());
Assert.assertTrue(files.get(0).getFileCreationTimeMs() >= currentTimeMillisEvent1);
Assert.assertTrue(files.get(1).getFileCreationTimeMs() >= currentTimeMillisEvent2);
} catch (Exception e) {
Assert.fail();
}
}
use of co.cask.cdap.logging.framework.LocalAppenderContext in project cdap by caskdata.
the class CDAPLogAppenderTest method testCDAPLogAppenderRotation.
@Test
public void testCDAPLogAppenderRotation() throws Exception {
int syncInterval = 1024 * 1024;
FileMetaDataReader fileMetaDataReader = injector.getInstance(FileMetaDataReader.class);
CDAPLogAppender cdapLogAppender = new CDAPLogAppender();
AppenderContext context = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
context.start();
cdapLogAppender.setSyncIntervalBytes(syncInterval);
cdapLogAppender.setMaxFileLifetimeMs(500);
cdapLogAppender.setMaxFileSizeInBytes(104857600);
cdapLogAppender.setDirPermissions("750");
cdapLogAppender.setFilePermissions("640");
cdapLogAppender.setFileRetentionDurationDays(1);
cdapLogAppender.setLogCleanupIntervalMins(10);
cdapLogAppender.setFileCleanupTransactionTimeout(30);
cdapLogAppender.setContext(context);
cdapLogAppender.start();
Map<String, String> properties = new HashMap<>();
properties.put(NamespaceLoggingContext.TAG_NAMESPACE_ID, "testTimeRotation");
properties.put(ApplicationLoggingContext.TAG_APPLICATION_ID, "testApp");
properties.put(FlowletLoggingContext.TAG_FLOW_ID, "testFlow");
properties.put(FlowletLoggingContext.TAG_FLOWLET_ID, "testFlowlet");
long currentTimeMillisEvent1 = System.currentTimeMillis();
LoggingEvent event1 = getLoggingEvent("co.cask.Test1", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message 1", properties);
event1.setTimeStamp(currentTimeMillisEvent1);
cdapLogAppender.doAppend(event1);
// Pause pass the max file lifetime ms
TimeUnit.MILLISECONDS.sleep(500);
long currentTimeMillisEvent2 = System.currentTimeMillis();
LoggingEvent event2 = getLoggingEvent("co.cask.Test2", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), Level.ERROR, "test message 2", properties);
event2.setTimeStamp(currentTimeMillisEvent1 + 1000);
cdapLogAppender.doAppend(event2);
cdapLogAppender.stop();
context.stop();
try {
List<LogLocation> files = fileMetaDataReader.listFiles(cdapLogAppender.getLoggingPath(properties), 0, Long.MAX_VALUE);
Assert.assertEquals(2, files.size());
assertLogEventDetails(event1, files.get(0));
assertLogEventDetails(event2, files.get(1));
Assert.assertEquals(currentTimeMillisEvent1, files.get(0).getEventTimeMs());
Assert.assertEquals(currentTimeMillisEvent1 + 1000, files.get(1).getEventTimeMs());
Assert.assertTrue(files.get(0).getFileCreationTimeMs() >= currentTimeMillisEvent1);
Assert.assertTrue(files.get(1).getFileCreationTimeMs() >= currentTimeMillisEvent2);
// checking permission
String expectedPermissions = "rw-r-----";
for (LogLocation file : files) {
Location location = file.getLocation();
Assert.assertEquals(expectedPermissions, location.getPermissions());
}
} catch (Exception e) {
Assert.fail();
}
}
Aggregations