Monday, February 04, 2008

One of the most powerful features of the Windows Workflow Foundation is its ability to automate state management of long running processes using a persistence service. Microsoft supply a persistence service out of the box - the SqlWorkflowPersistenceService. Unsurprisingly this persists state in SQL Server (or SQL Express).

Once you have a persistence service more possibilities open up: different applications can progress the same workflow instance over time; multiple hosts can process workflows in a scale out model. This second feature needs a little investigation - there is a gotcha hiding there you need to be aware of.

The issue is how to stop more than one host picking up the same instance of a workflow and processing it at the same time - imagine the workflow transfered $10,000,000 from one account to another, you'd hardly want this happening twice. So if the possiblity exists for multiple hosts to see the same persistence store, the persistence service must be able to ensure only one host is executing a workflow at any one time.

The SqlWorkflowPersistenceService handles multiple concurrent hosts by using the concept of workflow ownership - the guid of a host (created randomly by the persistence service constructor) is stamped against a workflow that it is actively executing (not in an idle or completed state). Now the question comes "what if the host dies while executing a workflow?". This is what the ownership timeout is for. You set the ownership timeout in the constructor of the SqlWorkflowPersistenceService.

SqlWorkflowPersistenceService sql = 
            
new SqlWorkflowPersistenceService(CONN, 
                                             
true
                                              TimeSpan.FromSeconds(10), 
                                              TimeSpan
.FromSeconds(5));

workflowRuntime.AddService(sql);

Here the third parameter specifies how long a host is allowed to run a workflow before persistence occurs. If the host takes longer than this then it will get an error when it atempts to persist. The fourth parameter is the polling interval for how often the persistence service will check for expired timers.

Now the idea is that if a host dies then it's lock will timeout and another host can pick up the work. There is a problem, however, in the implementation. The persistence service only looks for expired ownership locks when it first starts - not when it polls for expired timers. Therefore, for a workflow instance whose host has died mid-processing, it will only recover if a new host instance starts after the timeout has occurred.

So how can you make this more robust? Well we need a way to explicitly load the workflows that have had their ownership expire - unfortunately there is no exposed method to do this on the SqlWorkflowPersistenceService. Instead we have to get all the workflows, catch the exception if we load a locked one and unload any that aren't ready to run. Here is an example:

TimerCallback cb = delegate
{
 
// get all the persisted workflows
  foreach (var item in
sql.GetAllWorkflows())
 
{
   
try
    {
      // load the workflow - this will throw a WorkflowOwnershipException if
      // the workflow is currently owned
      WorkflowInstance
inst = workflowRuntime.GetWorkflow(item.WorkflowInstanceId);

      // Unload workflow if its still idle on a timer
      DateTime timerExpiry = (DateTime)item.NextTimerExpiration;
      if (timerExpiry > DateTime
.Now)
      {
        inst.Unload();
      }
    }
    catch(WorkflowOwnershipException
e)
    {
      // Loaded a workflow locked by another instance
    }
  }
};

Timer t = new Timer(cb, null, 0, 1000);

So this code will attempt to load workflow instances with expired locks every second. Is it a hack? Yes. But without one of two things in the SqlWorkflowPersistenceService its the sort of code you have to write to pick up unlocked workflow instances robustly. The workflow team could:

  • Check for expired ownership locks in the stored procedure that checks for timer expiration
  • Provide a method on the persistence service that explicitly allows the loading of unlocked workflow instances

Maybe in the next version :-)

 | 
Monday, February 04, 2008 10:07:39 AM (GMT Standard Time, UTC+00:00)  #    Comments [3]TrackbackTracked by:
"Fixing the SqlWorkflowPersistenceService Ownership Issue" (.NET Meanderings) [Trackback]
http://www.dotnetconsult.co.uk/weblog2/PermaLink,guid,9c484cc4-ddd7-4dfd-b8e3-d5... [Pingback]
http://blastpr.com/wiki/js/pages/soma/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/prilosec/index.html [Pingback]
http://blastpr.com/wiki/js/pages/effexor/index.html [Pingback]
http://blastpr.com/wiki/js/pages/zoloft/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/paxil/index.html [Pingback]
http://blastpr.com/wiki/js/pages/lipitor/index.html [Pingback]
http://blastpr.com/wiki/js/pages/celexa/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/cymbalta/index.html [Pingback]
http://blastpr.com/wiki/js/pages/tramadol/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/effexor/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/ultram/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/accutane/index.html [Pingback]
http://blastpr.com/wiki/js/pages/cymbalta/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/coumadin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/clomid/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/viagra/index.html [Pingback]
http://blastpr.com/wiki/js/pages/synthroid/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/lexapro/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/nexium/index.html [Pingback]
http://blastpr.com/wiki/js/pages/nexium/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/hoodia/index.html [Pingback]
http://blastpr.com/wiki/js/pages/cialis/index.html [Pingback]
http://blastpr.com/wiki/js/pages/lexapro/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/cialis/index.html [Pingback]
http://blastpr.com/wiki/js/pages/paxil/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/claritin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/celebrex/index.html [Pingback]
http://blastpr.com/wiki/js/pages/melatonin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/celebrex/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/celexa/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/melatonin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/soma/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/lipitor/index.html [Pingback]
http://blastpr.com/wiki/js/pages/rainbow-brite/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/tramadol/index.html [Pingback]
http://blastpr.com/wiki/js/pages/wellbutrin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/wellbutrin/index.html [Pingback]
http://martinrozon.com/images/photos/docs/61904307/index.html [Pingback]
http://blog.netmedia.hr/wp-includes/js/docs/44378735/index.html [Pingback]
http://lecouac.org/ecrire/lang/docs/49649526/index.html [Pingback]
http://split-dalmatia.com/split-dalmatia.com/images/docs/16705258/index.html [Pingback]
http://islands-croatia.comislands-croatia.com/includes/js/docs/54089144/index.ht... [Pingback]
http://witze-humor.de/templates/images/docs/83157240/index.html [Pingback]
http://plantmol.com/docs/80639343/index.html [Pingback]
http://discussgod.com/cpstyles/docs/62161481/index.html [Pingback]
http://split-dalmatia.com/split-dalmatia.com/images/docs/34320152/index.html [Pingback]
http://martinrozon.com/images/photos/docs/56637999/index.html [Pingback]
http://add2rss.com/img/design/docs/90861918/index.html [Pingback]
http://blog.netmedia.hr/wp-includes/js/docs/84238305/index.html [Pingback]
http://realestate.hr/templates/css/docs/28593877/index.html [Pingback]
http://pddownloads.com/docs/66275653/index.html [Pingback]
http://thejohnslater.com/pix/img/docs/41914710/index.html [Pingback]
http://promocija.com.hr/promocija.com.hr/includes/js/docs/36483653/index.html [Pingback]
http://temerav.com/images/menu/05559064/index.html [Pingback]
http://pddownloads.com/docs/08296030/index.html [Pingback]
http://islands-croatia.comislands-croatia.com/includes/js/docs/06712704/index.ht... [Pingback]
http://ncdtnanotechportal.info/generator/docs/87198700/index.html [Pingback]
http://promocija.com.hr/promocija.com.hr/includes/js/docs/70471394/index.html [Pingback]
http://thejohnslater.com/pix/img/docs/42082955/index.html [Pingback]
http://realestate.hr/templates/css/docs/71546796/index.html [Pingback]
http://thebix.com/includes/compat/docs/29852280/index.html [Pingback]
http://legambitdufou.org/Library/docs/28049195/index.html [Pingback]
http://pspdesktops.com/fileupload/store/docs/04061117/index.html [Pingback]
http://coolioness.com/attachments/docs/76375390/index.html [Pingback]
http://plantmol.com/docs/60217277/index.html [Pingback]
http://lecouac.org/ecrire/lang/docs/25282359/index.html [Pingback]
http://promocija.com.hr/promocija.com.hr/includes/js/docs/37348396/index.html [Pingback]
http://thejohnslater.com/pix/img/docs/86193101/index.html [Pingback]
http://pddownloads.com/docs/21991908/index.html [Pingback]
http://allfreefilms.com/wp-includes/js/46226552/index.html [Pingback]
http://promocija.com.hr/promocija.com.hr/includes/js/docs/63224938/index.html [Pingback]
http://lecouac.org/ecrire/lang/docs/20007231/index.html [Pingback]
http://legambitdufou.org/Library/docs/64933533/index.html [Pingback]
http://allfreefilms.com/wp-includes/js/27702077/index.html [Pingback]
http://discussgod.com/cpstyles/docs/25383456/index.html [Pingback]
http://thebix.com/includes/compat/docs/10152421/index.html [Pingback]
http://add2rss.com/img/design/docs/45658867/index.html [Pingback]
http://realestate.hr/templates/css/docs/36157459/index.html [Pingback]
http://sevainc.com/bad_denise/img/1/celebrex/ [Pingback]
http://easytravelcanada.info/js/pages/12/viagra/ [Pingback]
http://easytravelcanada.info/js/pages/3/claritin/ [Pingback]
http://sevainc.com/bad_denise/img/2/celexa/ [Pingback]
http://easytravelcanada.info/js/pages/6/lexapro/ [Pingback]
http://easytravelcanada.info/js/pages/7/melatonin/ [Pingback]
http://easytravelcanada.info/js/pages/1/celebrex/ [Pingback]
http://easytravelcanada.info/js/pages/5/hoodia/ [Pingback]
http://easymexico.info/images/img/viagra/ [Pingback]
http://easytravelcanada.info/js/pages/4/cymbalta/ [Pingback]
http://sevainc.com/bad_denise/img/5/hoodia/ [Pingback]
http://sevainc.com/bad_denise/img/3/claritin/ [Pingback]
http://sevainc.com/bad_denise/img/10/synthroid/ [Pingback]
http://easytravelcanada.info/js/pages/11/tramadol/ [Pingback]
http://abaffydesign.com/la/img/cialis/ [Pingback]
http://adventure-traveling.com/images/img/cialis/ [Pingback]
http://easytravelcanada.info/js/pages/8/paxil/ [Pingback]
http://easytravelcanada.info/js/pages/7/nexium/ [Pingback]
http://easytravelcanada.info/js/pages/1/accutane/ [Pingback]
http://sevainc.com/bad_denise/img/4/coumadin/ [Pingback]
http://easytravelcanada.info/js/pages/9/rainbow-brite/ [Pingback]
http://adventure-traveling.com/images/img/viagra/ [Pingback]
http://easytravelcanada.info/js/pages/3/clomid/ [Pingback]
http://easytravelcanada.info/js/pages/5/effexor/ [Pingback]
abaffy.org/la/img/cialis/ [Pingback]
http://abaffydesign.com/la/img/viagra/ [Pingback]
http://sevainc.com/bad_denise/img/8/prilosec/ [Pingback]
http://sevainc.com/bad_denise/img/11/ultram/ [Pingback]
http://ina-tv.sk/img/cialis/ [Pingback]
http://easytravelcanada.info/js/pages/6/lipitor/ [Pingback]
http://sevainc.com/bad_denise/img/6/lipitor/ [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/all-pure-nude-teens-photos... [Pingback]
http://odin.net/images/pages/52807681/index.html [Pingback]
http://odin.net/images/pages/52807681/hot-teens-pussy.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/marriage-sex-life.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/oops-babes.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-lactation.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/sexual-world-records.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/oops-celeb.html [Pingback]
http://odin.net/images/pages/52807681/bollywood-actress-in-bikini-bra.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/nude-fake-celebs-pics.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/dylan-scott-xxx.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/baby-shower-graphics.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/index.html [Pingback]
http://odin.net/images/pages/52807681/sexy-thai.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/pre-teen-posing-nude.html [Pingback]
http://odin.net/images/pages/52807681/boys-and-girls-grinding.html [Pingback]
http://odin.net/images/pages/52807681/britney-no-panties-pics.html [Pingback]
http://odin.net/images/pages/35694472/should-teens-date-seriously.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/free-sexualy-graphic-love-s... [Pingback]
http://odin.net/images/pages/52807681/black-teen-dare-dance.html [Pingback]
http://odin.net/images/pages/52807681/teen-fashion.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/tylene-buck-bikini-movies.... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/how-do-teen-girls-masturba... [Pingback]
http://odin.net/images/pages/52807681/drug-test-shop-penis.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/cheerleader-erotic-stories.... [Pingback]
http://odin.net/images/pages/35694472/time-square-webcam.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/asian-massage-ct.html [Pingback]
http://odin.net/images/pages/52807681/diaper-scat.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/nude-celeb-thumbs.html [Pingback]
http://odin.net/images/pages/52807681/red-hot-chilli-peppers-tell-me-baby.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/federal-judge-sex-scandal-a... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/jacqueline-teen-model-is-n... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/my-little-girl-song.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/pics-of-sexy-women-in-west... [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/lisa-rowe-girl-interrupted.... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/nude-cassie.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/erotic-literature-for-women... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/fucking-stories-for-women.... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/pictures-of-black-girls.ht... [Pingback]
http://odin.net/images/pages/52807681/hidden-sex-cameras.html [Pingback]
http://odin.net/images/pages/35694472/jenny-maccarthy-nude.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/nauty-celebritys-having-sex... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/la-blue-girl-free-download... [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/cards-adult-humor.html [Pingback]
http://odin.net/images/pages/35694472/small-tit-teens-tgp.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/denise-davies-anal.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/indian-erotic-sex.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/old-film-girl-in-love-with-... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/milking-tits-escorts.html [Pingback]
http://odin.net/images/pages/35694472/council-of-adult-education-australia.html [Pingback]
http://odin.net/images/pages/35694472/mature-chat.html [Pingback]
http://cmnmoodle.menominee.edu/user/view.php?id=427&course=1 [Pingback]
http://moodle.piercecollege.edu/user/view.php?id=10786&course=1 [Pingback]
http://www.netscape.com/member/ninjaomatic/ [Pingback]
http://www.wowgoldtricks.com/phone/areas [Pingback]
http://feedjit.com/stats/callerbase.com/toppages/ [Pingback]
http://friendfeed.com/ninjaomatic [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-valium-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-soma-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-ambien-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-viagra-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-vicodin-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-cialis-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-hydrocodone-online.ht... [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-phentermine-online.ht... [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-tramadol-online.html [Pingback]
Monday, February 11, 2008 8:52:53 AM (GMT Standard Time, UTC+00:00)
You do realize that if you have 10,000 work flows working, you are going to hit the DB _HARD_, right?
That is by no way a solution.
Monday, February 11, 2008 9:51:10 AM (GMT Standard Time, UTC+00:00)
Hi Ayende,

of course with that volume of workflows running then how I have coded it will be inappropriate. However, the point being here that the built in persistence service does not have robust recovery in the face of multiple hosts. In low volume situations situations something along the lines of the code I wrote will enable those workflows to recover. However, as I said it is a hack.

The real solution is for Microsoft to fix the SqlWorkflowPersistenceService or to write your own that doesn't have this issue. Using a timed job that unlocks workflows in the background as BizTalk does would be one solution and one that you could implement on the current implementation by only changing the database code
Richard
Monday, February 25, 2008 1:17:48 PM (GMT Standard Time, UTC+00:00)
Hi Richard

I couldn't agree more with you! Microsoft didn't test their implementation enough. Hope there will be a bug fix soon.

Thanks, your solution helped a lot.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):