Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ledger-api-bench-tool: Fix flaky MetricsCollectorSpec #11750

Merged
merged 7 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make MetricsCollector communicate via messages only, not by logging
  • Loading branch information
kamil-da committed Nov 17, 2021
commit d52644760a75c36bc6646e7c29f71e9cbe548584
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package com.daml.ledger.api.benchtool
import com.daml.ledger.api.benchtool.config.WorkflowConfig.StreamConfig
import com.daml.ledger.api.benchtool.metrics.{
MetricRegistryOwner,
MetricsCollector,
MetricsSet,
StreamMetrics,
StreamResult,
}
import com.daml.ledger.api.benchtool.services.LedgerApiServices
import com.daml.ledger.api.benchtool.util.TypedActorSystemResourceOwner
Expand Down Expand Up @@ -110,7 +110,7 @@ object Benchmark {
}
.transform {
case Success(results) =>
if (results.contains(MetricsCollector.Message.MetricsResult.ObjectivesViolated))
if (results.contains(StreamResult.ObjectivesViolated))
Failure(new RuntimeException("Metrics objectives not met."))
else Success(())
case Failure(ex) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class MeteredStreamObserver[T](
val streamName: String,
logger: Logger,
manager: MetricsManager[T],
) extends ObserverWithResult[T, MetricsCollector.Message.MetricsResult](logger) {
) extends ObserverWithResult[T, StreamResult](logger) {

override def onNext(value: T): Unit = {
Thread.sleep(1000)
manager.sendNewValue(value)
super.onNext(value)
}

override def completeWith(): Future[MetricsCollector.Message.MetricsResult] = {
override def completeWith(): Future[StreamResult] = {
logger.debug(withStreamName(s"Asking for stream result..."))
manager.result()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,50 @@

package com.daml.ledger.api.benchtool.metrics

import akka.actor.typed.scaladsl.{Behaviors, TimerScheduler}
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ActorRef, Behavior}
import com.daml.ledger.api.benchtool.util.{MetricFormatter, TimeUtil}
import com.daml.ledger.api.benchtool.metrics.objectives.ServiceLevelObjective
import com.daml.ledger.api.benchtool.util.TimeUtil

import java.time.Instant
import scala.concurrent.duration._

object MetricsCollector {

sealed trait Message
object Message {
sealed trait MetricsResult
object MetricsResult {
final case object Ok extends MetricsResult
final case object ObjectivesViolated extends MetricsResult
}
final case class NewValue[T](value: T) extends Message
final case class PeriodicUpdateCommand() extends Message
final case class StreamCompleted(replyTo: ActorRef[MetricsResult]) extends Message
final case class PeriodicReportRequest(replyTo: ActorRef[Response.PeriodicReport])
extends Message
final case class FinalReportRequest(replyTo: ActorRef[Response.FinalReport]) extends Message
}

sealed trait Response
object Response {
final case class PeriodicReport(values: List[MetricValue]) extends Response
final case class MetricFinalReportData(
name: String,
value: MetricValue,
violatedObjective: Option[(ServiceLevelObjective[_], MetricValue)],
)
final case class FinalReport(metricsData: List[MetricFinalReportData]) extends Response
}

def apply[T](
streamName: String,
metrics: List[Metric[T]],
logInterval: FiniteDuration,
reporter: MetricFormatter,
exposedMetrics: Option[ExposedMetrics[T]] = None,
): Behavior[Message] =
Behaviors.withTimers { timers =>
val startTime: Instant = Instant.now()
new MetricsCollector[T](timers, streamName, logInterval, reporter, startTime, exposedMetrics)
.handlingMessages(metrics, startTime)
}

): Behavior[Message] = {
val startTime: Instant = Instant.now()
new MetricsCollector[T](startTime, exposedMetrics).handlingMessages(metrics, startTime)
}
}

class MetricsCollector[T](
timers: TimerScheduler[MetricsCollector.Message],
streamName: String,
logInterval: FiniteDuration,
reporter: MetricFormatter,
startTime: Instant,
exposedMetrics: Option[ExposedMetrics[T]],
) {
import MetricsCollector._
import MetricsCollector.Message._

timers.startTimerWithFixedDelay(PeriodicUpdateCommand(), logInterval)
import MetricsCollector.Response._

def handlingMessages(metrics: List[Metric[T]], lastPeriodicCheck: Instant): Behavior[Message] = {
Behaviors.receive { case (context, message) =>
Expand All @@ -59,39 +55,29 @@ class MetricsCollector[T](
exposedMetrics.foreach(_.onNext(newValue.value))
handlingMessages(metrics.map(_.onNext(newValue.value)), lastPeriodicCheck)

case _: PeriodicUpdateCommand =>
case request: PeriodicReportRequest =>
val currentTime = Instant.now()
val (newMetrics, values) = metrics
.map(_.periodicValue(TimeUtil.durationBetween(lastPeriodicCheck, currentTime)))
.unzip
context.log.info(namedMessage(reporter.formattedValues(values)))
context.log.info(s"RETURNING VALUES")
request.replyTo ! Response.PeriodicReport(values)
handlingMessages(newMetrics, currentTime)

case message: StreamCompleted =>
context.log.info(
namedMessage(
reporter.finalReport(
streamName = streamName,
metrics = metrics,
duration = totalDuration,
case request: FinalReportRequest =>
val duration = TimeUtil.durationBetween(startTime, Instant.now())
val data: List[MetricFinalReportData] =
metrics.map { metric =>
MetricFinalReportData(
name = metric.name,
value = metric.finalValue(duration),
violatedObjective = metric.violatedObjective,
)
)
)
message.replyTo ! result(metrics)
}
context.log.info(s"RETURNING FINAL DATA")
request.replyTo ! FinalReport(data)
Behaviors.stopped
}
}
}

private def result(metrics: List[Metric[T]]): MetricsResult = {
val atLeastOneObjectiveViolated = metrics.exists(_.violatedObjective.nonEmpty)

if (atLeastOneObjectiveViolated) MetricsResult.ObjectivesViolated
else MetricsResult.Ok
}

private def namedMessage(message: String) = s"[$streamName] $message"

private def totalDuration: java.time.Duration =
TimeUtil.durationBetween(startTime, Instant.now())
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import akka.actor.CoordinatedShutdown
import akka.actor.typed.scaladsl.AskPattern._
import akka.actor.typed.{ActorRef, ActorSystem, Props, SpawnProtocol}
import akka.util.Timeout
import com.daml.ledger.api.benchtool.util.MetricFormatter

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}

case class MetricsManager[T](collector: ActorRef[MetricsCollector.Message])(implicit
case class MetricsManager[T](
collector: ActorRef[MetricsCollector.Message],
logInterval: FiniteDuration,
)(implicit
system: ActorSystem[SpawnProtocol.Command]
) {
CoordinatedShutdown(system).addTask(
Expand All @@ -23,12 +25,30 @@ case class MetricsManager[T](collector: ActorRef[MetricsCollector.Message])(impl
result().map(_ => akka.Done)(system.executionContext)
}

system.scheduler.scheduleWithFixedDelay(logInterval, logInterval)(() => {
implicit val timeout: Timeout = Timeout(logInterval)
collector
.ask(MetricsCollector.Message.PeriodicReportRequest)
.map { response =>
println(s"LOG NICE PERIODIC REPORT: ${response}")
}(system.executionContext)
()
})(system.executionContext)

def sendNewValue(value: T): Unit =
collector ! MetricsCollector.Message.NewValue(value)

def result[Result](): Future[MetricsCollector.Message.MetricsResult] = {
def result[Result](): Future[StreamResult] = {
implicit val timeout: Timeout = Timeout(3.seconds)
collector.ask(MetricsCollector.Message.StreamCompleted)
val result = collector.ask(MetricsCollector.Message.FinalReportRequest)

result.map { response: MetricsCollector.Response.FinalReport =>
println(s"PRINTLN NICE REPORT: ${response}")
if (response.metricsData.exists(_.violatedObjective.isDefined))
StreamResult.ObjectivesViolated
else
StreamResult.Ok
}(system.executionContext)
}
}

Expand All @@ -47,10 +67,7 @@ object MetricsManager {
val collectorActor: Future[ActorRef[MetricsCollector.Message]] = system.ask(
SpawnProtocol.Spawn(
behavior = MetricsCollector(
streamName = streamName,
metrics = metrics,
logInterval = logInterval,
reporter = MetricFormatter.Default,
exposedMetrics = exposedMetrics,
),
name = s"${streamName}-collector",
Expand All @@ -59,6 +76,11 @@ object MetricsManager {
)
)

collectorActor.map(MetricsManager[StreamElem](_))
collectorActor.map(collector =>
MetricsManager[StreamElem](
collector = collector,
logInterval = logInterval,
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.api.benchtool.metrics

sealed trait StreamResult extends Product with Serializable

object StreamResult {
final case object Ok extends StreamResult
final case object ObjectivesViolated extends StreamResult
}
Loading