concurrency problem with #include as template

"Carl H. Sayres" <[email protected]> Sat, 04 Feb 2006 13:56:25 -0500
Newsgroups gmane.comp.java.webmacro.user
Organization Data Representations, Inc.
Message-ID <[email protected]>
Hello,

We've encountered a concurrency problem with the #include as template 
directive. It appears that when multiple threads of execution are 
running, webmacro can intermittently select a different template than 
the one requested. We have created a test program that demonstrates the 
problem. If anyone can provide insight into the cause of the problem, 
(or even better, a fix), we would be very appreciative.

I have posted wmtest.jar at 
http://www.datarepresentations.com/wmtest/wmtest.jar
The jar contains the compiled test classes and templates, as well as the 
source code.

In the test, we have a Page object (which extends ArrayList) that 
contains an arbitrary number of PageItems. Each PageItem has a 
getTemplate() method that returns the name of the template file for that 
object. In the Page template, we do the following:

#foreach $pageItem in $page {
    #include as template "$pageItem.Template"
}

There are several  subclasses of PageItem ( PageItemA, PageItemB, and 
PageItemC ). Each of these subclasses has a unique method getA, getB, or 
getC. In the template file that corresponds to each PageItem, we call 
these methods using $pageItem.A, $pageItem.B, or $pageItem.C. As long as 
the correct template is being used, this works. But if PageItemA's 
template were to call $pageItem.B, a ContextException would be thrown. 
If things are working correctly, this should never happen.

We add 1000 of each (PageItems A,B,C) to the Page. When we run a single 
test, everything is fine. This confirms that our objects and templates 
work as expected. But when we run multiple concurrent tests, we 
occasionally see the following error in the log:

1:10:18 PM      engine  ERROR   No public property C on variable 
$pageItem of class wmtest.PageItemB at 
C:\webmacroTest\src\templates\PageItemC.wmt:1.10

This says that WebMacro attempted to render PageItemB using the template 
for PageItemC !!!

To run the test, put the wmtest.jar file (along with webmacro.jar and 
concurrent.jar) in the classpath and run (using j2sdk 4 or 5):

java wmtest.WMTest 1 0

The first number is the number of tests to run. The second is the number 
of milliseconds to sleep in between starting each test. (Each test runs 
in a separate thread. So "wmtest.WMTest 2 1000" starts two threads 1 
second apart).

The output for one test should look like:
Starting test #0
1:47:38 PM      LogFile NOTICE  --- Log Started ---
1:47:38 PM      broker  NOTICE  starting org.webmacro.Broker: 
WebMacro.properties
1:47:38 PM      broker  NOTICE  Loaded settings from WebMacro.defaults, 
WebMacro.properties, (System Properties)
1:47:38 PM      wm      NOTICE  new WebMacro(WebMacro.properties) v2.0
Finished test #0

With only one test, there is never an error. But running many 
simultaneous test often produces the error.  The problem is intermittent 
and very timing sensitive. On a 3GHz P4 WinXPSP2, "wmtest.WMTest 2 1000" 
almost always produces the error. On slower machines, you need to 
increase the number of tests and decrease the time interval to see the 
error. On a 1.0GHz P3 Win2000SP4, we found "wmtest.WMTest 8 700" usually 
produced the error. We have seen the error using both J2sdk 4 and 5 on 
Windows and Linux.

With 2 tests, on the P4 we get:
Starting test #0
1:48:47 PM      LogFile NOTICE  --- Log Started ---
1:48:47 PM      broker  NOTICE  starting org.webmacro.Broker: 
WebMacro.properties
1:48:47 PM      broker  NOTICE  Loaded settings from WebMacro.defaults, 
WebMacro.properties, (System Properties)
1:48:47 PM      wm      NOTICE  new WebMacro(WebMacro.properties) v2.0
Starting test #1
1:48:47 PM      wm      NOTICE  new WebMacro(WebMacro.properties) v2.0
1:48:47 PM      engine  ERROR   No public property B on variable 
$pageItem of class wmtest.PageItemA at 
C:\webmacroTest\src\templates\PageItemB.wmt:1.10
Failed test #0
Finished test #1

The error is very timing sensitive, so you may need to try different 
numbers until you see it on your machine. Also the exact error is 
different each time. We have seen every erroneous combination of 
PageItem[ABC] and the template for [ABC].




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642