Thursday, May 17, 2012

Autoscaling Application Blocks


Autoscaling Application blocks can automatically scale the Windows Azure application based on the rules defined specifically for the application. The Autoscaling application Block supports two autoscaling mechanisms:

1)      Instance Autoscaling, where the block changes the number of role instances based on constraint and reactive rules.
2)      Throttling, where the application modifies its own behavior to change its resource utilization based on set of reactive rules. For example switching off non-essential features, or gracefully degrading its UI.

So, there are two types of rules:

1)      Constraint rules: Constraint rule set the upper and lower bounds on the number of instances. For example, in evening between 6:00 and 8:00, you need a minimum of 3 instances and a maximum of 7 instances, then use constraint rule.
2)      Reactive rules: Reactive rule enables the number of role instances to change in response to unpredictable changes in demand. For example, if workload increases then increase the number of role instances by 1. The reactive rules can use a variety of techniques like performance counters, or windows azure queue length to monitor and control application’s workload. A reactive rule makes changes to the number of role instances only if a constraint rule applies at the same time. It is easy to create a default constraint rule that always applies.

While defining target of an autoscaling rule, you can identify a scale group instead of an individual role. A Scale group enables you to define autoscaling rules that target multiple roles. A scale group can have any number of roles.

Following is an example rule xml. In this there are two constraint rules. One is always active and default constraint. While other only become active at peak time daily at 6 for 2 hours, and overrides the default rule. There are two reactive roles:  One will increase the instance count by 1 if the average CPU power usage for last 30 minutes is over 70%, while the other one decrease the instance count by 1 if the average CPU usage for last 30 minutes is less than 30%.

<rules xmlns="http://schemas.microsoft.com/practices/2011/entlib/autoscaling/rules" enabled="true">
<constraintRules>
       <rule name="Default" description="Always active" enabled="true" rank="1">
              <actions>
                     <range min="2" max="5" target="RoleA"/>
              actions>
       rule>

       <rule name="Peak" description="Active at peak times" enabled="true" rank="100">
              <actions>
                     <range min="3" max="7" target="RoleA"/>
              actions>
              <timetable startTime="06:00:00" duration="02:00:00">
                     <daily/>
              timetable>
       rule>
constraintRules>

<reactiveRules>
       <rule name="ScaleUp" description="Increases instance count" enabled="true" rank="10">
              <when>
                     <greater operand="Avg_CPU_RoleA" than="70"/>
              when>
              <actions>
                     <scale target="RoleA" by="1"/>
              actions>
       rule>
       <rule name="ScaleDown" description="Decreases instance count" enabled="true" rank="10">
              <when>
                     <less operand="Avg_CPU_RoleA" than="30"/>
              when>
              <actions>
                     <scale target="RoleA" by="-1"/>
              actions>
       rule>
reactiveRules>

<operands>
       <performanceCounter alias="Avg_CPU_RoleA" performanceCounterName="\Processor(_Total)\% Processor Time" aggregate="Average" source="RoleA" timespan="00:30:00"/>
operands>
rules>

Conflicting Rules
1)      Conflicting Constraint and Reactive rules: A constraint rule always overrides a reactive rule.
2)      Conflicting Constraint rules: If two or more constraint rule includes timetables that specify they are active at the same time then
a)      The rule with highest rank is given priority.
b)       If two or more constraint rules of same rank conflict, then block will perform the action from first constraint rule it finds.
3)      Conflicting Reactive rules: If two or more reactive rules results in conflicting changes to number of role instances then
a)      The rule with highest rank is given priority.
b)      If two or more reactive rules of same rank conflict, then if any rule suggests increase in number of instances, then largest increase is used.
c)       If two or more reactive rules of same rank conflict, then if any rule suggests decrease in number of instances, then lowest decrease is used.
For example if one rule suggest increase the no. of instances by one, another suggest increase the number by two, and another suggest decrease the number by one, then the number will increase by two. Another example, if one rule suggest decrease in number of instances by one, another suggest decrease in number by three, then number of instances will decrease by one.
4)      Conflicting actions on scale group: It is possible that multiple rules could suggest different scaling actions on same target at same time, either because same role is member of different scale group or so. In that case, it uses same logic as it is used in conflicting reactive rules.

Tuesday, May 15, 2012

Queue based messaging in Windows Azure

A typical messaging solution exchanges data between its distributed components using message queues includes publishers publishing messages into queues and subscribers intended to receive messages. The subscriber can be implemented as single- or multi- threaded process, either continuously running or initiated on demand.
At higher level there are two primary queuing mechanism used to enable queue listener (receiver) to receive messages stored on a queue:

Polling or Poll- based model: A listener monitors a queue by checking the queue at regular intervals. A listener is a part of a worker role instance. Main processing logic is comprised of a loop in which messages are dequeued and dispatched for processing. The listener checks for messages periodically. The queue is polled until listener is notified to exit the loop. Windows Azure pricing model measures storage transactions based on requests performed against the queue, regardless if the queue is empty or not.

Triggering or Push- based model: A listener subscribes to an event triggered either by the publisher or by queue service manager, whenever a message arrives on a queue. Then the listener dispatched the message for processing. So, it does not have to poll the queue in order to determine whether any new work is available or not. A notification can be pushed to the queue listeners for every new message, or when the first message arrives to an empty queue, or when queue reaches a certain level. While using Windows Azure Service Bus volume of messaging entities like queue or topics should be considered.

Best Practices for Optimizing Transaction Costs
In queue-based messaging solution, the volume of storage transactions can be reduced using combination of the following methods:
  1. Group related messages into a single larger batch, and compress and stores the compressed image in the blob storage, while keeping a reference of blob in queue.
  2. Batch multiple messages together in a single storage transaction. The GetMessages method in the Queue Service API enables de-queuing the specified number of messages in a single transaction.
  3. While polling, avoid aggressive polling intervals and implement a back-off delay that increases the time between polling requests if a queue remains continuously empty.
  4. Reduce the number of queue listeners – when using a pull-based model, use only 1 queue listener per role instance when a queue is empty. To further reduce the number of queue listeners per role instance to zero, and use a notification mechanism to instantiate queue listeners when the queue receives work items.
  5. If queues remain empty for most of the time, automatically scale down the number of role instances and continue to monitor relevant system metrics to determine if and when the application should scale up the number of role instances to handle increasing workload.
  6. Using a combination of polling and push-based notifications, enabling the listeners to subscribe to a notification event (trigger) that is raised upon certain conditions to indicate that a new workload is put on queue.

Friday, May 11, 2012

Windows Azure Service Bus


Windows azure service bus provides infrastructure for communication, event distribution, naming and service publishing.

Azure Service bus features:
  1. It provides connectivity option for WCF and other service endpoints (including REST endpoints) running behind firewalls and NAT routers, bound to frequently-changing or dynamically-assigned IP addresses.
  2. It enables secure inbound communication from devices outside the firewall.
  3. It provides relayed messaging capabilities. The relay service supports direct one-way messaging, request/response messaging, full duplex messaging, and peer-to-peer messaging.
  4. It provides brokered (asynchronous) messaging capabilities. It supports publish-subscribe and temporal decoupling, i.e., sender and receiver don’t have to be online at same time. It reliably stores message until receiving party is ready to receive message. The core components of brokered messaging are- Queues, Topics and Subscriptions.
  5. It provides a global namespace system that is location- independent, i.e., the name of service in the service bus provides no information about the final destination of the communication.
  6. Provides a service registry for publishing and discovering service endpoint references in a service namespace.
  7. It builds and hosts service endpoints that supports:
a)      Exposing a web service to remote users. Expose and secure a local web service in the cloud without managing any firewall or NAT settings
b)     Eventing behavior. Listen for notification on any device, anywhere in the world.
c)      Tunneling between any two endpoints to enable bidirectional streams.

                                                                                                                                                   

Tuesday, December 27, 2011

Deploying on Windows Azure


In the previous article we have seen how to create a package for deployment. Now in this article we will see how to deploy it in azure.

1)      Open the management portal https://windows.azure.com
2)      Click on the Hosted Services, Storage Accounts & CDN tab, and then click Hosted Services, and click New Hosted Service.

3)      Now select the subscription. Enter the name of the service and url prefix. Then select a region  and do not deploy option. And then click OK. We will create deployment after creating the service.

4)      It will start creating hosted service. Once the hosted service is created. You can add certificate and deployment to the service.
5)      Now select the Certificates in the newly created hosted service and click Add certificate. Now add the certificate that is used to create remote desktop connections while creating the deployment. Select the certificate and enter password and click OK.

6)      Now select the service and click New Staging Deployment.


7)      Now enter name of the deployment. Browse for the package file and configuration file and click OK. By default when you package a solution, visual studio will place these file under bin -> Debug -> app.Publish folder.

8)      It will start deployment. Once the deployment is ready you can browse to the DNS name of the deployment and can see it.

Creating the Service Model for VM role in Windows Azure


Now in this article we will create a service model for the image uploaded in azure.

1)      Start Visual Studio 2010. Create new project. From the Visual C# template, click Cloud. Give a name to the project and click OK.

2)      Now Click OK, without selecting any role.

3)      Now add a new VM role. For this right click the Roles select Add and then select New Virtual Machine Role.


In case if didn’t find the “New Virtual Machine Role”, you have to run the scripts below to add new registry key or just change the following [dword] registry key: 
HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Windows Azure Tools for Microsoft Visual Studio 2010\1.0\VirtualMachineRoleEnabled​=1
4)      Now in properties of the VMRole, select Virtual Hard Disk tab, and select an account for the VHD. If you are using it for first time then you will not find any account there, and either you have to import or manage it.

5)      Now click on manage, It opens a window and then click New.

6)      Now select a certificate

7)      You can either choose an existing certificate, or can create a new one. To create a new certificate click “Create”. It asks for a friendly name. Give name and click OK.
8)      You can view the certificate. Just select the certificate and click View.

9)      You can install the certificate and create a .pfx for it. Just click on the Install.

10)   Now you have to upload the newly created certificate to the management portal. For this click “Copy the full path”. Now click on the “Window Azure Portal”

11)   On the portal, click on the Add Certificate.

12)   Now select the certificate, by pasting the path of the certificate. And click OK.


13)   Now enter the subscription Id on which certificate is uploaded and click OK.

14)   Now select the account and click Close.

15)   Now it will try to get the vhd’s of the account. Then select the VHD uploaded.

16)   Now configure endpoints to allow external connection. For this click on the Endpoints tab, and then click Add Endpoint. Now define Http as the protocol and port 80 for both public and private ports.

17)   Now configure the remote desktop connection for the role. Right click the VM role solution and click Package…


18)   Now click the enable Remote desktop for all roles.


19)   Now here select an existing certificate or you can create a new one. And enter username and password for remote desktop connection. Also select the expiration date. And then click OK.

20)   Now click on the Package.

21)   It creates the .cspkg and .cscfg file for deployment. And open the location in windows explorer.

Uploading the Disk Image to Windows Azure


In this article we will see how to upload .vhd image to Windows Azure. You cannot upload image directly from the portal, but it can be done through command prompt. To upload image

1)      Open Windows Azure SDK Command Prompt as an administrator from Start -> All Programs -> Windows Azure SDK v1.x
2)      At command prompt execute the following command
 csupload Add-VMImage -Connection "SubscriptionId=; CertificateThumbprint=" -Description "Base image Windows Server 2008 R2" -LiteralPath "" -Name baseimage.vhd -Location
Where
·         is your subscription Id
·         is the thumbprint of the certificate on the management portal.
·         Set the description as Base image Windows Server 2008 R2
·         is the full path of the .vhd file.
·         Set the name of the image as baseimage.vhd. So, that the name of the image on portal will be baseimage.vhd
·         set the location where the hosted service will be deployed. The location can be “East Asia”, “North Central US”, “North Europe”, “South Central US”, “Southeast Asia”, “West Europe”.
3)      In the Windows Azure VHD Verification Tool dialog, click OK to allow the VHD to be mounted.

4)      Now it mounts the .vhd file and verifies it. In this process it generates a compressed file (.preped file) in the same folder as the original image file.


5)      After that it creates a new blob for uploading the image file and starts uploading the compressed image to the azure account. It will take significant amount of time. After completion it shows the details.


6)      Once the image is uploaded successfully it shows the status of the image as Committed.


Installing Windows Azure Integration Components on VM role


In this article we will see how to install integration components on the virtual machine. The integration components must be installed on the machine before base image is uploaded to the Azure. The integration components start each time the OS starts. It handles the integration between the role instance and Azure environment. Windows Azure Integration components support:

·         Windows setup for deployment
When a VM role instance comes online, the generalized image that is used to create the instance must run through a setup specialization phase. This specialization phase is performed by the System Preparation Tool (sysprep), provided by the Windows Azure Integration Components.  To automate the specialization, it creates an answer file named c:\unattend.xml.
·         Interaction with the Windows Azure environment
It installs the Service runtime API’s, so that the VM role can get information about the Azure environment, like instance ID, upgrade domain etc. This information is only available to the code that is running under an Administrator or LocalSystem account in the VM role instance.
·         Local storage resources
You can access the local resources using the Service runtime API’s or at c:\Resources\.
·         Certificate installation
You can install the certificates to the LocalMachine store location. It is not possible to install certificates to the CurrentUser store location or any other store location. Do not install the certificate before uploading, because while generalizing the image, System Preparation Tool (sysprep) will destroy the private key of the certificate.

Now we will see how to install Integration components on VM role.
1)      From the Hyper-V manager, right click the VM role and click Connect, and start it. Now Click on the Media -> DVD Drive -> Insert Disk…

2)      Now open the location for wavmroleic.iso. It will be located at C:\Program Files\Windows Azure SDK\v1.6\iso.

3)      It opens an AutoPlay dialogue. Now click Open folder to view files using Windows Explorer.

4)      Now click double click the WaIntegrationComponents-x64.msi (Windows Installer) file.

5)      Click next on the Setup wizard.

6)      Now at Operating system Configuration wizard, enter Password and click Next.

7)      Now Click Install.

8)      If it asks for installing the device software, click Install.

9)      It will take some time for installation. When the installation completes, click Finish.

10)   Now it will ask you to restart the system. Click Yes to restart the system.

11)   Now launch the System Preparation Tool from the location C:\system32\sysprep\sysprep.exe. Set the System Cleanup Action to “Enter System Out-of-Box Experience (OOBE)”, check the option labeled Generalize, set the Shutdown Options to Shutdown, and then press OK.

12)   Now the image is ready for deployment.