Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

WIP

Use Case 

Users would like to have custom logic in filtering log messages at application level and write the log messages to a log files in HDFS. 

Design 

Introduce Log Processor interface and FileWriter interfaces Pluggable in CDAP Logsaver. 

API Design

Step1: LogProcessor

 

Code Block
public interface LogProcessor {

  /**
   * Called during initialize, passed properties for log processor
   * @param properties
   */
  void initialize(Properties properties);

  /**
   * Process method will be called with iterator of log messages, log messages received will be in sorted order,
   * sorted by timestamp. This method should not throw any exception, if any unchecked exceptions are thrown,
   * log.saver will log an error and the processor will not receive messages.
   * Will start receiving messages on log.saver startup
   * 
   * @param events list of {@link LogEvent}
   */
  void process(Iterator<LogEvent> events);

  /**
   * stop logprocessor
   */
  void destroy();
}
Code Block
class LogEvent {
  ILoggingEvent iLoggingEvent;
  EntityId entityId;
}

 

Step2: FileWriter

Currently we only have AvroFileWriter in Log.saver, we can create an interface for users to configure the FileWriter to provide if needed. This provides the option to abstract certain common logic for file rotation,etc into an AbstractFileWriter and custom file writer can implement the other methods, example : writing to HDFS text file, etc.

 

Code Block
public interface FileWriter {
  append(Iterator<ILoggingEvent> events)
  rotateFile(File file, EntityId entityId, long timestamp)
  getFile(EntityId entityId, long timestamp)
  close(File file, long timestamp)
  closeAndDelete(File file)
  flush()
}

Code Block
public abstract class AbstractFileWriter implements FileWriter {
	
	public File rotateFile(File file, EntityId entityId, long timestamp) {
  		// common-logic for rotating files
	}
	public getFile(EntityId entityId, long timestamp) {
		// common-logic for getting previously files
	}
}

 

Option-1

Plugins run in the same container as log.saver. 

Lifecycle

1) Log saver will load and initialize the log processor plugin.

2) As Log saver processes the messages, the log processor's process will also be called with logging events.

3) on plugin error, we will stop the plugin. optionally if log processor extension throws an error :

  • we can Stop the plugin (or) 
  • we can log an error and continue and stop the plugin after

...

  • an error threshold.

4) stop the plugin when log.saver stops.

 

Class-Loading Isolation

1) Should the log processor plugins have separate classloaders class-loaders (or) can they share the same classloader and ClassLoader as the log.saver system

     Having isolation helps with plugins to depend on different libraries, but should we allow them ? Its not clear. 

2) If we create separate Class loader - we need to expose the following 

  • cdap-watchdog-api
  • cdap-proto
  • hadoop api 
  • classes exposed by CDAP system class loader ?

 

...

  • logback-classic ( we need ILoggingEvent)
  • should we expose more classes ? 
  • What if user wants to write to a kafka server or third-party storage s3 on the log.processor logic? Having separate class loader will help in these scenarios.

 

Sample Custom Log Plugin Implementation 

1) Log Processor would want to process the ILoggingEvent, format it to a log message string (maybe using log-back layout) and write it to a destination in HDFS.

2) However the configuration for this log.processor cannot be a logback.xml.

...

3) the configuration for logging location (base directory in hdfs) and logging class to use (SizeBasedRolling, etc) could be provided through cdap-site.xml for the extensions. These properties would be passed to the extension during initialize.

4) We have to extract logic performed in Log.saver for managing file size,etc and reuse them in extensionsLog processor extension could provide an implementation of FileWriter interface (or extension of AbstractFileWriter) for HDFSFileWriter logic for the events it has processed using LogProcessor

4) Future implementation for other policies have to be handled implemented at the end of extensions and configured through cdap-site.xml 

 

Pros

1) Leverages scalability of log.saver

2) Utilization of existing resources , logic and processing.  Leveraging sorted messaging capability of log.saver for plugins.

3) Makes log saver extendable, to have option to store in different format and have custom logic on filtering.

 

Cons

1) As number of extensions increases and if a processor extension is slow, this could cause performance of log.saver to drop, which will affect the CDAP log.saver performance

 

Option-2 (or) Improvement on Option-1:

 

Configure and Run a separate container for every log.processor plugin. 

...