Causeway Platform Developer: Promises

Placeholder -- ignore for now

Instrumenting a Platform to Generate Causeway Trace Logs
Introduction, communicating events loops


 * Waterken
 * promises
 * AmbientTalk
 * futures

Getting Started

 * See Causeway for user documentation, which includes instructions for launching Causeway from a command line shell.


 * See HP Labs Technical Report for a more in-depth discussion of the material presented here. A careful read is not necessary but a quick skim to see what's there is a good idea.

This documentation assumes an understanding of the purchase-order example program.


 * Read the source code. It's part of the E distribution and can be found here.

Also, we're asking that you write a version of the purchase-order example that runs on your platform, as one of your first test cases. It's best to start with the Ajax-style continuation-passing, using callbacks. This approach is less expressive than promises, but generating the trace log is more straightforward.


 * Browse the example programs in Causeway, e.g., Help >> Open Waterken Example (Ajax-style).

Support Tools

 * Enable Causeway's debug view

Setting Causeway's debug flag enables a debug view. As events are selected in the viewer, the debug view shows the corresponding trace record in the log file. This is a very useful option, especially in the beginning, when things aren't quite working. $ rune -Dcauseway_debug=true causeway.e-swt

( Note: When this debug option is set, Causeway's JSON parser uses getTwine. This is an inefficient algorithm which is not likely to change anytime soon. The trace logs for the example programs are roughly 20K; the poor performance is noticed with logs > 250K.)




 * Support for visualizing graph filter algorithm

Your first traces are likely to be noisy and Causeway's views, baffling. It can take some careful consideration and cleverness to figure out why and what to do about it. Simple filters (e.g., hiding stack frames, options for more or less detail) are necessary but not sufficient. The right abstractions must be found to enhance program understanding, while maintaining causality. Currently, Causeway does not support user-specified filters that manipulate the message graph (DAG) structure. Our approach to debugging our filtering algorithm is described HERE. Once we have more experience, we expect to support user-specified filters.

Causeway's Trace Log Format
Causeway supports the trace log format defined by Tyler Close at waterken.sourceforge.net/debug/.

We will explain this format, and how to get your platform to generate it, in three steps: Simple Ajax (below), Ajax with promotion, and full promises.



When causality tracing is on the events are logged as follows.

In the Causeway viewer, individual tree items represent events and their descriptive labels depend on the information available in the trace record for the event.



To present the most useful information available, Causeway labels tree items according to the following priority.


 * 1) User comments specified in the text field, prefixed with "#".
 * 2) Source code specified in the top call stack object. The trace object must include a span.
 * 3) Source file and function name specified in top call stack object. The trace object must include a name and source.
 * 4) If none of the above, the label is a Causeway-generated comment, prefixed with "##", e.g., ## unknown sender.

Logging Ajax-style Messaging in Waterken
Consider the sequence of events shown below.




 * a remote inventory object is queried for the availability of a part
 * the inventory object reports true to teller, a callback object

The eventual send to the inventory object has two log entries: a Sent</tt> and its corresponding Got</tt>.

"class" : [ "org.ref_send.log.Sent", "org.ref_send.log.Event" ], "anchor" : { "number" : 3, "turn" : { "loop" : "http://localhost:8080/-/buyer/", "number" : 3 }   },    "message" : "lqhjpwbbeemozk-2-0", "timestamp" : 1274238401772, "trace" : { "calls" : [ { "name" : "Main.Buy.fulfill", "source" : "org/waterken/purchase_ajax/Main.java", "span" : [ [ 68 ] ] }, {         "name" : "Main.Buy.fulfill", "source" : "org/waterken/purchase_ajax/Main.java", "span" : [ [ 48 ] ] } ]   }

"class" : [ "org.ref_send.log.Got", "org.ref_send.log.Event" ], "anchor" : { "number" : 1, "turn" : { "loop" : "http://localhost:8080/-/buyer/product/", "number" : 2 }   },    "message" : "lqhjpwbbeemozk-2-0", "trace" : { "calls" : [ { "name" : "InventoryMaker.InventoryX.partInStock", "source" : "org/waterken/purchase_ajax/InventoryMaker.java" } ]   }


 * In the Sent</tt> trace record:
 * anchor</tt> uniquely identifies the origin of this message send as the 3rd messaging event from the buyer vat, turn 3.
 * message</tt> is a generated string which uniquely identifies a message.
 * trace</tt> is the stack capture at the point of the message send.

(Note: The loop</tt> field identifies the vat by URI. By convention, Causeway picks up the part following "/-/", in this case buyer, for a short display name.)

(Note: The timestamp</tt> field is optional. Currently, Causeway ignores it, so it's not shown in the remaining trace records.)

The corresponding Got</tt> has a matching message</tt>. The message delivery in the product vat starts a new turn, turn 2. Being at the top of a new turn, there is limited stack capture and getting a source span through Java reflection, is not practical.

Reporting true to teller</tt> has two log entries: a Sent</tt> and its corresponding Got</tt>.

"class" : [ "org.ref_send.log.Sent", "org.ref_send.log.Event" ], "anchor" : { "number" : 2, "turn" : { "loop" : "http://localhost:8080/-/buyer/product/", "number" : 2 }   },    "message" : "ewrigzpctikrhk-1-0", "trace" : { "calls" : [ { "name" : "InventoryMaker.InventoryX.partInStock", "source" : "org/waterken/purchase_ajax/InventoryMaker.java", "span" : [ [ 19 ] ] } ]   }

"class" : [ "org.ref_send.log.Got", "org.ref_send.log.Event" ], "anchor" : { "number" : 1, "turn" : { "loop" : "http://localhost:8080/-/buyer/", "number" : 10 }   },    "message" : "ewrigzpctikrhk-1-0", "trace" : { "calls" : [ { "name" : "AsyncAnd.run", "source" : "org/waterken/purchase_ajax/AsyncAnd.java" } ]   }

The reply to the query is the 2nd messaging event from the product vat, turn 2.

The corresponding Got</tt> has a matching message</tt>. The message delivery in the buyer vat starts a new turn, turn 10.

Logging Promise-based Messaging in Waterken
Our example program implements a promise-based distributed procedure for handling new purchase orders. Before an order is placed, certain conditions must be met: the item is in stock and available, the customer's account is in good standing, and the delivery options are up to date.

An object residing in the "buyer" vat has remote references to objects residing in the "product" and "accounts" vats. The buyer queries the remote objects with asynchronous (non-blocking) message sends. A promise is a placeholder for the answer to a query; when the answer becomes available the promise resolves to that value.

The code snippet below shows the 3 queries being fired off. The order of the incoming answers cannot be known. All 3 answers must be examined before the order is placed.

Collecting the answers is handled by an AsyncAnd</tt> object. The <tt>run</tt> method returns a promise for the result. By registering a when-block on the resolution of that promise, the invocation of <tt>checkAnswers</tt> is synchronized with the completion of the collection of the answers.

Promise<Boolean> partP = _._(inventory).partInStock(partNo); Promise<Boolean> creditP = _._(creditBureau).checkCredit(name); Promise<Boolean> deliverP = _._(shipper).canDeliver(profile);

final Promise<Boolean> allOkP = new AsyncAnd(_).run(partP, creditP, deliverP);

/*    * Register a when-block on promise returned by AsyncAnd. * The block executes when the promise resolves (either     * fulfilled or rejected). */

_.when(allOkP, checkAnswers(_, inventory));

We start with a detailed description of a single message, <tt>partInStock</tt>. Then we'll look at how <tt>AsyncAnd</tt> collects the answers and reports a final result.

The graph below describes the mechanics of Waterken promise resolution.




 * 1) buyer does an eventual send to product and registers a when-block on the resolution of that promise
 * 2) product receives the message and returns a result
 * 3) buyer receives the result and resolves the promise locally
 * 4) buyer executes the when-block

The sequence of graphs below describe transformations on Causeway's message graph to better represent promise semantics, rather than the mechanics of promise resolution.



Now, we can say more about the  logic.

In  a promise-resolver pair implements the communication synchronization.

public Promise<Boolean> run(Promise<Boolean>... answers) { final Channel<Boolean> result = _.defer; final int[] expected = {answers.length}; for (Promise<Boolean> answerP : answers) {

/*            * Register a when-block on each promise. The block executes * when the promise resolves (either fulfilled or rejected). */

_.when(answerP, new DoAnswer(expected, result.resolver)); }       return result.promise; }

The  message captures a stack trace and logs a <tt>Progressed</tt> record to indicate that a state-based event contributed to the resolution of a promise. (If logging is off, it is a semantic no-op.)

Let's say all 3 answers are true and the promise eventually resolves to true. It's reasonable to think of all 3 answers as contributing to the resolution, i.e., the resolving of the promise had 3 causes. But without event promotion, the message-order view would show only 1 cause.

The arrival of the first 2 answers causes a local state change (a counter is decremented), while the last answer causes a message send. Without event promotion, the promise resolution would have a single cause: the last answer. The  message is used to promote state-based events to message order.

public Void fulfill(Boolean answer) { if (answer) { myExpected[0]--; if (myExpected[0] == 0) { /*                * Resolve the promise with true. */               myResolver.apply(true); } else { /*                * Progress had been made in resolving the promise. * If logging is on, a Progressed event record is written. * If logging is off, this is a no-op. */               myResolver.progress; }       } else { /*            * Resolve the promise with false. Notice that this * short-circuits the logic: any remaining expected answers * are ignored. */           myResolver.apply(false); }   }

The resulting process-order view and message-order view are shown below. The registration of when-blocks are filtered from message-order as they don't contribute to the understanding of message flow; however, they do appear in process-order.



Performance Issues in Waterken
Due to the expense of stack capture in Java, tracing in Waterken incurs roughly, an order of magnitude performance penalty. If tracing is off, there is no penalty.

Waterken guarantees message order delivery and in addition, if a connection is dropped, there's enough information to know about partial success. For example, if 2 messages (msg1, msg2) are sent from vat A to vat B, they are guaranteed to be processed in the order sent. If the connection is dropped after msg1 is successfully sent, when the connection is re-established, it is known that only msg2 must be resent.

The identifiers used to support these guarantees are also used for tracing. The advantage of these multi-purpose identifiers is there is no overhead when tracing is off (i.e., unique message identifiers, just for tracing, are not sent out over the wire.)

( Note: Resending a message after a connection is re-established can result in 2 identical <tt>Sent</tt> events being logged. Causeway notices when the event records are identical and ignores the duplicate.)