Skip to content

Commit

Permalink
improve logging fixed #580 (#581)
Browse files Browse the repository at this point in the history
* refactored/extracted bash util

* -> we should print config file path, which is started as log.info
-> we see in log file on server which commit is used

* Feedback incorporated

* Feedback incorporated and warnings fixed

* Feedback incorporated
  • Loading branch information
zishanbilal committed Sep 18, 2018
1 parent d8a4f43 commit ad8d892
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 56 deletions.
37 changes: 0 additions & 37 deletions src/main/java/beam/utils/LoggingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,4 @@ public static void createFileLogger(String outputDirectory) {
rootLogger.addAppender(fileAppender);
rootLogger.setAdditive(true); /* set to true if root should log too */
}

/**
* Method returns the git commit hash or HEAD if git not present
* git rev-parse --abbrev-ref HEAD
*
* @return returns the git commit hash or HEAD if git not present
*/
public static String getCommitHash() {
String resp = readCommandResponse("git rev-parse HEAD");
if (resp != null) return resp;
return "HEAD"; //for the env where git is not present
}

/**
* Method returns the git branch or master if git not present
*
* @return returns the current git branch
*/
public static String getBranch() {
String resp = readCommandResponse("git rev-parse --abbrev-ref HEAD");
if (resp != null) return resp;
return "master"; //for the env where git is not present
}

private static String readCommandResponse(String command) {
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(command);
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream())
)) {
return reader.readLine();
}
} catch (Exception e) {
return null; //for the env where command is not recognized
}
}
}
39 changes: 20 additions & 19 deletions src/main/scala/beam/sim/BeamHelper.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package beam.sim

import java.io.FileOutputStream
import java.nio.file.{Files, Paths}
import java.util
import java.nio.file.{Files, Paths, StandardCopyOption}
import java.util.Properties

import beam.agentsim.agents.ridehail.RideHailSurgePricingManager
import beam.agentsim.events.handling.BeamEventsHandling
import org.matsim.core.api.experimental.events.EventsManager
import beam.analysis.plots.{GraphSurgePricing, RideHailRevenueAnalysis}
import beam.replanning._
import beam.replanning.utilitybased.UtilityBasedModeChoice
Expand All @@ -17,16 +15,17 @@ import beam.sim.config.{BeamConfig, ConfigModule, MatSimBeamConfigBuilder}
import beam.sim.metrics.Metrics._
import beam.sim.modules.{BeamAgentModule, UtilsModule}
import beam.utils.reflection.ReflectionUtils
import beam.utils.{BeamConfigUtils, FileUtils, LoggingUtil}
import beam.utils.{BashUtils, BeamConfigUtils, FileUtils, LoggingUtil}
import com.conveyal.r5.streets.StreetLayer
import com.conveyal.r5.transit.TransportNetwork
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.typesafe.config.{Config => TypesafeConfig, ConfigFactory}
import com.typesafe.config.{ConfigFactory, ConfigRenderOptions, Config => TypesafeConfig}
import com.typesafe.scalalogging.LazyLogging
import kamon.Kamon
import org.matsim.api.core.v01.population.Person
import org.matsim.api.core.v01.{Id, Scenario}
import org.matsim.core.api.experimental.events.EventsManager
import org.matsim.core.config.Config
import org.matsim.core.config.groups.TravelTimeCalculatorConfigGroup
import org.matsim.core.controler._
Expand Down Expand Up @@ -225,9 +224,9 @@ trait BeamHelper extends LazyLogging {
}
)

def runBeamUsing(args: Array[String], isConfigArgRequired: Boolean = true) = {
def runBeamUsing(args: Array[String], isConfigArgRequired: Boolean = true): Unit = {
val parsedArgs = argsParser.parse(args, init = Arguments()) match {
case Some(parsedArgs) => parsedArgs
case Some(pArgs) => pArgs
case None =>
throw new IllegalArgumentException(
"Arguments provided were unable to be parsed. See above for reasoning."
Expand All @@ -249,7 +248,7 @@ trait BeamHelper extends LazyLogging {
case _ =>
val (_, outputDirectory) = runBeamWithConfig(config)
val props = new Properties()
props.setProperty("commitHash", LoggingUtil.getCommitHash)
props.setProperty("commitHash", BashUtils.getCommitHash)
props.setProperty("configFile", configLocation)
val out = new FileOutputStream(Paths.get(outputDirectory, "beam.properties").toFile)
props.store(out, "Simulation out put props.")
Expand All @@ -267,11 +266,11 @@ trait BeamHelper extends LazyLogging {
)
)
}
Files.copy(Paths.get(configLocation), Paths.get(outputDirectory, "beam.conf"))
Files.copy(Paths.get(configLocation), Paths.get(outputDirectory, "beam.conf"), StandardCopyOption.REPLACE_EXISTING)
}
}

def runClusterWorkerUsing(config: TypesafeConfig) = {
def runClusterWorkerUsing(config: TypesafeConfig): Unit = {
val clusterConfig = ConfigFactory
.parseString(s"""
|akka.cluster.roles = [compute]
Expand All @@ -292,12 +291,7 @@ trait BeamHelper extends LazyLogging {
if (isMetricsEnable) Kamon.start(clusterConfig.withFallback(ConfigFactory.defaultReference()))

import akka.actor.{ActorSystem, DeadLetter, PoisonPill, Props}
import akka.cluster.singleton.{
ClusterSingletonManager,
ClusterSingletonManagerSettings,
ClusterSingletonProxy,
ClusterSingletonProxySettings
}
import akka.cluster.singleton.{ClusterSingletonManager, ClusterSingletonManagerSettings, ClusterSingletonProxy, ClusterSingletonProxySettings}
import beam.router.ClusterWorkerRouter
import beam.sim.monitoring.DeadLetterReplayer

Expand Down Expand Up @@ -344,10 +338,16 @@ trait BeamHelper extends LazyLogging {
beamConfig.beam.agentsim.simulationName,
beamConfig.beam.outputs.addTimestampToOutputDirectory
)

LoggingUtil.createFileLogger(outputDirectory)
matsimConfig.controler.setOutputDirectory(outputDirectory)
matsimConfig.controler().setWritePlansInterval(beamConfig.beam.outputs.writePlansInterval)

logger.info(s"Starting beam on branch {} at commit {}.", BashUtils.getBranch, BashUtils.getCommitHash)
val outConf = Paths.get(outputDirectory, "beam.conf")
Files.write(outConf, config.root().render(ConfigRenderOptions.concise()).getBytes)
logger.info(s"Config [{}] copied to {}.", beamConfig.beam.agentsim.simulationName, outConf)

val networkCoordinator = new NetworkCoordinator(beamConfig)
networkCoordinator.loadNetwork()

Expand Down Expand Up @@ -414,6 +414,7 @@ trait BeamHelper extends LazyLogging {
}
}


}

case class MapStringDouble(data: Map[String, Double])
Expand All @@ -426,13 +427,13 @@ case class Arguments(
seedAddress: Option[String] = None,
useLocalWorker: Option[Boolean] = None
) {
val useCluster = clusterType.isDefined
val useCluster: Boolean = clusterType.isDefined
}

sealed trait ClusterType
case object Master extends ClusterType {
override def toString() = "master"
override def toString = "master"
}
case object Worker extends ClusterType {
override def toString() = "worker"
override def toString = "worker"
}
47 changes: 47 additions & 0 deletions src/main/scala/beam/utils/BashUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package beam.utils

import java.io.{BufferedReader, InputStreamReader}

object BashUtils {

/**
* Method returns the git commit hash or HEAD if git not present
* git rev-parse --abbrev-ref HEAD
*
* @return returns the git commit hash or HEAD if git not present
*/
def getCommitHash: String = {
val resp = readCommandResponse("git rev-parse HEAD")
if (resp != null) return resp
"HEAD" //for the env where git is not present

}

/**
* Method returns the git branch or master if git not present
*
* @return returns the current git branch
*/
def getBranch: String = {
val resp = readCommandResponse("git rev-parse --abbrev-ref HEAD")
if (resp != null) return resp
"master"
}

private def readCommandResponse(command: String) = {
val runtime = Runtime.getRuntime
try {
val process = runtime.exec(command)
try {
val reader = new BufferedReader(new InputStreamReader(process.getInputStream))
try
reader.readLine
finally if (reader != null) reader.close()
}
} catch {
case _: Exception =>
null //for the env where command is not recognized

}
}
}

0 comments on commit ad8d892

Please sign in to comment.