0

I have a Finatra application that accesses Cassandra using Datastax driver which produces Guava Futures. The conversion is done by following code

import com.google.common.util.concurrent.{FutureCallback, Futures, ListenableFuture}
import com.twitter.util.{Future, Promise}

implicit def toTwitterFuture[A](f: ListenableFuture[A]): Future[A] = {
  val p = Promise[A]()

  val callback = new FutureCallback[A] {
    override def onSuccess(result: A): Unit = p.setValue(result)

    override def onFailure(t: Throwable): Unit = p.setException(t)
  }

  Futures.addCallback(f, callback)
  p
}

Problem is, after this conversion the rest of the request processing happens in a thread pool created by Cassandra driver potentially blocking other I/O tasks. How can I access Finagle's executor where this kind of CPU-intensive work should be done?

synapse
  • 5,588
  • 6
  • 35
  • 65
  • Guava futures are eager, meaning once you have a `ListenableFuture[A]`, it is already running inside some executor. I'm not sure what you're referring to here? Are you talking about the callback execution? – Yuval Itzchakov Nov 01 '18 at 06:19
  • @YuvalItzchakov yes, I'm talking about callbacks. Datastax docs suggest using another executor for CPU intensive continuations so I did just that. Finagle's executors are doing nothing but I guess that's not a big deal. – synapse Nov 01 '18 at 12:11
  • 1
    `Futures.addCallback` has an overload which accepts an `Executor`. – Yuval Itzchakov Nov 01 '18 at 13:14

0 Answers0