What is causing Thread Contentions with my Azure Queue code?
I just ran the Visual Studio 2010 Thread Profiler against my Azure code and noticed I'm getting a LOT of contentions. My code is blocked more often than it is running!
My worker has no custom threading logic in it at all. It is a simple While loop which asks various queues if they have work. I make an instance of the QueueRepositoryClass and then invoke the GetQueueMessage function.
It looks like that for some reason multiple threads are hitting my GetQueueMessage method and are using the same queueClient instance. Perhaps its开发者_C百科 a result of me rusing the CloudStorageAccount object? Or is it the CreateCloudQueueClient extension method?
Without seeing the calling code, this is partly speculation, but it would appear that you are calling GetMessage() in a fairly tight loop. GetMessage is a synchronous call, doing so will make that particular thread block itself while waiting on a response from the queue (other threads continue to run). In a profiler, this will look really bad b/c the rest of the loop logic executes so quickly (i.e. you spend a ms or two executing code & 50ms getting a message, will look exactly like that).
One other note, you need to be really careful querying the queue in tight loops for several reasons:
1) Though the per Tx charge is small, it isn't 0. I usually suggest some back off logic - i.e. if the queue is empty, wait 100ms before checking again. Empty twice, back off to 500ms, etc. Obviously you have to balance that w/the User Experience you are going for.
2) If you end up scaling the application - you effectively create a DoS attack against the queues. 100 instances all pounding a queue in a tight loop can really hurt perf.
Pat
I switched to use the Async QueueClient methods and the whole system performs much better now.
精彩评论