Asynchronous Functions

Asynchronous Commands are a useful way to organize asynchronous activities, but they don’t have any way to pass values or control back to a caller. This post contains a simple Asynchronous Function library that lets you do that. In C# you call an Asynchronous Function like:

 void CallingMethod(...) {
    ... do some things ...
    IAsyncFunction<String> httpGet=new HttpGet(... parameters...);
    anAsynchronousFunction.Execute(CallbackMethod);
}

void CallbackMethod(CallbackReturnValue<String> crv) {
    if (crv.Error!=null) { ... handle Error,  which is an Exception ...}
    String returnValue=crv.Value;
    ... do something with the return value ...
}

We’re using generics so that return values can be passed back in a type safe manner. The type of the return value of the asynchronous function is specified in the type parameter of IAsyncFunction and CallbackReturnValue.

Asynchronous functions catch exceptions and pass them back in  in the CallbackReturnValue.  This makes it possible to propagate exceptions back to the caller,  as in synchronous functions.  The code to do this must has to be manually replicated in each asynchronous function,  however,  the code can be put into a wrapper delegate.

You could do the same thing in Java, but the CallbackMethod would need to be a class that implements an interface rather than a delegate.

Continue Reading »

Reliable Distributed Systems

Developing distributed systems can be difficult, and many of the patterns that are successful in developing conventional applications (such as constructing complex operations by composing simpler operations) lead to applications that work… some of the time. Although researchers have known it for years, a new generation of practitioners are learning the hard way that there’s an intractable contradiction between scalability, reliability and data integrity.

Ken Birman’s textbook Reliable Distributed Systems, is an excellent introduction to this brave new world, focused on the construction of systems that are reliable — that keep working when something goes wrong. This is critical for rich internet applications (that work over an unreliable public internet) and for applications that run on large clusters (where there’s a lot of hardware to fail.) If you find his text is pricey, you’ll appreciate the slides from his Cornell course available on his home page.

The Asynchronous Command Pattern for HTTP in Silverlight and GWT

When you’re writing RIA applications in an environment like Silverlight or GWT, you’re restricted to doing asynchronous http calls to the server — this leaves you with a number of tricky choices, such as, where to put your callback functions. To be specific, imagine we’ve created a panel in the user interface where a user enters information, then clicks on a form to submit it. The first place you might think of putting the callback is in the class for the panel, something like

public class MyPanel:StackPanel {
	... other functions ...

        void SubmitButton_Click(Object sender,EventArgs e) {
           ... collect data from forms ...
           ServerWrapper.DoSubmission(formData,SubmissionCallback);
        }

        void SubmissionCallback(SubmissionResult result) {
           ... update user interface ...
        }
}

(Although code samples are in C#, the language I’m using now, I developed this pattern when working on a Java project.) This is a straightforward pattern for the simplest applications, but it runs out of steam when your application becomes more complex. It can become confusing to keep track of your callback functions when your object does more than one kind of asynchronous call: for instance, if it has multiple buttons. If the same action can be done on the server from more than one place in the UI, it’s not clear where the callback belongs.

One answer to the problem is to use the Command Pattern, to organize asynchronous activities into their own classes that contain both the code that initiates an asynchronous request and the callback that runs when the request completes. Continue Reading »

Optimistic Locking For Retrieving Result Sets

I’m in the middle of updating my Silverlight code to use asynchronous HTTP requests — fortunately, I spent last summer writing a GWT application, where HTTP requests have always been asynchronous, so I’ve got a library of patterns for solving common problems.

For instance, suppose that you’re doing a search, and then you’re displaying the result of the search. The most reliable way to do this is to use Pattern Zero, which is, do a single request to the server that retrieves all the information — in that case you don’t need to worry about what happens if, out of 20 HTTP requests, one fails.

Sometimes you can’t redesign the client-server protocol, or you’d like to take advantage of caching, in which case you might do something like this (in psuedo code):

getAListOfResults(new AsyncCallback {
     ... clearGUI();
         foreach(result as item) {
            fetchItem(item,new AsyncCallback {
               ... addItemToGui()
         }
}

First we retrieve a list of items, then we retrieve information about each item: this is straightforward, but not always reliable. Even if your application runs in a single thread, as it would in GWT or if you did everything in the UI thread in Silverlight, you can still have race conditions: for instance, results can come back in a random order, and getAListOfResults() can be called more than once by multiple callbacks — that’s really the worst of the problems, because it can cause results to appear more than once in the GUI.

There are a number of solutions to this problem, and a number of non-solutions. A simple solution is to make sure that getAListOfResults() never gets called until the result set has come back. I was able to do that for quite a while last summer, but the application finally reached a level of complexity where it was impossible… or would have required a major redesign of the app. Another is to use pessimistic locking: to not let getAListOfResults() run while result sets are coming back — I think this can be made to work, but if you’re not careful, your app can display stale data or permanently lock up.

Fortunately there’s a pattern to retrieve result sets using optimistic locking that displays fresh data and can’t fail catastrophically

Continue Reading »

Managing Concurrency With Asynchronous HTTP Requests

I developed a rather complicated GWT application last summer and spent plenty of time struggling with the concurrency issues involved with with applications that use asynchronous web requests: for instance, the HttpWebRequest in Silverlight or the XmlHttpRequest in Javascript. Up until Silverlight 2 beta, Silverlight programmers could perform synchronous requests, but the latest version of Silverlight supports only asynchronous requests… We’re scrambling to update our apps.

There’s a “standard model” that works for writing reliable, performant and secure RIAs — it works for GWT, Flex, and Silverlight and plain old AJAX apps too.

Continue Reading »

Threading in C# and dot net

It’s not always easy to find good documentation online for the Microsoft universe, but Joe Albahari has written a great article about Threading in C#.

The Wisdom of a Closed Platform

Daniel Eran Dilger has been a leading writer about the iPhone since before it came out,  and this week he writes about the choices Apple made about concurrency in the iPhone.   Unlike Windows Mobile,  the iPhone only allows the user to have a single third-party application running at a time.  Dan makes the case that portables don’t provide a rich enough interface to let users juggle multiple running tasks (it’s hard enough to do this with a desktop computer) and that it won’t be possible to give a phone-like experience without tight control on process lifecycle.

He’s right.  I’ve got a friend who has an HTC handheld that runs Windows Mobile.  It looks nice,  and it’s got some good features,  but every so often it gets in a state that pegs the CPU at 100%;  it gets warm pretty quick,  so this can’t be good for battery life.  Vendors like Apple, Sun and Nintendo, who have a lot of control over hardware and software aspects of  platform,  can often users an experience that can’t be matched by more open platforms,  where no vendor claims responsibility for the performance of a system.

Windows Server Core Installation

Tom’s Hardware has a nice review of the Core Installation of Windows Server 2008,  which makes it possible to install Windows Server 2008 with a limited GUI.  It’s exciting to see Windows get more mature and flexible:  the core install takes up about half the disk space of a conventional install.  However,  the fit and finish of the Core Installer is poor — for instance,  an administrator needs to type more than 900 characters on the command line to install Microsoft IIS.  We’ll give Microsoft another chance,  but,  for now,  the Core Installation is less mature than text-mode linux distributions were a decade ago.

Fit and finish are important factors in choosing a server operating system — I’ve been trying to repurpose an old laptop as a media server,  and finding it a challenge to find a modern Linux installation that installs without trouble on a machine with no DVD drive and limited RAM.

Intrusion of the Real

Dave Bonta photographs a red hawk on the Penn State campus, and meditates on how people’s experience of nature is conditioned by our shared hyperreality. Dave notes that ‘a helpful webpage on film sound clichés, “the Red-Tailed Hawk scree signifies outdoors and a big, lonely place.” Anytime a rocky mountainside appears in a movie, you can almost count on hearing that raspy scream, which most people probably assume belongs to an eagle. It’s also used as an all-purpose signifier of impending or just-concluded drama in the typical outdoors adventure flick.’

Most students walked by obliviously while a handful used cellphone cameras to capture the hawk attacking it’s prey. He was left with an awkward feeling that he’d met a creature from another world — but wasn’t completely sure if he, or the hawk, were the alien visitor.

Share My Web Statistics?

Michael Arrington at Tech Crunch announces that Google Analytics now lets webmasters share data with other Google products, and suggests that they should let us share web statistics with everyone, a suggestion that I find a little disturbing.

Like it or not, web marketing is cutthroat, and search engine optimizers are always looking for an edge on the competition — open web statistics are an invitation for somebody else to eat your lunch.

Personally, I like Quantcast, which provides public statistics about the readership of web sites that are useful for advertisers and others, but that respect the privacy of webmasters and users.