diff --git a/tools.descartes.dql.connector.kieker/src/tools/descartes/dql/connector/kieker/KiekerManager.java b/tools.descartes.dql.connector.kieker/src/tools/descartes/dql/connector/kieker/KiekerManager.java index e2abbf4629c2fedb52fc53e0407ca1315e2a6158..fad1e00ea7f9c65f20c9d22442b3350366c5d9a2 100644 --- a/tools.descartes.dql.connector.kieker/src/tools/descartes/dql/connector/kieker/KiekerManager.java +++ b/tools.descartes.dql.connector.kieker/src/tools/descartes/dql/connector/kieker/KiekerManager.java @@ -1,233 +1,238 @@ -package tools.descartes.dql.connector.kieker; - -import java.io.BufferedReader; -import java.io.File; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.Properties; - -import org.apache.log4j.Logger; - -import kieker.common.configuration.Configuration; -import tools.descartes.dql.connector.kieker.configgenerator.XMLGenerationManager; -import tools.descartes.dql.connector.kieker.filter.KiekerAnalysisController; -import tools.descartes.dql.connector.kieker.structures.Trace; -import tools.descartes.dql.core.engine.util.DQLLogger; -import tools.descartes.dql.models.mapping.mapping.EntityMapping; - - - -/** - * The KiekerManager processes an incoming DQL request according to its contents. - * It produces a Kieker Trace file containing the analysis results needed to answer the DQL Query. - * - * @author Matthias Blohm <st140250@stud.uni-stuttgart.de> - */ -public class KiekerManager { - - Logger log; - - private final EntityMapping request; - private String modelLocation; - private String appDirectory; - private String appLocation; - private String kiekerJarLocation; - private String kiekerConfigLocation; - private String kiekerLocation; - private String traceFileOutputPath; - private String traceFileHostName; - - private Trace analysisResult; - - int monitoringTimeout = 10; - - /** - * The Class must be initialized with an DQL Query that the manager takes control over. - * - * @param request the DQL Query to process with kieker data - */ - public KiekerManager(EntityMapping request){ - this.log = DQLLogger.getLogger(KiekerManager.class.getName()); - this.request = request; - this.modelLocation = request.getModelLocation(); - this.analysisResult = new Trace(); - } - - /** - * By running this method the processing of the query is initiated. - * The method tries to find a properties fiel under the modelLocation given by the query. - * If a properties file is found that specifies a folder already containing valid kieker trace files, analysis is triggered directly. - * Otherwise, tracefiles are searched directly under the path of the model location. - * If still no tracefiles are found, monitoring with kieker is started first for receiving trace files. - */ - public void run(){ - log("Checking location for user config file..."); - if (KiekerHelper.checkPropertiesFileExist(modelLocation)){ - log("Valid user config found, start processing..."); - Properties prop = KiekerHelper.getProperties(modelLocation); - this.traceFileOutputPath= (String) prop.get(KiekerHelper.KIEKER_LOG_DIRECTORY); - if (!KiekerHelper.checkTraceFilesExist(traceFileOutputPath)){ - log("No traceFiles found in specified log directory"); - log("Start monitoring"); - //do monitoring first - loadMonitoringConfig(prop); - startMonitoring(); - log("Finished monitoring"); - if (!KiekerHelper.checkTraceFilesExist(traceFileOutputPath)){ - log("Produced traceFiles cannot be found under "+traceFileOutputPath+", aborting..."); - throw new IllegalStateException( - "Could not find any valid Kieker traceFiles under "+traceFileOutputPath); - } - } - this.traceFileHostName= (String) prop.get(KiekerHelper.KIEKER_LOG_HOSTNAME); - if (this.traceFileHostName == ""){ - this.traceFileHostName = KiekerHelper.getHostName(); - } - log("TraceFiles found under "+traceFileOutputPath); - startAnalysis(); - - } else { - // no user config, try modelLocation as traceFile directory - log("No user config found under "+modelLocation); - log("Search for existent traceFiles instead..."); - if (!KiekerHelper.checkTraceFilesExist(modelLocation)){ - log.error("No traceFiles found under "+modelLocation+", aborting..."); - throw new IllegalStateException( - "Could not find any valid Kieker traceFiles under "+modelLocation); - } else { - log("TraceFiles possibly found under "+modelLocation); - this.traceFileOutputPath = modelLocation; - this.traceFileHostName = KiekerHelper.getHostName(); - log("Try Analysis..."); - startAnalysis(); - } - } - } - - /** - * This method loads the properties found in the modelLocation into KiekerManager for further processing - * It first checks whether all required properties can be found and then tries to parse them correctly. - */ - private void loadMonitoringConfig(Properties prop){ - log("Loading monitoring properties..."); - String[] requiredProperties = new String[]{KiekerHelper.APPLICATION_JAR_FILE,KiekerHelper.KIEKER_JAR_FILE,KiekerHelper.KIEKER_LOCATION,KiekerHelper.KIEKER_CONFIG_DEFAULT_KEY}; - KiekerHelper.checkProperties(prop, requiredProperties); - try{ - - this.appLocation = (String) prop.get(KiekerHelper.APPLICATION_JAR_FILE); - this.kiekerJarLocation = (String) prop.get(KiekerHelper.KIEKER_JAR_FILE); - this.kiekerLocation = (String) prop.get(KiekerHelper.KIEKER_LOCATION); - this.appDirectory = (String) prop.get(KiekerHelper.APPLICATION_DIRECTORY); - this.monitoringTimeout = Integer.valueOf(prop.getProperty(KiekerHelper.MAX_TIMEOUT_KEY)); - this.kiekerConfigLocation = prop.getProperty(KiekerHelper.KIEKER_CONFIG_DEFAULT_KEY); - log("Loading monitoring properties finished"); - } catch(Exception e){ - log("could not parse Properties-file. Aborting..."); - throw new IllegalStateException("Could not parse properties file under "+modelLocation); - } - } - - /** - * By calling this method, monitoring is started. - * If the monitored application does not terminate after the timeout specified in the properties file, - * monitoring is terminated. - * This method triggers both resource and application monitoring. - */ - private void startMonitoring(){ - log("Creating monitoring configurations..."); - KiekerHelper.createNewOutPutPath(traceFileOutputPath); - KiekerHelper.setMonitoringProperties(kiekerConfigLocation, traceFileOutputPath); - - ArrayList<String> services = KiekerHelper.getComponentIdentifiers(request); - // Config Generation - if (!XMLGenerationManager.generateDefaultXML(services, appDirectory+File.separator+"META-INF")) { - log("Warning: Config could not be created"); - } - log("Start monitoring for "+monitoringTimeout +" seconds..."); - Process p; - try { - ResourceMonitor.startResourceMonitor(kiekerLocation,appDirectory); - String command = "java -javaagent:" + kiekerJarLocation + " -jar " + appLocation; - p = Runtime.getRuntime() - .exec(command); - String line; - BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); - /*while ((line = in.readLine()) != null) { - System.out.println(line); - }*/ - in.close(); - } catch (Exception e) { - log(e.getMessage()); - e.printStackTrace(); - throw new IllegalStateException("Could not execute application with kieker"); - } - - // warten bis timeout - try { - Thread.sleep(monitoringTimeout * 1000); - p.destroy(); - ResourceMonitor.stopRessourceMonitor(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - log("Monitoring terminated"); - } - - /** - * By calling this method, analysis is started. - * Therefore a kieker analysis configuration is created based on the request, which is then passed to an instance of KiekerAnalysisController. - */ - private void startAnalysis(){ - log("Starting analysis..."); - try{ - Configuration analysisConfiguration = KiekerHelper.createAnalysisConfig(request, traceFileOutputPath,traceFileHostName); - - KiekerAnalysisController c = new KiekerAnalysisController(analysisConfiguration); - c.fillTrace(); - this.analysisResult = c.getTrace(); - this.analysisResult.sortChronological(); - } catch(Exception e){ - log("Unable to analyze traceFiles!"); - throw new IllegalStateException("Analyzing Kieker traceFiles failed"); - } - log("Analysis finished"); - } - - /** - * Gives back the analysis result as a Kieker Trace containing the relevant data passed through the filters. - * Only call after the run() method has been executed, otherwise the result will be empty. - * - * @return the analysis result as a filtered Kieker Trace - */ - public Trace getAnalysisResult(){ - return this.analysisResult; - } - - /** - * Gives back the timestamp of the first event occuring in the trace files. - * It is used for displaying any following records relatively to the first one. - * This way Pavo's timeline can start at zero. - * - * @return the timestamp of the first captured event in ns - */ - public long getStartingTime(){ - long firstEvent = Long.MAX_VALUE; - long firstResource = Long.MAX_VALUE; - if (this.analysisResult.getEvents() != null && this.analysisResult.getEvents().size()>0){ - firstEvent = this.analysisResult.getEvents().get(0).getTimestamp(); - } - if (this.analysisResult.getResources() != null && this.analysisResult.getResources().size()>0){ - firstResource = this.analysisResult.getResources().get(0).getTimestamp(); - } - return Math.min(firstEvent, firstResource); - } - - /** - * private method for logging events happening in KiekerManager - */ - private void log(String msg){ - log.info(msg); - } - - -} +package tools.descartes.dql.connector.kieker; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Properties; + +import org.apache.log4j.Logger; + +import tools.descartes.dql.connector.kieker.configgenerator.XMLGenerationManager; +import tools.descartes.dql.connector.kieker.filter.KiekerAnalysisController; +import tools.descartes.dql.connector.kieker.structures.Trace; +import tools.descartes.dql.core.engine.util.DQLLogger; +import tools.descartes.dql.models.mapping.mapping.EntityMapping; + + + +/** + * The KiekerManager processes an incoming DQL request according to its contents. + * It produces a Kieker Trace file containing the analysis results needed to answer the DQL Query. + * + * @author Matthias Blohm <st140250@stud.uni-stuttgart.de> + */ +public class KiekerManager { + + Logger log; + + private final EntityMapping request; + private String modelLocation; + private String appDirectory; + private String appLocation; + private String kiekerJarLocation; + private String kiekerConfigLocation; + private String kiekerLocation; + private String traceFileOutputPath; + private String traceFileHostName; + + private Trace analysisResult; + + int monitoringTimeout = 10; + + /** + * The Class must be initialized with an DQL Query that the manager takes control over. + * + * @param request the DQL Query to process with kieker data + */ + public KiekerManager(EntityMapping request){ + this.log = DQLLogger.getLogger(KiekerManager.class.getName()); + this.request = request; + this.modelLocation = request.getModelLocation(); + this.analysisResult = new Trace(); + } + + /** + * By running this method the processing of the query is initiated. + * The method tries to find a properties fiel under the modelLocation given by the query. + * If a properties file is found that specifies a folder already containing valid kieker trace files, analysis is triggered directly. + * Otherwise, tracefiles are searched directly under the path of the model location. + * If still no tracefiles are found, monitoring with kieker is started first for receiving trace files. + */ + public void run(){ + log("Checking location for user config file..."); + if (KiekerHelper.checkPropertiesFileExist(modelLocation)){ + log("Valid user config found, start processing..."); + Properties prop = KiekerHelper.getProperties(modelLocation); + this.traceFileOutputPath= (String) prop.get(KiekerHelper.KIEKER_LOG_DIRECTORY); + traceFileOutputPath = traceFileOutputPath.replace("%20", " "); // fixes + // space + // in + // path + // problem + + if (!KiekerHelper.checkTraceFilesExist(traceFileOutputPath)){ + log("No traceFiles found in specified log directory"); + log("Start monitoring"); + //do monitoring first + loadMonitoringConfig(prop); + startMonitoring(); + log("Finished monitoring"); + if (!KiekerHelper.checkTraceFilesExist(traceFileOutputPath)){ + log("Produced traceFiles cannot be found under "+traceFileOutputPath+", aborting..."); + throw new IllegalStateException( + "Could not find any valid Kieker traceFiles under "+traceFileOutputPath); + } + } + this.traceFileHostName= (String) prop.get(KiekerHelper.KIEKER_LOG_HOSTNAME); + if (this.traceFileHostName == ""){ + this.traceFileHostName = KiekerHelper.getHostName(); + } + log("TraceFiles found under "+traceFileOutputPath); + startAnalysis(); + + } else { + // no user config, try modelLocation as traceFile directory + log("No user config found under "+modelLocation); + log("Search for existent traceFiles instead..."); + if (!KiekerHelper.checkTraceFilesExist(modelLocation)){ + log.error("No traceFiles found under "+modelLocation+", aborting..."); + throw new IllegalStateException( + "Could not find any valid Kieker traceFiles under "+modelLocation); + } else { + log("TraceFiles possibly found under "+modelLocation); + this.traceFileOutputPath = modelLocation; + this.traceFileHostName = KiekerHelper.getHostName(); + log("Try Analysis..."); + startAnalysis(); + } + } + } + + /** + * This method loads the properties found in the modelLocation into KiekerManager for further processing + * It first checks whether all required properties can be found and then tries to parse them correctly. + */ + private void loadMonitoringConfig(Properties prop){ + log("Loading monitoring properties..."); + String[] requiredProperties = new String[]{KiekerHelper.APPLICATION_JAR_FILE,KiekerHelper.KIEKER_JAR_FILE,KiekerHelper.KIEKER_LOCATION,KiekerHelper.KIEKER_CONFIG_DEFAULT_KEY}; + KiekerHelper.checkProperties(prop, requiredProperties); + try{ + + this.appLocation = (String) prop.get(KiekerHelper.APPLICATION_JAR_FILE); + this.kiekerJarLocation = (String) prop.get(KiekerHelper.KIEKER_JAR_FILE); + this.kiekerLocation = (String) prop.get(KiekerHelper.KIEKER_LOCATION); + this.appDirectory = (String) prop.get(KiekerHelper.APPLICATION_DIRECTORY); + this.monitoringTimeout = Integer.valueOf(prop.getProperty(KiekerHelper.MAX_TIMEOUT_KEY)); + this.kiekerConfigLocation = prop.getProperty(KiekerHelper.KIEKER_CONFIG_DEFAULT_KEY); + log("Loading monitoring properties finished"); + } catch(Exception e){ + log("could not parse Properties-file. Aborting..."); + throw new IllegalStateException("Could not parse properties file under "+modelLocation); + } + } + + /** + * By calling this method, monitoring is started. + * If the monitored application does not terminate after the timeout specified in the properties file, + * monitoring is terminated. + * This method triggers both resource and application monitoring. + */ + private void startMonitoring(){ + log("Creating monitoring configurations..."); + KiekerHelper.createNewOutPutPath(traceFileOutputPath); + KiekerHelper.setMonitoringProperties(kiekerConfigLocation, traceFileOutputPath); + + ArrayList<String> services = KiekerHelper.getComponentIdentifiers(request); + // Config Generation + if (!XMLGenerationManager.generateDefaultXML(services, appDirectory+File.separator+"META-INF")) { + log("Warning: Config could not be created"); + } + log("Start monitoring for "+monitoringTimeout +" seconds..."); + Process p; + try { + ResourceMonitor.startResourceMonitor(kiekerLocation,appDirectory); + String command = "java -javaagent:" + kiekerJarLocation + " -jar " + appLocation; + p = Runtime.getRuntime() + .exec(command); + String line; + BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); + /*while ((line = in.readLine()) != null) { + System.out.println(line); + }*/ + in.close(); + } catch (Exception e) { + log(e.getMessage()); + e.printStackTrace(); + throw new IllegalStateException("Could not execute application with kieker"); + } + + // warten bis timeout + try { + Thread.sleep(monitoringTimeout * 1000); + p.destroy(); + ResourceMonitor.stopRessourceMonitor(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + log("Monitoring terminated"); + } + + /** + * By calling this method, analysis is started. + * Therefore a kieker analysis configuration is created based on the request, which is then passed to an instance of KiekerAnalysisController. + */ + private void startAnalysis(){ + log("Starting analysis..."); + try{ + Configuration analysisConfiguration = KiekerHelper.createAnalysisConfig(request, traceFileOutputPath,traceFileHostName); + + KiekerAnalysisController c = new KiekerAnalysisController(analysisConfiguration); + c.fillTrace(); + this.analysisResult = c.getTrace(); + this.analysisResult.sortChronological(); + } catch(Exception e){ + log("Unable to analyze traceFiles!"); + throw new IllegalStateException("Analyzing Kieker traceFiles failed"); + } + log("Analysis finished"); + } + + /** + * Gives back the analysis result as a Kieker Trace containing the relevant data passed through the filters. + * Only call after the run() method has been executed, otherwise the result will be empty. + * + * @return the analysis result as a filtered Kieker Trace + */ + public Trace getAnalysisResult(){ + return this.analysisResult; + } + + /** + * Gives back the timestamp of the first event occuring in the trace files. + * It is used for displaying any following records relatively to the first one. + * This way Pavo's timeline can start at zero. + * + * @return the timestamp of the first captured event in ns + */ + public long getStartingTime(){ + long firstEvent = Long.MAX_VALUE; + long firstResource = Long.MAX_VALUE; + if (this.analysisResult.getEvents() != null && this.analysisResult.getEvents().size()>0){ + firstEvent = this.analysisResult.getEvents().get(0).getTimestamp(); + } + if (this.analysisResult.getResources() != null && this.analysisResult.getResources().size()>0){ + firstResource = this.analysisResult.getResources().get(0).getTimestamp(); + } + return Math.min(firstEvent, firstResource); + } + + /** + * private method for logging events happening in KiekerManager + */ + private void log(String msg){ + log.info(msg); + } + + +}