Get Cloud Ready!

Janakiram MSV

Subscribe to Janakiram MSV: eMailAlertsEmail Alerts
Get Janakiram MSV via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Blog Feed Post

Understanding Windows Azure Runtime: Part II

In part I of the article, I introduced the concepts and terminology of Windows Azure runtime. In this part, we will take a closer look at the Roles and the way to control the lifecycle of the Roles.

Most of the modern applications are logically partitioned into individual layers. Each layer is responsible for a specific function. UI rendering, business logic, data access and database are the common layers of these applications. These layers may or may not be running on dedicated machines. For maximum scalability, each layer can run on a cluster of machines or all these layers can be running on the same machine. Windows Azure Roles are modeled around these application layers or tiers. Each role instance is actually a dedicated Azure VM. Based on the role, the VM is customized for specific tasks.

Let’s understand more about Windows Azure Roles.

  • Web Role– This is a VM that is designed to primarily host web applications. Whatever layer you host on IIS can be easily moved into the Web Role. As of now, Web Role runs the Hosted Web Core (HWC) to host web applications. That’s the reason why you cannot host multiple websites within one Web Role. Going forward, Web Role will support IIS 7.x and this brings parity with the on-premise deployment scenarios. Web Role can also host WCF services and CGI applications like PHP and Perl. They only support HTTP/S based communication.
  • Worker Role – This Role is typically mapped to the application tier or the business logic layer of the application. Though Worker Roles can expose TCP, HTTP/S endpoints, they are mostly reached via the Web Role. Worker Role will never have any UI and does the ‘heavy lifting’ required by the application. They can execute external processes and act as a surrogate process for legacy executables. Worker Roles are used to bootstrap other runtimes like Java and Python. They can also host databases like MySQL or MongoDB and expose the ports of these database servers as external endpoints.
  • VM Role – This is the new Role that got announced at PDC 2010. This is used to host legacy applications that do not fit into Web Role or Worker Role. We will get more details once the VM Roles go into the CTP mode. Since it is too early to include this in the discussion, I am not going to discuss this in detail.

Lifecycle of Role Instances

The lifecycle of the role is managed by the Windows Azure runtime. There are certain implicit and explicit factors that may force the runtime to reboot or re-launch the role instances. Implicit factors are internal to Azure environment and explicit factors are those that are initiated through the change in configuration files or by performing an in-place upgrade. As developers, it is important for us to know when the instance is going to be recycled and how to gracefully handle these changes.

Worker Roles implement an abstract class called RoleEntryPoint. This is optional for Web Roles but is mandatory for Worker Roles. RoleEntryPoint is the prescriptive interface between the Windows Azure Runtime and the Role instances. This interface has three methods – 1) OnStart(), 2) Run() and 3) OnStop(). During the initialization of a Role, the OnStart() method is called. To ensure that the Role is ‘awake’ all the time, there is a while(true) construct in the Run() method. This is comparable to the static void main() method in a classic C# program. After exiting from the Run() method, the Windows Azure environment calls the OnStop() method. This is where the cleanup code will be placed. Implementing RoleEntryPoint class is optional in a Web Role because of a few reasons. Web Roles function in a typical Request/Response pattern and may not long running processes. Web Roles also rely on ASP.NET lifecycle management methods for initialization and clean up. Technically, Web Roles can be forced to act like Worker Roles by implementing the RoleEntryPoint class and overriding the required methods. This is particularly useful in scenarios where an extra Worker Role is not required and the Web Role can easily manage few other tasks including processing messages. Steve Marx has written an excellent post discussing the scenario for combining roles.

By overriding the methods of RoleEntryPoint we can make Worker Roles very efficient and intelligent. Within the OnStop() method, the state of the Role can be dumped into a Azure Table. During the startup phase of the Worker Role, we can read the previous state of the application from an Azure Table and continue from the same point where the last execution was left. It is entirely possible to externalize the tasks of Worker Role to Azure Storage and every time the Worker Role boots, it will learn what it is supposed to execute by querying an external state machine. This will be the basis of building efficient Map/Reduce scenarios on Windows Azure.

Generic Worker Role

Windows Azure Runtime also offers mechanism to intercept and control the reboot of the Roles. This done by subscribing to the RoleEnvironment.Changing, RoleEnvironment.Changed and RoleEnvironment.Stopping events. By detecting the factor that forced the event to take place, we can either reboot and cancel the reboot and continue with the process. I will cover the factors and conditions that typically force a reboot in a separate article.

I want to summarize this article with a few tips on leveraging the RoleEntryPoint and RoleEnvironment classes –

  • OnStart()
    • Wire-up event handlers in the OnStart() method
    • Initialize Azure storage and perform mandatory check of resources
    • Initialize Windows Azure Diagnostics
  • Run()
    • Never exit the Run() method as the OnStop() method is called immediately and the Role will be terminated
    • Try to parallelize and multithread the operations in the Run() method
  • OnStop()
    • Only perform quick cleanup in OnStop() that doesn’t take too long
    • The method must return within 30 sec. or there will be a forced shutdown

Hope you found this article useful! If you have any questions/queries, feel free to drop a comment.

Read the original blog entry...

More Stories By Janakiram MSV

Janakiram MSV heads the Cloud Infrastructure Services at Aditi Technologies. He was the founder and CTO of Get Cloud Ready Consulting, a niche Cloud Migration and Cloud Operations firm that recently got acquired by Aditi Technologies. In his current role, he leads a highly talented engineering team that focuses on migrating and managing applications deployed on Amazon Web Services and Microsoft Windows Azure Infrastructure Services.
Janakiram is an industry analyst with deep understanding of Cloud services. Through his speaking, writing and analysis, he helps businesses take advantage of the emerging technologies. He leverages his experience of engaging with the industry in developing informative and practical research, analysis and authoritative content to inform, influence and guide decision makers. He analyzes market trends, new products / features, announcements, industry happenings and the impact of executive transitions.
Janakiram is one of the first few Microsoft Certified Professionals on Windows Azure in India. Demystifying The Cloud, an eBook authored by Janakiram is downloaded more than 100,000 times within the first few months. He is the Chief Editor of a popular portal on Cloud called www.CloudStory.in that covers the latest trends in Cloud Computing. Janakiram is an analyst with the GigaOM Pro analyst network where he analyzes the Cloud Services landscape. He is a guest faculty at the International Institute of Information Technology, Hyderabad (IIIT-H) where he teaches Big Data and Cloud Computing to students enrolled for the Masters course. As a passionate speaker, he has chaired the Cloud Computing track at premier events in India.
He has been the keynote speaker at many premier conferences, and his seminars are attended by thousands of architects, developers and IT professionals. His sessions are rated among the best in every conference he participates.
Janakiram has worked at the world-class product companies including Microsoft Corporation, Amazon Web Services and Alcatel-Lucent. Joining as the first employee of Amazon Web Services in India, he was the AWS Technology Evangelist. Prior to that, Janakiram spent 10 years at Microsoft Corporation where he was involved in selling, marketing and evangelizing the Microsoft Application Platform and Tools.