Hi Manik,<div><br></div><div><br></div><div>all this, aggregating and chaining Futures looks like the way to go, but what about streaming ?</div><div><br></div><div>It would be elegant to think of a completely 'streamed' interface, using Pipes for the asynchronous aspect (a Piped(In/Out)putStream has to be running in another thread -> the Future's thread ?).</div>
<div><br></div><div>Combining Futures and Pipes sound like an even better solution, lower memory usage and higher throughput ! </div><div><br></div><div>Being able to stream data in and out of the cache is particularly interesting for a Qi4j entitystore since we also use marshaling (JSon for the moment). </div>
<div><br></div><div>And JClouds has a streamed interface, we could connect all the streams together (Infinispan extended Map interface -> Marshaling -> JClouds) and fill multiple pipes at the same time.... sounds like a dream, a funny one ?</div>
<div><br></div><div>Cheers</div><div><br></div><div>phil</div><div><br></div><div><br><div class="gmail_quote">On Wed, Jan 13, 2010 at 3:22 PM, Manik Surtani <span dir="ltr"><<a href="mailto:manik@jboss.org">manik@jboss.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">So I've been spending some time thinking about how we deal with async tasks in Infinispan, both from an API perspective as well as an implementation detail, and wanted to throw a few ideas out there.<br>
<br>
First, lets understand the 4 most expensive things that happen in Infinispan, either simply expensive or things that could block under high contention (in descending order): RPC calls, marshalling, CacheStore and locking.<br>
<br>
We deal with asynchronism in a somewhat haphazard way at the moment, each of these functions receiving a somewhat different treatment:<br>
<br>
1) RPC: Using JGroups' ResponseMode of waiting for none.<br>
2) Marshalling: using an async repl executor to take this offline<br>
3) Use an AsyncStore wrapper which places tasks in an executor<br>
4) Nothing<br>
<br>
and to add to it,<br>
<br>
5) READs are never asynchronous. E.g., no such thing as an async GET - even if it entails RPC or a CacheStore lookup (which may be a remote call like S3!)<br>
<br>
The impact of this approach is that end users never really get to benefit from the general asynchronism in place, and still needs to configure stuff in several different places. And internally, it makes dealing with internal APIs hard.<br>
<br>
Externally, this is quite well encapsulated in the Cache interface, by offering async methods such as putAsync(), etc. so there would be little to change here. They return Futures, parameterized to the actual method return type, e.g., putAsync returns Future<V> in a parameterized Cache<K, V>. (More precisely, they return a NotifyingFuture, a sub-interface of Future that allows attaching listeners, but that's a detail.)<br>
<br>
So I think we should start with that. The user receives a Future. This Future is an aggregate Future, which should aggregate and block based on several sub-Futures, one for each of the tasks (1 ~ 4) outlined above. Now what is the impact of this? Designing such a Future is easy enough, but how would this change internal components?<br>
<br>
1) RPC. Async RPC is, IMO, broken at the moment. It is unsafe in that it offers no guarantees that the calls are received by the recipient, and you have no way of knowing. So RPC should always be synchronous, but wrapped in a Future so that it is taken offline and the Future can be checked for success.<br>
<br>
2) Marshalling happens offline at the moment and a Future is returned as it stands, but this could probably be combined with RPC to a single Future since this step is logically before RPC, and RPC relies on marshalling to complete.<br>
<br>
3) The CacheStore interface should only return parameterized Futures. Implementations that do not offer any async support may simply return a wrapped value. Calls to this interface that require immediate results would simply poll the Future. But in all other cases - ranging from the AsyncStore wrapper to more sophisticated async backends such as JClouds, we get to harness the async nature offered by the impl.<br>
<br>
4) Locking could be async, but understanding context is important here. We currently use 2 different types of Locks: JDK reentrant locks for non-transactional calls, or OwnableReentrantLock for transactional calls where the lock owner is not the thread but instead the GlobalTransaction. For this to work, any invocation context should have reference to the original invoking thread, OwnableReentrantLocks should always be used, and the owner set accordingly, to ensure an Executor thread doesn't gain ownership of a lock instead.<br>
<br>
The end result of all this, is that async API calls will be truly non-blocking, and will be able t harness as much parallelism as possible in the system. Another effect is that we'd have a cleaner internal API which is specifically designed with async operations in mind, and converting any of the async calls to sync calls is trivial (Future.get()) anywhere along the invocation chain. Also, I think once we have this in place, any async transport modes (REPL_ASYNC, DIST_ASYNC) should be deprecated in favour of an async API, since you get the ability to check that a call completes even though it happens offline.<br>
<br>
And a disclaimer: I have no plans to implement anything like this soon, I was just throwing this out for comment and distillation. :)<br>
<br>
Cheers<br>
--<br>
Manik Surtani<br>
<a href="mailto:manik@jboss.org">manik@jboss.org</a><br>
Lead, Infinispan<br>
Lead, JBoss Cache<br>
<a href="http://www.infinispan.org" target="_blank">http://www.infinispan.org</a><br>
<a href="http://www.jbosscache.org" target="_blank">http://www.jbosscache.org</a><br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
infinispan-dev mailing list<br>
<a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
</blockquote></div><br></div>