Monthly Archives: November 2012

Do not overdo parallelism and asynchronous

Sometimes it’s easy to little bit overdo the need for having everything asynchronous and parallel. Quite often in last few weeks I’ve seen methods similar to this one.

Console.WriteLine("Starting");
Parallel.For(1, 10, async i =>
{
	await Task.Delay(200);
	Console.WriteLine(i);
});
Console.WriteLine("Finished");

What’s the result? For somebody maybe surprisingly:

Starting
Finished

Why? What we see here is a two pieces “process”. First the Parallel.For. This methods runs the provided method in parallel (for our discussion it doesn’t matter how and what exactly that means) and waits for all methods to complete. The lambda expression we’re providing is asynchronous. And though the async/await simplified the programming a lot, it’s still standing on basic principles around Tasks. And that’s the key for understanding what’s wrong. The async lambda is basically (I’m simplifying here) starting a task to do the work and returning that task so you can eventually (a)wait it to complete. But the Parallel.For care about the method (all of them) returning, not (a)waiting tasks (it’s actually an Action<int> also known as “async void” hence it has no idea about the task inside). And here you have it.

The question that’s left is, how to fix it? :) Probably easiest way is to extract the lambda to method and wait for that task to complete. That will make the method blocking so the Parallel.For is not going to end prematurely.

static void Main(string[] args)
{
	Console.WriteLine("Starting");
	Parallel.For(1, 10, i => Action(i).Wait());
	Console.WriteLine("Finished");
}

static async Task Action(int i)
{
	await Task.Delay(200);
	Console.WriteLine(i);
}

But wait. That’s a little bit crazy, isn’t it? We have tasks running asynchronously and we’re spinning Parallel.For and waiting??? You can actually run the loop starting the asynchronous methods capturing the returned tasks and then use Task.WaitAll or if you want to go really deep async ;) you can use Task.WhenAll and await it.

If you’d like to see something like ForEachAsync (or maybe ForAsync) you can get and inspiration and other interesting notes from this Stephen Toub’s blog post.

Error “Could not load type ‘System.Runtime.CompilerServices.ExtensionAttribute’ from assembly ‘mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′.” on .NET 4.0/.NET 4.5

This error isn’t related only to FirebirdClient only, but any .NET application that is targeting multiple .NET Framework versions, but I spotted it first during FirebirdClient development.

The error message Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. is little bit cryptic. Let me explain how all this mess happened. The .NET 4.5 is in-place update of .NET 4.0. So a lot of stuff looks like it’s .NET 4.0, but it’s not. It’s .NET 4.5. One change Microsoft did in .NET 4.5 was moving the ExtensionAttribute into mscorlib (so they can use extension methods in mscorlib). So if you build application targeting .NET 4.5 and it uses just plain .NET 4.0 (or even older) it runs fine. You’re fine, user is fine. Until it hits some extension method. Then it tries to locate the above mentioned attribute, but from mscorlib. On .NET 4.0 it’s not there. Even if the version says 4.0.0.0. Yeah, in-place update.

Problem is, that users (even some developers) are not aware of these minor changes. And because the application runs – at least a while – it’s confusing. :\ The bottom line is – always download/use exact .NET Framework version version of the application/library.

NuoDB has new (web-based) management console

I don’t know whether you noticed, NuoDB is now in RC stage. If you’re following NuoDB long enough you might remember old console (you can check some images in my older post). It was a Java based application. Simple and it was not nice. Now it’s different. It’s web based. So it’s easier to connect to it. You can even connect to it from your phone (if it’s powerful enough to handle the JavaScript). It’s way nicer, nothing fancy (but I’m not expecting to be super cool, it’s basically admin panel for database, right?) and mainly, it has tons of new features.

After you log in, you see (initially) basic screen about your (now) so called domain.

Probably first thing you’ll try is starting new database. Simple wizard will allow you to do this. Here’s final screen before starting it up.

You can then see how is your database doing. Transaction nodes, storage nodes, memory etc.

You can even see some graphs about what’s going on (it’s live updated).

Very important for your database is keeping everything smooth and cleanly running. And if something goes wrong (or actually seems to be going wrong), act. In the new console you can set up alerts based on different events or metrics and browse these for investigation, for example.

You know I said I’m considering it being admin panel for database. Plain simple is OK for me. If I’m using it often enough my muscle memory learns the moves and I can get my information quickly whenever it is. Sometimes it takes little it longer, but hopefully we’re not doing this admin stuff often. But the new web console allows you customize some views to your needs, hence you can have all your information in front of you on few screens. You can show different views, organize these or collapse some. Some views are even parametrized, i.e. by database.

I like the new console. With the old one I was more happy with the command line and doing everything manually. With the new one I’m doing more and more stuff in it, especially if trying something.

I know I promised blog post about using NuoDB in cloud (Amazon EC2/Amazon S3), but still busy to prepare it completely. :\

ADO.NET provider for Firebird 3.0.1 released

Few weeks ago the 3.0.0 version was released (actually it was during Firebird Conference 2012, during my talk ;) ) and it came with some new features and improvements (hence version 3.0.0). And as it goes, nothing is perfect and few items slipped through testing phase. So that’s th reason why today, I’m happy to announce version 3.0.1 to be released.

You can download it from NuGet or from Firebird SQL site as it gets updated.

This release contains fixes for two regressions introduced in 3.0.0. You can see both in tracker. It’s also my initial step to release these small bugfix releases more often (as discussed in list).

Go grab it, while it’s hot.

AWS Simple Storage Service (S3) pain

When we worked together with Aleš Roubíček using Windows Azure cloud, we faced quite a few surprises with applications in cloud. Especially when the application isn’t small and is using a lot of “cloud” services. Sometimes we used hashtag on Twitter for it: #cloudlife.

Last week I was creating fairly simple tool that allows (or should allow) me to copy some files to S3 bucket. I had some special needs because the folder where the files were was heavily changed and the tool needed to handle that. Because AWS has SDK for .NET I was not expecting that to be extremely difficult. How wrong I was.

Every file in S3 has ETag associated. And you will get it with almost every request, i.e. when you’re listing bucket. Good candidate for decision whether to copy the file (because it was changed) or not. The SDK contains AmazonS3Util class which allows you to compute the checksum/ETag. Great, should be piece of cake. Not so fast. The SDK and S3 itself has some gotchas – #cloudlife.

First the ETag returned from SDK is inside double quotes. But the value from AmazonS3Util is not. I can fix that. Could be worse. Let’s move forward. For big or huge files the new multipart upload (you are uploading the file in smaller chunks) is recommended. Boom. Checksum/ETag is then different. For same file uploaded “normal” way and multipart the ETag differs. Sadly the AmazonS3Util can compute only the checksum for “normal” type of upload. Never mind. Next. Every file can have some metadata associated, I can store my own checksum there. I will need to request metadata in separate call, but I can live with that. So let’s use WithMetadata method to add some key/value. Good no problem so far. Retrieving metadata. Suspicious method GetObjectMetadata (and the BeginGetObjectMetadata/EndGetObjectMetadata counterparts) returns NameValueCollection, I can probably find the same key as I stored there. Not so fast. My key originally stored as i.e. foobar is now x-amz-meta-foobar. Maybe the WithMetadata could enforce that in input, so I know there’s always this prefix. I don’t like hidden magic. Almost there. Every request is also signed (that’s, also, why we have the key and secret) to be sure the request was not changed on the way etc. (headers, parameters etc. are part or the input for signature). SDK handles the signature for me. Badly. As long as you use US-ASCII characters in filenames/paths or better to say keys for storing the data, you’re fine. Try to put there some “special” characters, like in my case diacritics. Suddenly the signature doesn’t match what’s expected and server will not allow me proceed further. Yes you’re right, the encoding issue. Sadly I don’t know currently workaround. Hope it’ll be fixed soon. Hitting wall every while.

And you know what? Some limitations and challenges I like (ETag issue. These are reasons why I like development and creating software. Some I don’t like (encoding issue), I shows somebody wasn’t doing own work properly. And why #cloudlife? From marketing we’re told the cloud is so cool and so … best, it’ll solve all our problems and we expect it to be perfect. But it’s not. It’s piece of architecture as every other component in your solution. You should expect issues.

Have fun, develop for fun.