Creating a Poor Man’s Distributed Cache in Azure

by Brian Hitney 31. August 2010 10:57

If you’ve read up on the Windows Server AppFabric (which contains Velocity, the distributed caching project) you’re likely familiar with the concepts of distributed cache.   Distributed caching isn’t strictly limited to web environments, but for this post (or if I ramble on and it becomes a series) we’ll act like it does. 

In a web environment, session state is one of the more problematic server-side features to deal with in multiple server applications.   You are likely already familiar with all of this, but for those who aren’t:  the challenge in storing session state is handling situations where a user’s first request goes to one server in the farm, then the next request goes to another.   If session state is being relied upon, there are only a few options:  1) store session state off-server (for example, in a common SQL Server shared by all web servers) or 2) use “sticky” sessions so that a user’s entire session is served from the same server (in this case, the load balancer typically handles this).   Each method has pros and cons.

Caching is similar.  In typical web applications, you cache expensive objects in the web server’s RAM.  In very complex applications, you can create a caching tier – this is exactly the situation Velocity/AppFabric solves really well.  But, it’s often overkill for more basic applications.  

The general rule of thumb(s) with caching is:  1) caching should always be considered volatile – if an item isn’t in the cache for any reason, the application should be able to reconstruct itself seamlessly.  And 2) an item in the cache should expire such that no stale data retained.   (The SqlCacheDependency helps in many of these situations, but generally doesn’t apply in the cloud.)

The last part about stale data is pretty tricky in some situations.  Consider this situation:  suppose your web app has 4 servers and, on the the home page, a stock ticker for the company’s stock.  This is fetched from a web service, but cached for a period of time (say, 60 minutes) to increase performance.    These values will quickly get out of sync – it might not be that important, but it illustrates the point about keeping cache in sync.  A very simple way to deal with this situation is to expire the cache at an absolute time, such as the top of the hour.  (But this, too, has some downsides.)

As soon as you move into a more complicated scenario, things get a bit trickier.  Suppose you want to expire items from a web app if they go out of stock.   Depending on how fast this happens,  you might expire them based on the number in stock – if the number gets really low, you could expire them in seconds, or even not cache them at all. 

But what if you aren’t sure when an item might expire?   Take Worldmaps … storing aggregated data is ideal in the cache (in fact, there’s 2 levels of cache in Worldmaps).  In general, handling ‘when’ the data expires is predictable.  Based on age and volume, a map will redraw itself (and stats updated) between 2 and 24 hours.   I also have a tool that lets me click a button to force a redraw.   When one server gets this request, it can flush its own cache, but the other servers know nothing about this.   In situations, then, when user interaction can cause cache expiration, it’s very difficult to cache effectively and often the result is just not caching at all.

With all of this background out of the way, even though technologies like the SqlCacheDependency currently don’t exist in Azure, there are a few ways we can effectively create a distributed cache in Azure – or perhaps more appropriately, sync the cache in a Windows Azure project.

In the next post, we’ll get technical and I’ll show how to use the RoleEnvironment class and WCF to sync caches across different web roles.  Stay tuned!

Tags: , ,

Azure | Development

Upcoming Windows Phone 7 Events

by Brian Hitney 23. August 2010 13:13

Want to learn more about developing for Windows Phone 7?  Check out his event series coming soon!   The event is broken down into two parts:  the Firestarter event which runs all day, then an evening event called the Windows Phone Garage… learn more below, and the registration links for both events in all the cities is at the bottom of the post:

Morning Sessions:
· Introduction to Windows Phone Development and the WP7 platform
· Building Windows Phone 7 Applications with Silverlight
· Building Windows Phone 7 Applications using XNA

Lunch (included)
Afternoon Sessions:
· Monetizing Your Apps with Marketplace
· Windows Phone 7 Services
· Light Up Windows Phone 7

Windows Phone Garage| 6pm - 9pm

This evening event is the opportunity for you to get started on that next killer app for Windows Phone 7, or put the finishing touches on your masterpiece. The Windows Phone Garage for mobile application developers follows the daytime Firestarter event. As you are designing, writing & testing your apps onsite, we’ll provide Windows Phone 7 experts (both Microsoft and community) to help you get things right. These experts will also present “quick hit” talks throughout the evening, containing tips and tricks on their experiences writing Windows Phone apps. It's the perfect opportunity to design and implement that cool new app you've been dreaming about, so bring your ideas and get ready to code. To learn more about the Phone Garage event or to register, click on an event city near you. Hurry, space is limited!

For more information or to register,
visit > www.msdnevents.com/firestarter

OR CALL 1-877-MSEVENT

Windows Phone 7 Firestarter

Windows Phone 7 Garage

To register, select a city

To register, select a city

Atlanta, GA

8/24/2010

Atlanta,GA

8/24/2010

Waltham, MA

8/24/2010

Waltham, MA

8/24/2010

Birmingham, AL

8/26/2010

Birmingham, AL

8/26/2010

Tampa, FL

8/31/2010

Tampa, FL

8/31/2010

Ft. Lauderdale, FL

9/2/2010

Ft. Lauderdale, FL

9/2/2010

New York, NY

9/7/2010

New York, NY

9/7/2010

Raleigh, NC

9/8/2010

Raleigh, NC

9/8/2010

Chevy Chase, MD

9/21/2010

Chevy Chase, MD

9/21/2010

Charlotte, NC

9/21/2010

Charlotte, NC

9/21/2010

Philadelphia, PA

9/22/2010

Philadelphia, PA

9/22/2010

Pittsburgh, PA

9/28/2010

Pittsburgh, PA

9/28/2010

Farmington, CT

9/30/2010

Farmington, CT

9/30/2010

Tags: ,

Microsoft | Events | Windows Phone 7

@home: The Beginning

by Brian Hitney 23. July 2010 19:35

This post is part of a series diving into the implementation of the @home With Windows Azure project, which formed the basis of a webcast series by Developer Evangelists Brian Hitney and Jim O’Neil.  Be sure to read the introductory post for the context of this and subsequent articles in the series.

To give even more background than in the first post … way back in late March (possibly early April) Jim had the idea to start something we originally called “Azure Across America” … not be confused with AAA :).   If you put yourself in our shoes, Azure is a very difficult technology for us to evangelize.   It reminds me a little of what it was like to explain the value prop of “WPF/e” back when the first bits were made available, long before it took the name Silverlight.   Azure is obviously a pay for use model, so what would be an interesting app to build in a webcast series?  Preferably something that helps illustrate cloud computing, and not just a “hello world” application.

While we debated what this would look like (I’ll spare the details), the corp team solidified a trail account program that enabled us to get free trial accounts for attendees to the series.   This changed the game completely, because now we weren’t hindered by signup costs, deployment costs, etc.  In fact, the biggest challenge was doing something interesting enough that would be worth your time to deploy.

That’s when we had the idea of a distributed computing project.   Contributing back to a well-known distributed computing project would be interesting, useful, demonstrate cloud computing, and not be hindered by the constant fluctuation of apps going online and offline.   So now that we had the idea, which project would we choose? 

We also had a number of limitations in the Azure platform.  Don’t get me wrong:  Azure offers a number of strengths as a fully managed PaaS … but we don’t have administrator rights or the ability to remote desktop into the VMs.  In essence, we need whatever we deploy to not require admin access, and be xcopy deployable. 

Stanford’s Folding@home project was perfect for this.  It’s a great cause, and the console version is easy to work with.  What we wanted to do was put together a site that would, in addition to providing the details, how-to’s, and recordings, show stats to track the current progress … image

In the next posts, I’ll go over the site and some of the issues we faced when developing the app.

Tags: ,

Azure | Development

@home with Windows Azure: Behind the Scenes

by Brian Hitney 16. July 2010 06:17

As over a thousand of you know (because you signed up for our Webcast series during May and June), my colleague Jim O’Neil and I have been working on a Windows Azure project – known as @home With Windows Azure – to demonstrate a number of features of the platform to you, learn a bit ourselves, and contribute to a medical research project to boot.  During the series, it quickly became clear (…like after the first session) that the two hours was barely enough time to scratch the surface, and while we hope the series was a useful exercise in introducing you to Windows Azure and allowing you to deploy perhaps your first application to the cloud, we wanted (and had intended) to dive much deeper.

So enter not one but two blog series.  This introductory post appears on both of our blogs, but from here on out we’re going to divide and conquer, each of us focusing on one of the two primary aspects of the project.  Jim will cover the application you might deploy (and did if you attended the series), and I will cover the distributed.cloudapp.net application, which also resides in Azure and serves as the ‘mothership’ for @home with Windows Azure.  Source code for the project is available, so you’ll be able to crack open the solutions and follow along – and perhaps even add to or improve our design. 

Don't make us make you read this!You are responsible for monitoring your own Azure account utilization.  This project, in particular, can amass significant costs for CPU utilization.  We recommend your self-study be limited to using the Azure development fabric on your local machine, unless you have a limited-time trial account or other consumption plan that will cover the costs of execution.

So let’s get started.  In this initial post, we’ll cover a few items

    • Project history
    • Folding@home overview
    • @home with Windows Azure high-level architecture
    • Prerequisites to follow along

Project history

Jim and I have both been intrigued by Windows Azure and cloud computing in general, but we realized it’s a fairly disruptive technology and can often seem unapproachable for many of your who are focused on your own (typically on-premises) application development projects and just trying to catch up on the latest topical developments in WPF, Silverlight, Entity Framework, WCF, and a host of other technologies that flow from the fire hose at Redmond.   Walking through the steps to deploy “Hello World Cloud” to Windows Azure was an obvious choice (and in fact we did that during our webcast), but we wanted an example that’s a bit more interesting in terms of domain as well as something that wasn’t gratuitously leveraging (or not) the power of the cloud.

Originally, we’d considered just doing a blog series, but then our colleague John McClelland had a great idea – run a webcast series (over and over… and over again x9) so we could reach a crop of 100 new Azure-curious viewers each week.  With the serendipitous availability of ‘unlimited’ use, two-week Windows Azure trial accounts for the webcast series, we knew we could do something impactful that wouldn’t break anyone’s individual pocketbook – something along the lines of a distributed computing project, such as SETI

SETI may be the most well-known of the efforts, but there are numerous others, and we settled on one (http://folding.stanford.edu/, sponsored by Stanford University) based on its mission, longevity, and low barrier to entry (read: no login required and minimal software download).  Once we decided on the project, it was just a matter of building up something in Windows Azure that would not only harness the computing power of Microsoft’s data centers but also showcase a number of the core concepts of Windows Azure and indeed cloud computing in general.  We weren’t quite sure what to expect in terms of interest in the webcast series, but via the efforts of our amazing marketing team (thank you, Jana Underwood and Susan Wisowaty), we ‘sold out’ each of the webcasts, including the last two at which we were able to double the registrants - and then some!

For those of you that attended, we thank you.  For those that didn’t, each of our presentations was recorded and is available for viewing.  As we mentioned at the start of this blog post, the two hours we’d allotted seemed like a lot of time during the planning stages, but in practice we rarely got the chance to look at code or explain some the application constructs in our implementation.  Many of you, too, commented that you’d like to have seen us go deeper, and that’s, of course, where we’re headed with this post and others that will be forthcoming in our blogs.

Overview of Stanford’s Folding@Home (FAH) project

Folding@homeStanford’s http://folding.stanford.edu/ was launched by the Pande lab at the Departments of Chemistry and Structural Biology at Stanford University on October 1, 2000, with a goal “to understand protein folding, protein aggregation, and related diseases,” diseases that include Alzheimer’s, cystic fibrosis, CBE (Mad Cow disease) and several cancers. The project is funded by both the National Institutes of Health and the National Science Foundation, and has enjoyed significant corporate sponsorship as well over the last decade.  To date, over 5 million CPUs have contributed to the project (310,000 CPUs are currently active), and the effort has spawned over 70 academic research papers and a number of awards.

The project’s Executive Summary answers perhaps the three most frequently asked questions (a more extensive FAQ is also available):

What are proteins and why do they "fold"? Proteins are biology's workhorses -- its "nanomachines." Before proteins can carry out their biochemical function, they remarkably assemble themselves, or "fold." The process of protein folding, while critical and fundamental to virtually all of biology, remains a mystery. Moreover, perhaps not surprisingly, when proteins do not fold correctly (i.e. "misfold"), there can be serious effects, including many well known diseases, such as Alzheimer's, Mad Cow (BSE), CJD, ALS, and Parkinson's disease.

What does Folding@Home do? Folding@Home is a distributed computing project which studies protein folding, misfolding, aggregation, and related diseases. We use novel computational methods and large scale distributed computing, to simulate timescales thousands to millions of times longer than previously achieved. This has allowed us to simulate folding for the first time, and to now direct our approach to examine folding related disease.

How can you help? You can help our project by downloading and running our client software. Our algorithms are designed such that for every computer that joins the project, we get a commensurate increase in simulation speed.

FAH client applications are available for the Macintosh, PC, and Linux, and GPU and SMP clients are also available.  In fact, Sony has developed a FAH client for its Playstation 3 consoles (it’s included with system version 1.6 and later, and downloadable otherwise) to leverage its CELL microprocessor to provide performance at a 20 GigaFLOP scale.

As you’ll note in the architecture overview below, the @home with Windows Azure project specifically leverages the FAH Windows console client.

@home with Windows Azure high-level architecture

The @home with Windows Azure project comprises two distinct Azure applications, the distributed.cloudapp.net site (on the right in the diagram below) and the application you deploy to your own account via the source code we’ve provided (shown on the left).  We’ll call this the Azure@home application from here on out.

High level architecture of @home with Windows Azure

distributed.cloudapp.net has three main purposes:

  1. Serve as the ‘go-to’ site for this effort with download instructions, webcast recordings, and links to other Azure resources.
  2. Log and reflect the progress made by each of the individual contributors to the project (including the cool Silverlight map depicted below)
  3. Contribute itself to the effort by spawning off Folding@home work units.

@home with Windows Azure Project status pageSilverlight visualizer

 

 

 

 

 

 

 

I’ll focus mostly on this backend piece and other related bits and pieces, design choices, etc. 

The other Azure service in play is the one you can download from distributed.cloudapp.net (either in VS2008 or VS2010 format) – the one we’re referring to as Azure@home.  This cloud application contains a web front end and a worker role implementation that wraps the console client downloaded from the http://folding.stanford.edu/English/Download site.  When you deploy this application, you will be setting up a small web site including a default page (image the left below) with a Bing Maps UI and a few text fields to kick off the folding activity.  Worker roles deployed with the Azure service are responsible for spawning the Folding@home console client application - within a VM in Azure - and reporting the progress to both your local account’s Azure storage and the distributed.cloudapp.net application (via a WCF service call). 

@home with Windows Azure - start page@home with Windows Azure - status screen

 

 

 

 

 

 

 

 

 

Via your own service’s website you can keep tabs on the contribution your deployment is making to the http://folding.stanford.edu/English/Stats effort (image to right above), and via distributed.cloudapp.net you can view the overall http://fah-web.stanford.edu/cgi-bin/main.py?qtype=teampage&teamnum=184157 – as I’m writing this the project is ranked 583 out of over 184,000 teams; that’s in roughly the top 0.3% after a little over two months, not bad!

Jim will be exploring the design and implementation of the Azure@home piece via upcoming posts on my blog.

Prerequisites to follow along

Everything you need to know about getting started with @home with Windows Azure is available at the distributed.cloudapp.net site, but here’s a summary:

In addition to the core pieces listed above, feel free to view one of the webcast recordings or my screencast to learn how to deploy the application.  We won’t be focusing so much on the deployment in the upcoming blog series, but more on the underlying implementation of the constituent Azure services.

Lastly, we want to reiterate that the Azure@home application requires a minimum of two Azure roles.  That’s tallied as two CPU hours in terms of Azure consumption, and therefore results in a default charge of $0.24/hour; add to that a much smaller amount of Azure storage charges, and you’ll find that it’s left running 7x24, your monthly charge will be around $172!  There are various Azure platform offers available, including an introductory special; however, the introductory special includes only 25 compute hours per month (equating to12 hours of running the smallest version of Azure@home possible).

Most of the units of work assigned by the Folding@home project require at least 24 hours of computation time to complete, so it’s unlikely you can make a substantial contribution to the Stanford effort without leveraging idle CPUs within a paid account or having free access to Azure via a limited-time trial account.  You can, of course, utilize the development fabric on your local machine to run and analyze the application, and theoretically run the Folding@home client application locally to contribute to the project on a smaller scale.

That’s it for now.  I’ll be following up with the next post within a few days or so; until then keep your head in the clouds, and your eye on your code.

Tags: ,

Azure | Development

@home: Most Common Problems #1

by Brian Hitney 20. June 2010 18:37

Jim and I are nearly done with the @home with Azure series, but we wanted to document some of the biggest issues we see every week.  As we go through the online workshop, many users are deploying an Azure application for the first time after installing the tools and SDK.   In some cases, attendees are installing the tools and SDK in the beginning of the workshop.

When installing the tools and SDK, it’s important to make sure all the prerequisites are installed (available on the download page).  The biggest roadblock is typically IIS7 – which basically rules out Windows XP and similar pre-IIS7 operating systems.  IIS7 also needs to be installed (by default, it isn’t), which can be verified by going into the control panels / programs and features.

The first time you hit F5 on an Azure project, development storage and the development fabric are initialized, so this is typically the second hurdle to cross.   Development storage relies on SQL Server to house the data for the local development storage simulation.  If you have SQL Express installed, this should just work out of the box.  If you have SQL Server Standard (or other edition), or a non-default instance of SQL Server, you’ll likely receive an error to the effect of, “unable to initialize development storage.”

The Azure SDK includes a tool called DSINIT that can be used to configure development storage for these cases.  Using the DSINIT tool, you can configure development storage to use a default or named instance of SQL Server.

With these steps complete, you should be up and running!

Tags: , ,

Azure | Development | Events

Best Buy Employees: Hear Me

by Brian Hitney 18. June 2010 14:32

Today I made a fairly exciting trek to Best Buy to buy some printer ink.  I live on the edge!

While I was checking out and paying with my credit card, the cashier asked to see my ID.   I admit, I’ve shown it in the past, and it has always irritated me.  It doesn’t irritate me because it’s inconvenient to take my license out of my wallet, but rather, because it doesn’t do anything but put me at risk.

Merchant agreements (at least with Visa cards) basically say the merchants can’t demand to see any form of ID to verify the card holder if the card is signed (of course they can require it if you’re buying something that requires ID – like alcohol), but they can ask (and most people may assume that if they’re asking, they don’t have a choice).  The problem with asking for ID is that it exposes essentially any information on that ID – your name, address, height, weight, eye color, license number, date of birth, etc.    Now, we can argue the cashier (or anyone else within visual distance) couldn’t possibly remember any incriminating information, but that’s not the point.   Fundamentally, it exposes you to a greater risk of identity theft.

The clincher is this – I tried to explain in a friendly way why I was hesitant to show my ID, but the cashier was visibly aggravated and simply said, “I’m just trying to protect you.”  

I firmly believe that he believes this, but it’s just not the case.  In a credit card transaction, we have Visa, the bank who issued me the card, Best Buy (in this case), and me, all involved in this transaction.   In reality, he’s protecting Best Buy, not me.  Nothing wrong with that as an employee, but not at the expense of exposing my personal information.

On 2 occasions over the past 10 years, my credit card information has been compromised by someone – I never found out how because Visa won’t tell me, but they were pretty broad cases that happened to many people online so the presumption was an online retailer was compromised.    In any event, it was not really a problem – I signed a paper that said I didn’t make the charge, and the problem for me was gone.  It would be far worse if someone opened an account in my name or I was a victim of fully blown identity theft.   And the best way to avoid identity theft is to never expose your personal information.

In this case, if I were a criminal (craftily trying to forestall suspicion by buying printer ink for $30 instead of a $10k home theater) I would’ve refused to show ID and Best Buy would have to accept the sale anyway (if my signatures matched).   So all I’m doing is proving I’m me, and I already knew that. 

So what could the Best Buy cashier do?   Well, comparing signatures on the card is a pretty fool proof method --  nothing more is needed.  Next is applying common sense – I’m buying printer ink for $30.   Visa will automatically call me or block an ‘out of character’ transaction.    I used to work retail and the credit card machine would return a “Call” message instead of “Approved” in these cases.  If I fail the signature test, either call Visa or ask for an ID.

What happens if I was a criminal and was able to buy the ink?  Well, I’d notice this on my statement and refute the charge, at which time someone will be screwed – either Best Buy or the bank – I’m guessing it depends on what kind of evidence is produced and perhaps their agreements in place, I’m not sure.   

So if Best Buy or another merchant is so exposed, why accept Visa (or other) credit cards at all?   As I mentioned above, signatures are a pretty fool proof method.  But, the reason is getting more business.   Best Buy has the option to not accept Visa, but they have made the choice to accept the merchant agreement in doing so.   If Visa thought it was a problem, they’d change the agreement or put my picture on the card.

Should the cashier or any other merchant read this, my advice is to always be friendly – lose the chip on your shoulder if you have one.  I was politely trying to explain this and not give the guy a hard time (no one was in line behind me), so as long as I’m friendly, be open to the possibility I might know what I’m talking about.   In turn, it aggravated me that he was so aggravated.   I’ve been there myself, and I work with customers all the time today – fortunately the vast majority are great. 

Coincidentally, and to make this entry a little more apropos, while I was writing this I saw a Microsoft commercial for IE8 that talks about identity theft – check it out at http://ie8protects.com… it’s a “reality” style commercial where they set up a fake bank, and entice customers into a false sense of security – it was pretty funny actually, especially since I was writing this post!

How about you?  Do you care if merchants ask to see your ID?   If so, did this post change your mind?

Tags: , ,

Babble | Security

Windows Azure Guest OS

by Brian Hitney 17. May 2010 20:05

In a Windows Azure project, you can specify the Guest OS version for your VM.  This is done by setting the osVersion property inside the ServiceConfiguration file:

image

If you don’t specify a version explicitly, the latest Guest OS is chosen for you.  For production applications, it’s probably best to always provide an explicit value, and I have a real world lesson that demonstrates this!

MSDN currently has a Guest OS Version and SDK Compatibility Matrix page that is extremely helpful if you’re trying to figure out which versions offer what features.  I recently ran into a problem when examining some of my performance counters – they were all zero (and shouldn’t have been)!  Curious to find out why, I did some digging (which means someone internal told me what I was doing wrong).

In short, I had specified a performance counter to monitor like so:  "\ASP.NET Applications(__Total__)\Requests/Sec".  This had worked fine, but when I next redeployed some weeks later, the new Guest OS (with .NET Fx 4.0) took this to mean 4.0 Requests/Sec, because I didn’t specify a version.   So, I was getting zero requests/sec because my app was running on the earlier runtime.  This was fixed by changing the performance counter to "\ASP.NET Apps v2.0.50727(__Total__)\Requests/Sec".  

For more information on this, check out this article on MSDN.  And thanks to the guys in the forum for getting me up and running so quickly!

Tags: , ,

Azure | Development | Tech Tips

Scaling Down – Text Version

by Brian Hitney 17. May 2010 19:42

I caught some Flak this weekend at the Charlotte Code Camp when Justin realized my recent Scale Down with Windows Azure post was principally a screencast (aside from the code sample).   So Justin, I’m documenting the screencast just for you! :)

First, a good place to start with this concept is on Neil Kidd’s blog post.   Go ahead and read that now … I’ll wait.  Most of this code is based off of his original sample, I’ve modified a few things and brought it forward to work with the latest SDK.

So, in a nutshell, a typical worker role template contains a Run() method in which we’d implement the logic to run our worker role.  In many cases, there are multiple tasks and multiple workers.  Unless the majority of the work you are doing is CPU bound (which is entirely possible, as is the case with our Azure-based distributed Folding project), the resources of the VM can be better utilized by multithreading the tasks and workers.

The trick is to do this correctly as writing multithreaded code is challenging.  In general, parallel extensions is likely not the right approach in this situation.  There are some exceptions – for example, if you are using a 4-core (large) VM and require lots of parallel processing, PFx might be the best approach.  But that’s not often the case in the cloud.  Instead, we need a lightweight framework that allows us to create a number of “processors” (using quotes here to avoid confusion with a CPU) that are responsible for doing their work independent of any other “processors” in the current instance.  Each “processor” can run on its own thread, but the worker role itself, instead of doing the work, simply monitors the health of all of the threads and restarts them as necessary.

The implementation is not terribly complex – but if you aren’t comfortable with threading or just don’t want to reinvent the wheel, check out the base project.  Feel free to add to or modify the project as necessary.  Let’s step through some of the concepts.

Download the sample project (Visual Studio 2010) here.

First, it doesn’t matter if you implement this in a webrole or a workerrole.  A webrole exposes the same Run() method that a workerrole does, and it doesn’t interfere with the operation of hosting a website – aside from the fact that there are limited resources per VM of course.

First up is the IProcessMessages interface.  This interface is simple, basically saying our processors need to define how long they need per work unit, and expose a Process() method to call.   Our health monitor keeps tabs on the processor, so it needs to know how long to wait before assuming the processor is hung:

image

A simple processor is then very easy to create.  We just implement the IProcessMessages interface, and code whatever logic we need our worker to do inside the Process() method.  We’re specifying that this processor needs only 20 seconds per work unit, so the health monitor will restart the worker in the event it doesn’t see progress when 20 seconds elapse.  SyncRoot isn’t needed unless you need to do some locking:

image

So far, pretty simple.  Our processor doesn’t need to be aware of threading, or handling/restarting itself.   The ProcessRecord class does this for us.  It won’t do the actual monitoring, but rather, implements the nuts and bolts of starting the thread for the current processor:

image

When the ProcessorRecord class is told to start the thread, it calls a single Run() method passing in the processor.  This method will essentially run forever, calling Process() each iteration.  Since we’re not getting notified of work, each processor is essentially polling for work.  Because of this, a traditional implementation is to say if there is work to do, keep calling Process() as frequently as possible, but if there’s no work to do, sleep for some amount of time:

image

The current implementation is simple – it doesn’t do exponential back off if there’s no work to do, it just sleeps for the amount of time specified in the ProcessorRecord.  That leaves us with one more task, and that’s defining our processors in the web/worker role Run() method.  The nice thing about this approach is that it’s quite easy to add multiple instances to scale up or down as needed:

image

In the case above, we’re creating 2 processors of the same type, giving them different names (helpful for the log files), the same thread priority, and a sleep time of 5 seconds per iteration if there’s no work to do.  In the Run() method, instead of doing any work, we’ll just monitor the health of all the processors.  Remember, the Run() method shouldn’t exit under normal conditions because it will cause the role to recycle:

image

It may look complicated, but it’s pretty simple.  Each iteration, we’ll look at each Processor.  The Timeout is calculated based on the last known “thread test” (when the thread was last known to be alive and well, plus any process time or sleep time adjustments.  If that time is exceeded, a warning is written to the log file and the Processor is reset.  Worldmaps has been using this approach for about 6 months now, and it’s been flawless.

Is this the most robust and complete framework for multithreading worker roles?  No.  It’s a prototype – a good starting place for a more robust solution.  But, the pattern you see here is the right starting point:  the role instance itself knows what processors it wants, but doesn’t concern itself with their implementation or threading details.  Each ProcessorRecord will execute its processor, and implements the threading logic, without regard to the other processors or the host.  The Processors don’t care about threading, other processors, or the host, it just does its work.  This separation of concerns makes it easy to expand or modify this concept as the application changes.

If you’re trying to get more performance out of your workers, try this approach and let me know if you have any comments.

Tags: ,

Azure | Development | Microsoft

Scaling Down with Windows Azure

by Brian Hitney 12. May 2010 09:56

Awhile back, Neil Kidd created a great blog post on scaling down in Windows Azure.  The concept is to multithread your Azure worker roles (and web roles, too) – even if you have a single core (small instance) VM, most workloads are network IO blocked, not CPU blocked, so creating a lightweight framework for supporting multiple workers in a single role not only saves money, but makes sense architecturally. 

In this screencast, I updated Neil’s concept a bit, and bring it forward to work with the latest Azure SDK:

Get Microsoft Silverlight

Download the sample project (Visual Studio 2010) used in this screencast here.

Tags: , ,

Azure | Development

Finally, at last, done with Webhost4Life

by Brian Hitney 11. May 2010 10:18

I’ve recently completed my migration from Webhost4Life!  Woohoo!  For a long time, I’ve felt a bit captured because they’ve hosted my blog, email for a few domains, etc.  It’s difficult to make the move.

I’ll say this upfront:  if you need $10/mo hosting, it’s hard to beat and easier than hosting at home.  So right away, before I slam webhost4life too much, I understand the cheap price and you can’t really expect the moon.  If you’re hosting your kid’s sports league site, the neighborhood website, etc., it’s a nice option.  But it ends there.

I've been using Webhost4Life since 2004, so I’m a long time customer.  I’ve had a few speed bumps along the way (like getting shut down with no notice due to high volume when I hosted Worldmaps on the site), or arguing over using too much file space when I was still way under the account limit.  Webhost4Life also went through a migration back in February that was a bit painful.  This past weekend, there was a solid 48-72 hour outage that, due to the more than 48 hour response time after I submitted the ticket, it was simply easier to accept the outage and migrate elsewhere.

What bothered me about this outage in particular was that it was clear my files were being messed with.  For example, each of my sites looked a little like this:

image

Here’s another:

image

Obviously I last deployed the site back in the migration in late Feb.  I noticed the outage late Friday, 5/7/2010, and it was probably down most of the day.  So sometime earlier that day, my web.config files were messed with.  Not only that, but each of my sites (some of which were legacy, and had no App_Theme / Ajax had these added:

image

And, even further, my global.asax.cs file was modified and namespace was changed, as if updated by a tool or opened in VS.  Many folders had an App_Themes folder – all new.  Things get a tad more interesting when I crack into specifically what was changed (this is what leads me to believe it’s Webhost4Life, not a hacker, making the changes).  First, this is what my web.config looked like locally … I use 3 databases.  The default/logging databases are the same, the warehouse is my local server (at home) where I store archives of the logging data, where the SQL Server is on the same box:image

Once deployed, the Warehouse server isn’t used at all.  I just keep the setting there so the settings are side by side.  When I opened the modified file, I saw this:

image

My connection strings were modified!  First up, my Warehouse setting was something I only used locally at home – it seems some tool has likely replaced it.  Also, the sql399 site was replaced with VCNSQL86 connection.  When I log into the Webhost4Life control panel, I see that VCNSQL86 is the correct server name – obviously at some point, the name was changed from sql399.  

I don’t have a problem with name changes, but I do have a problem with the files being changed for me and not being notified of a change.  In fact, I think a better approach is to just let my app die than modify it for me.  Shared hosting or not, I think someone going into the files without explicit permission is a violation.  Besides, like most developers, I work locally and FTP my changes to the site, so any changes they have would be overwritten next time I deploy.

Even though I redeployed the applications, all sites were still broken.  Something still wasn’t working … the subdomain to folder feature wasn’t working correctly which prevented the sites from starting.  I know the site was working Thursday, so it had to be related to all these changes.   After more than 48 hours, I did finally get a reply back on my original ticket, and the reply sums it up: 

I have checked the domain 'structuretoobig.com' and sub domain 'blog.structuretoobig.com' and noticed that it is not pointing to our server.

I’ve asked for more details, so we’ll see what happens. 

Tags: , ,

Rant | Reviews

your host...

Brian Hitney
Developer Evangelist
Microsoft Corp.

About Me

My Worldmap