use of co.cask.cdap.logging.write.LogLocation 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.write.LogLocation 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();
}
}
use of co.cask.cdap.logging.write.LogLocation in project cdap by caskdata.
the class FileMetadataCleanerTest method testFileMetadataWithCommonContextPrefix.
@Test
public void testFileMetadataWithCommonContextPrefix() throws Exception {
DatasetFramework datasetFramework = injector.getInstance(DatasetFramework.class);
DatasetManager datasetManager = new DefaultDatasetManager(datasetFramework, NamespaceId.SYSTEM, co.cask.cdap.common.service.RetryStrategies.noRetry());
Transactional transactional = Transactions.createTransactionalWithRetry(Transactions.createTransactional(new MultiThreadDatasetCache(new SystemDatasetInstantiator(datasetFramework), injector.getInstance(TransactionSystemClient.class), NamespaceId.SYSTEM, ImmutableMap.<String, String>of(), null, null)), RetryStrategies.retryOnConflict(20, 100));
FileMetaDataWriter fileMetaDataWriter = new FileMetaDataWriter(datasetManager, transactional);
FileMetaDataReader fileMetadataReader = injector.getInstance(FileMetaDataReader.class);
FileMetadataCleaner fileMetadataCleaner = new FileMetadataCleaner(datasetManager, transactional);
try {
List<LogPathIdentifier> logPathIdentifiers = new ArrayList<>();
// this should be able to scan and delete common prefix programs like testFlow1, testFlow10 during clenaup.
for (int i = 1; i <= 20; i++) {
logPathIdentifiers.add(new LogPathIdentifier(NamespaceId.DEFAULT.getNamespace(), "testApp", String.format("testFlow%s", i)));
}
LocationFactory locationFactory = injector.getInstance(LocationFactory.class);
Location location = locationFactory.create(TMP_FOLDER.newFolder().getPath()).append("/logs");
long currentTime = System.currentTimeMillis();
long newCurrentTime = currentTime + 100;
for (int i = 1; i <= 20; i++) {
LogPathIdentifier identifier = logPathIdentifiers.get(i - 1);
for (int j = 0; j < 10; j++) {
fileMetaDataWriter.writeMetaData(identifier, newCurrentTime + j, newCurrentTime + j, location.append("testFileNew" + Integer.toString(j)));
}
}
List<LogLocation> locations;
for (int i = 1; i <= 20; i++) {
locations = fileMetadataReader.listFiles(logPathIdentifiers.get(i - 1), newCurrentTime, newCurrentTime + 10);
// should include files from currentTime (0..9)
Assert.assertEquals(10, locations.size());
}
long tillTime = newCurrentTime + 4;
List<FileMetadataCleaner.DeletedEntry> deleteEntries = fileMetadataCleaner.scanAndGetFilesToDelete(tillTime, TRANSACTION_TIMEOUT);
// 20 context, 5 entries each
Assert.assertEquals(100, deleteEntries.size());
for (int i = 1; i <= 20; i++) {
locations = fileMetadataReader.listFiles(logPathIdentifiers.get(i - 1), newCurrentTime, newCurrentTime + 10);
// should include files from time (5..9)
Assert.assertEquals(5, locations.size());
int startIndex = 5;
for (LogLocation logLocation : locations) {
Assert.assertEquals(String.format("testFileNew%s", startIndex), logLocation.getLocation().getName());
startIndex++;
}
}
} finally {
// cleanup meta
cleanupMetadata(transactional, datasetManager);
}
}
Aggregations