Thursday, May 30, 2013

Slow response IIS with long running requests

Last week i got myself in a little trouble. I have a back-end website that does a lot of processing stuff with very long running requests (some take days to complete). The website got slow if about 8-9 long running requests were active. These request do not consume a lot of cpu or memory so the problem had to be somewhere in IIS or Coldfusion. I posted a blog about the maximum number of simultaneous templates in coldfusion earlier this month (maximum number of simultaneous templates). Although this could have been an issue here it was not. In the IIS application pool each pool has 1 worker process assigned to it by default. In most cases (read 99%) this is fine, but if you are working with multiple long running request this can become a bottleneck. To resolve the problem you can assign more worker processes to the application pool.
Note: In general it is not a good idea to increase the number of maximum worker processes. This should only be done in very specific circumstances.

Wednesday, May 15, 2013

Quit long running regular expression in coldfusion (aka backtracking)

A problem you might encouter when working with regular expressions is you can't quit a regex currently in progress. There is no timeout setting. This can potentially freeze your server. The function below uses coldfusion's relatively new cfthread function to bypass this problem. In the example below the regex is quit if it is running for more then 3000 miliseconds.
<cffunction name="timedRegex" access="public" returntype="any" output="true">
 <cfargument name="txt" type="string">
 <cfargument name="pattern" type="string">

 <cfset result = structNew()>
 <cfset result.success = false>
 <cfset result.found = arrayNew(1)>

 <cfset var threadName = "thread_" & rereplace(createUuid(),'-','','all')>
 <cfthread action="run" name="#threadName#" pattern=#pattern# txt=#txt# result=#result#>
  <cfset var local = structNew()>
  <cfset local.objPattern = ''>
  <cfset local.objMatcher = ''>
  <cfset local.str = structNew()>
  <cfset local.c = 0>
  <cfset local.d = 0>
  <cfset local.objPattern = CreateObject("java","java.util.regex.Pattern").Compile('#trim(pattern)#') />
  <cfset local.objMatcher = local.objPattern.Matcher(txt) />
  <cfloop condition="local.objMatcher.Find()">
   <cfset result.found[arraylen(result.found)+1] = structNew()>
   <cfset result.found[arraylen(result.found)].string = objMatcher.Group() />
   <cfset result.found[arraylen(result.found)].groups = arrayNew(1) />
   <cfloop from=1 to="#objMatcher.groupCount()#" index=local.d>
    <cfset result.found[arraylen(result.found)].groups[local.d] = objMatcher.Group(local.d) />

  <cfset result.success = true>
  <cfset THREAD.response = result>

 <cfthread action="join" name="#threadName#" timeout="3000"/>
 <cfif evaluate(threadName & ".status") eq 'COMPLETED'>
  <cfreturn evaluate(threadName & ".response")>
  <cfthread action="terminate" name="#threadName#"/>
  <cfreturn result>

<cfdump var="#timedRegex(txt="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy",pattern="(x+x+)+(y)")#">
<cfdump var="#timedRegex(txt="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",pattern="(x+x+)+(y)")#">

Tuesday, May 14, 2013

Maximum number of simultaneous Template requests

John: How do you increase the number of concurrent template requests in coldfusion 9?

Jesse: I am thinking. Isn't this a setting in the coldfusion administrator under request tuning?

John: Are you sure?

Jesse: Yes I am sure it is there i looked it up see "Maximum number of simultaneous Template requests" and "Maximum number of running JRun threads".

John: Okay have you tested it?

Jesse: No, wait i will test it. Just hang on a sec.

John: (taking a small cup of coffee and reading a post on his favourite website)

Jesse: Hmmm, John i tried setting up 35 simultaneous requests but for some reason i can only get up to 25 simultaneous template requests.

John: I know you are running IIS 6.0 right?

Jesse: Yes.

John: Read this article and try again: Adobe website

Jesse: What the heck.

John: (smiling)

Jesse: Why is there a setting called "Maximum number of simultaneous Template requests" in the coldfusion administrator when this does not work. And why do i have to set this in some config file placed deep inside coldfusion.

John: (still smiling) Only the coldfusion God knows.

---- End of conversation ----

If for some reason you do not wish to goto the adobe official website, below the article they posted on how to increase the number of simultaneous template requests.


IIS 6/IIS 7 for ColdFusion 9: Increasing the Number of Worker Threads

While using IIS6 for ColdFusion 9 (or ColdFusion 9.0.1), owing to heavy load, if you encounter performance issues, you may increase the number of worker threads.

    Go to the directory cf_root\runtime\lib\wsconfig\1.
    Note: In multiserver scenarios, the location is  jrun_root/lib/wsconfig/1. 1 indicates a single IIS website. If you have configured multiple IIS websites, the number can change accordingly.
    Open the file jrun_iis6_wildcard.ini (jrun_iis7_wildcard.ini if you use IIS 7).
    Uncomment maxworkerthreads=25.
    Note: By default, maxworkerthreads is commented.
    Change the value to 50 (maxworkerthreads=50) from the default value of 25.
    Restart IIS 6/IIS 7.

Note: The Maximum number of Simultaneous Template requests (Admin > Server Settings > Request Tunings > Request Limits) must also be increased accordingly.