This project is read-only.

MultiThreadedWorkerRole project

The MultiThreadedWorkerRole project is the worker role in the AzureProviders solution.

Multi-threaded role

The MultiThreadedWorkerRole project's job is to spawn a new thread for each job that is done. If you create a standard Azure worker role only a single thread is in use, this is a huge waste of resources as the server instances that the worker roles run on have processors with many cores that are capable of processing many threads asynchronously, MultiThreadedWorkerRole worker role counters his by spawning one thread for each role.

See http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2010/12/running-multiple-threads-on-windows.html for information on how this multi-threaded worker role software design pattern works.

Back-off pattern

The MultiThreadedWorkerRole project inherits a Back-Off design pattern from ThreadedWorkerRoleLib, this pattern causes the System.Threading.Thread.Sleep() to double after each time the job was unemployed. Since you pay per processing hour on Azure this design pattern saves you money.

Since some jobs are more important than others you can specify the maximum back-off time. As an example, sending an e-mail immediately may not be so important, the user gladly can wait a minute, but charging someone's credit card should happen immediately.

app.config

app.config is an application configuration file.

<configuration><appSettings>

Key/value's:
  • NumberOfSendEmailRetries; (default=3) Number of send an e-mail attempts before giving up
  • CheckDeadThreadSleepInSeconds; (default=4) Number of seconds to sleep after checking that all the worker role threads are up and running
  • QueueDequeueRetrySleepSeconds; (default=3600) Number of seconds to wait before retrying if a message was dequeued
  • EmailWorkerMaxThreadSleepSeconds; (default=30) When using the Back Off pattern never let the e-mail worker thread sleep more than this number of seconds
  • SessionStateWorkerMaxThreadSleepSeconds; (default=60) When using the Back Off pattern never let the session-state worker thread sleep more than this number of seconds
  • ApplicationName; (default="MyApp1") Name of application, should match the one in the WebPages web.config

<configuration><system.net><mailSettings>

Contains the E-mail SMTP server settings. You must change this to your own e-mail server provider, if you do not have one the you can use SendGrid (http://sendgrid.com) which allows you to send up to 200 e-mails a day for free.

<configuration><system.net><connectionManagement>

By default ASP.NET only allows 2 concurrent client requests, this is really inefficient as your Azure server instances has capabilities of processing many more simultaneous asynchronous requests. Because of this AzureProviders accepts 48 concurrent client requests by default, but you should really try and find the setting that works best for your solution. See http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83 for more information on the subject.

<configuration><system.net><settings>

By default ASP.NET uses the HTTP Expect 100-Continue pattern, which can be a performance issue if your HTTP operations are small, because of this AzureProviders has turned HTTP Expect 100-Continue off. See http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/d84ba34b-b0e0-4961-a167-bbe7618beb83 for more information on the subject.

EmailWorker.cs

The EmailWorker class listens to the Azure e-mail queue for e-mails that is to be sent. EmailWorker will try to send the e-mail as many times as specified in the application setting NumberOfSendEmailRetries before giving up.

Since EmailWorker inherits WorkerEntryPoint it will run in its own thread.

SMTP server

The SMTP settings is found in the mailSettings section of app.config.

MessageWorker.cs

The MessageWorker class is an example of how a minimal job is defined in AzureProviders. A job defined like this would run in its own thread.

Since MessageWorker inherits WorkerEntryPoint it will run in its own thread.

SessionStateWorker.cs

The SessionStateWorker class clears any expired ASP.NET Membership sessions from the SessionStateEntity table.

Since SessionStateWorker inherits WorkerEntryPoint it will run in its own thread.

WorkerRole.cs

The WorkerRole class is the entry point for the Azure infrastructure process, it initializes the role process.

The WorkerRole.GetWorkerRoles() method is where you define which jobs you want to run in the MultiThreadedWorkerRole worker role, by default the EmailWorker and SessionStateWorker jobds are defined here, you can add your own here. All jobds will run in their own thread, separate from the other roles in the MultiThreadedWorkerRole worker role. You can comment in MessageWorker to experiment with adding a new job.

WorkerRole inherits the abstract ThreadedWorkerRoleLib.ThreadedRoleEntryPoint which handles most of the logic for the job.

EmailWorkerMaxThreadSleepSeconds and SessionStateWorkerMaxThreadSleepSeconds settings in app.config sets the maximum time to thread sleep using the Back-Off pattern.

Last edited Oct 2, 2011 at 2:30 PM by ihenriksen, version 5

Comments

No comments yet.