Finagle Future objects, can they be reused?

I am new to Finagle. Now I am reading the code and found that the Future object is reused in different merge actions. My question is because of the reason that the Future object will be executed several times (in each connection), or will be executed only once and save the result for subsequent connections?

Example:

Future<A> a= b .join(c) .flatMap(new SomeFunctionReturningA()); Future<Tuple2<A, B>> future1 = a.join(b); Future<D> future2 = future1.flatMap(new SomeFunctionReturningD()); future2.get(); 

So, will b be executed twice or only once?

+4
source share
3 answers

A Future is just a container for a value, and the value will only be set once! Future[T] have different states:

  • Empty
  • Set with a value of type T
  • Install with exception

When you use map/flatMap with the function f in Future A , you simply create a new Future B that will be the result of the previous one transformed by the function f .

Note that:

  • If Future A is not yet filled, you will have B not yet filled
  • A thread that sets the value of A to A will also execute f(a) and set the value to B
  • If A already "full", then the calling map / flatMap object will also execute f(a) , but it will NOT recalculate the value of A
  • You can also use onSuccess/onFailure , which will simply register the code that will be executed when the future gets its value.
+5
source

I myself wrote a quick test:

 import com.twitter.util.Function; import com.twitter.util.Future; import java.util.Random; import org.junit.Test; import scala.Tuple2; public class FinagleFutureTest { int counter = 0; @Test public void test(){ Counter counter = new Counter(); Future<Counter> one = Future.value(counter).flatMap(new IncrementFunction()); Future<Counter> two = one.flatMap(new IncrementFunction()); Future<Tuple2<Counter, Counter>> three = two.join(one); Tuple2<Counter, Counter> tuple = three.flatMap(new TupleFunction()).get(); System.out.println("one: "+ tuple._2().count+", "+tuple._2().randomInt); System.out.println("two: "+ tuple._1().count+", "+tuple._1().randomInt); } static class TupleFunction extends Function<Tuple2<Counter, Counter>, Future<Tuple2<Counter, Counter>>>{ @Override public Future<Tuple2<Counter, Counter>> apply(Tuple2<Counter, Counter> t1) { return Future.value(t1); } } static class IncrementFunction extends Function<Counter, Future<Counter>>{ @Override public Future<Counter> apply(Counter counter) { counter.add(); return Future.value(counter); } } static class Counter{ public int count = 0; public int randomInt; Counter(){ Random random = new Random(); randomInt = random.nextInt(); } public void add(){ count++; } } } 

and here is the result:

 one: 2, 2009034289 two: 2, 2009034289 

Thus, the conclusion is that the Future object is executed only once, no matter how many join operations it takes.

+1
source

The future object is executed only once.

Finagle uses com.twitter.util.A The Future is a handle to a value that is not yet available.

The two easiest ways to use the Future are: to block and wait for the calculation to return, register a callback that will be called when the calculation ultimately fails or fails. The FuturePool object allows you to place the blocking operation in its stream.

You can get more information about Finagle Future (Future Pool) from twitter blog and fingale github

+1
source

Source: https://habr.com/ru/post/1442329/


All Articles