Reputation: 63
I'm working on a backend migration on some scala code. The code current returns com.twitter.util.Future[A] and I am taking the response from my new backend and transforming it into Future[A] as well
To monitor the migration, I want to do a comparison so I can tell how often the response from the 2 backends differ, so I have something like
val b1Resp: Future[A] = getResponseFromBackend1()
val b2Resp: Future[A] = getResponseFromBackend2() map { resp => convert(resp)}
return b1Resp
I want to verify that the results of the futures are either the same or that they both fail. On discrepancies, I will be publishing a metric.
Moreover, I don't want to delay the return. I don't really care when the metric is published (within reason of course), so it's okay to allow execution to continue
How do I do the comparison?
Upvotes: 0
Views: 148
Reputation: 20601
The broad outline of what you want to do is
{
val b1Resp: Future[A] = getResponseFromBackend1()
val b2Resp: Future[A] = getResponseFromBackend2() map { resp => convert(resp)}
Future.join(b1Resp, b2Resp) // combine the two futures into a future of a pair of their values
.foreach { // when both are complete...
case (b1, b2) =>
// compare and tickle a metric or whatever
() // explicit Unit value, in case the compiler warns about discarding a value
}
b1Resp // return should generally be avoided in Scala
}
Note that Future.join.foreach
just arranges for the comparison etc. to happen in the future; it should only add a few microseconds to the block.
For standard library futures, the equivalent to Future.join.foreach
is (assuming an implicit ExecutionContext
is in scope):
b1Resp.zip(b2Resp)
.foreach {
// as above
}
Upvotes: 1