package com.twitter.finagle.serverset2.client

import com.twitter.finagle.stats.StatsReceiver
import com.twitter.util.{Closable, Duration, Future, Time, Timer}

class StateTracker(
  statsReceiver: StatsReceiver,
  samplePeriod: Duration,
  timer: Timer
) extends Closable {

  private[this] var currState: Option[SessionState] = None
  private[this] var lastSample: Time = Time.now

  private[this] val timerTask = timer.schedule(Time.now + samplePeriod, samplePeriod) {
    sample()
  }

  def close(deadline: Time): Future[Unit] = {
    timerTask.close(deadline)
  }

  def transition(newState: SessionState): Unit = synchronized {
    sample()
    currState = Some(newState)
  }

  private[this] def sample(): Unit = synchronized {
    val now = Time.now
    val delta = now - lastSample
    lastSample = now
    currState foreach { state =>
      statsReceiver.counter(s"${state.name}_duration_ms").incr(delta.inMilliseconds.toInt)
    }
  }
}

package com.twitter.finagle.serverset2.client

import com.twitter.finagle.stats.{Counter, StatsReceiver}
import com.twitter.util.{Closable, Duration, Future, Time, Timer}

class StateTracker(
  statsReceiver: StatsReceiver,
  samplePeriod: Duration,
  timer: Timer
) extends Closable {

  private[this] var currCounter: Option[Counter] = None
  private[this] var lastSample: Time = Time.now

  private[this] val timerTask = timer.schedule(Time.now + samplePeriod, samplePeriod) {
    sample()
  }

  def close(deadline: Time): Future[Unit] = {
    timerTask.close(deadline)
  }

  def transition(newState: SessionState): Unit = synchronized {
    sample()
    currCounter = Some(statsReceiver.counter(s"${newState.name}_duration_ms"))
  }

  private[this] def sample(): Unit = synchronized {
    val now = Time.now
    val delta = now - lastSample
    lastSample = now
    currCounter.foreach { counter => counter.incr(delta.inMilliseconds.toInt) }
  }
}

Make statetracker cache counter instead of state.
finagle-serverset: Make statetracker cache counter instead of state.

Problem:
The statetracker creates a new counter everytime it samples and
outputs the time in current state.

Solution:
Instead of caching the current state, we can just cache the a
counter which will prevent a new counter from being created.

RB_ID=902951
