Algorithm Computation in the Cloud: Microsoft Azure Web Roles


Worker and Web Roles are some of the great features that Microsoft Azure has to offer. These two features are designed to do computation for you in the cloud. A worker role is basically a virtual machine that can be used as a back-end application server in the cloud. Similarly, a web role is a virtual machine hosted in the cloud, but the difference is that this one is used as a front-end server that requires Internet Information Services (IIS). So you can use a web role if you want to have an interface exposed to the client - for example an ASP.NET web site - that makes interaction from the outside possible.

In the following tutorial, I will focus on web roles. I will show you how to create a web role and host it on an Azure web site. The web role's task will be to launch a console application - which is a path finder algorithm that I wrote in C++ - that reads input from the client, computes and then returns results. The reason for choosing a web role for this, is to make communication possible through REST, so the client can use a web site or simply do a GET request to fire up the console application in the cloud and get back the results as JSON.

Creating the Azure Web Role

The first step is to download the Microsoft Azure SDK. Since I'm a .NET developer, I downloaded the Visual Studio 2013 version. The SDK includes an Azure emulator, that we will be using locally. The web role has to be hosted somewhere, we will choose a cloud service for that. So, start up Visual Studio and create a Windows Azure Cloud Service project:

cloud

Choose ASP.NET Web Role:

cloud2

Choosing the front-end stack type is up to you, I choose an MVC Web API without authentication:

cloud3

Go to the WebRole class in the WebRole project and set a break point inside the OnStart() method. Now build and run your cloud service, you will see that an Azure Compute Emulator is started up and the break point is reached:

cloud4

Press F5 to continue. Your web role web site is now up and running. Right click the Azure Compute Emulator icon in the task bar, and choose "Show Compute Emulator UI". A new window shows up, click on your web role in the left column:

cloud5

This shows you the status of the web role. A web role inherits the abstract class RoleEntryPoint that has three virtual methods; OnStart(), Run() and OnStop(). These methods are called as their name suggests and can be overridden. We already overrode the OnStart() method as we saw earlier. Now, the next step is to launch the console application as a process from the web role.

Starting a Console Application Process from the Azure Web Role

Delete the default override of OnStart() in the WebRole class. We want to call a custom method from our web role independently. Create an interface IWebRole that looks like this:

public interface IWebRole
{
   string RunInternalProcess(string stringParams);
}

Make WebRole implement this interface. RunInternalProcess(string stringParams) is a custom method that we will call from the client. The method will then launch a console application process and return results back to the client as JSON. We want the process to do the operation asynchronously. Here is part of how the implementation looks like:

public string RunInternalProcess(string stringParams)
{
    var path = HttpContext.Current.Server.MapPath("..\\TSP_Genetic_Algorithm.exe");
    var result = RunProcessAndGetOutputAsync(stringParams, path).Result;
    return result.Replace(" ", "\n");
}

private static async Task<string> RunProcessAndGetOutputAsync(string stringParams, string path)
{
    return await RunProcessAndGetOutput(stringParams, path);
}

private static Task<string> RunProcessAndGetOutput(string stringParams, string path)
{
    var process = CreateProcess(stringParams, path);
    process.Start();
    var result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    var taskCompletionSource = CreateTaskCompletionSourceAndSet(result);
    return taskCompletionSource.Task;
}

As you can see, the method starts a process called TSP_Genetic_Algorithm.exe which is included in the project. It's important to set the "Copy to Output Directory" property of this executable file to "Copy always", such that it's always copied to the project directory. You can do this by right clicking the executable, and choosing "Properties":

cloud15

The next step is to make the client call up the web role through an HTTP GET request.

Calling the Azure Web Role from the Client

We need to make it possible for the client to call the web role, we will do this by creating an HttpGet ActionResult. Go to HomeController and inject the WebRole interface there:

private readonly IWebRole _webRole;

public HomeController(IWebRole webRole)
{
    _webRole = webRole;
}

Create an HttpGet ActionResult, it can look like this:

public ActionResult Solve(string[] c)
{
    var coordinates = c.Aggregate(string.Empty, (current, t) =&gt; current + (t + " "));
    _results = _webRole.RunInternalProcess(coordinates);
    return Json(new { results = _results }, JsonRequestBehavior.AllowGet);
}

This GET request takes in an array of string coordinates, calls the web role with these coordinates, which in turn launches up the process with the input and then finally return the results back to the client as JSON. Beautiful, isn't it? Build and run your cloud service. Now type this in the browser address field:

http://127.0.0.1:81/Home/Solve?c=200,300&c=400,300&c=400,400&c=400,200&c=500,200&c=200,400&c=400,300

And here are the results:

cloud11

Publishing the Azure Cloud Service

The final step is to publish our Azure cloud service so it goes online. This process is pretty straightforward, but it assumes that you have a Microsoft Azure subscription and a target profile. Once you have registered a subscription, right click the cloud service project and choose "Publish...":

cloud7

Go through the wizard to create a target profile:

cloud8

Enable Remote Desktop if you want remote access to your virtual machine in Azure, this is pretty handy. Once done, click on Publish in the last dialog:

cloud9

The publishing process will start in Visual Studio:

cloud10

And then complete:

cloud12

That's it! Your Azure web role web site is now online, and you can now do the GET request through the web:

cloud14

You can go to the Azure Portal to view statistics and maintain your web role there:

cloud13

Hope you enjoyed this tutorial!