Openbravo Issue Tracking System - Openbravo ERP
View Issue Details
0020515Openbravo ERPA. Platformpublic2012-05-15 17:182017-03-15 20:19
alostale 
alostale 
normalmajorhave not tried
closedfixed 
5
 
3.0PR17Q2 
caristu
Core
No
0020515: OBCriteria creates contention at JVM
OBCriteria is still adding more overhead than OBQuery, after fix 0020485 it is slower.

Note this measurement is just for overhead and not for query itself.

Profiling attached test cases, it can be seen most of the time is spent in class loading triggered by Hibernate criteria (cpu-time.png). An additional problem is this classloading is synchronized by class name (lock-monitor.png), this in practice means, concurrent threads needs to wait these locks to be released in order to execute the criteria.

The problem is the className SessionFactoryImpl.getImplementors receives [1] is not actually a classname but entity name, so ReflectHelper.classForName finally performs a locking Class.forName [2]

[1] https://github.com/alostale/hibernate-orm/blob/3.6.3-patched/hibernate-core/src/main/java/org/hibernate/impl/SessionFactoryImpl.java#L827 [^]
[2] https://github.com/alostale/hibernate-orm/blob/3.6.3-patched/hibernate-core/src/main/java/org/hibernate/util/ReflectHelper.java#L192 [^]
Execute attached test cases which executes 50000 times a very simple sql comparing times between OBCriteria and OBQuery. These 50000 executions are done splitting them from 1 to 8 threads.

The results in a laptop with 8 cores are:

Criteria with 1 threads done in 14985 ms
Criteria with 2 threads done in 13768 ms
Criteria with 3 threads done in 14811 ms
Criteria with 4 threads done in 14988 ms
Criteria with 5 threads done in 15200 ms
Criteria with 6 threads done in 15388 ms
Criteria with 7 threads done in 15311 ms
Criteria with 8 threads done in 15401 ms

Query with 1 threads done in 3728 ms
Query with 2 threads done in 1743 ms
Query with 3 threads done in 1287 ms
Query with 4 threads done in 1187 ms
Query with 5 threads done in 1121 ms
Query with 6 threads done in 1067 ms
Query with 7 threads done in 1000 ms
Query with 8 threads done in 955 ms

Here we can see OBCriteria overhead is 5 times OBQuery's. But not only that, but parallel executions do not improve throughput in criteria's case.
When creating the criteria, use the actual class name implementing the entity rather than entity's name so the class lookup can be done faster.

Doing it the overhead is partially reduced, but the locking is much better.

These are the results of the same executions done before, where can be seen it scales much better with multiple threads than before.

Criteria with 1 threads done in 10717 ms
Criteria with 2 threads done in 4915 ms
Criteria with 3 threads done in 3317 ms
Criteria with 4 threads done in 2796 ms
Criteria with 5 threads done in 2563 ms
Criteria with 6 threads done in 2486 ms
Criteria with 7 threads done in 2265 ms
Criteria with 8 threads done in 2176 ms
Performance
related to design defect 0020611 closed alostale OBCriteria doesn't support ScrollabeResults 
related to feature request 00206663.0MP13 closed mtaal Allow creation of proxy when setting property in business object 
related to defect 00204853.0MP12 closed alostale Slow OBCriteria 
related to design defect 0035271 closed alostale OBCriteria overhead 
related to design defect 0037015 closed alostale contention in DAL proxies with high concurrency 
? CriteriaVsQuery.java (3,459) 2017-02-15 12:38
https://issues.openbravo.com/file_download.php?file_id=10448&type=bug
png cpu-time.png (34,108) 2017-02-15 12:59
https://issues.openbravo.com/file_download.php?file_id=10449&type=bug
png

png lock-monitor.png (32,517) 2017-02-15 13:00
https://issues.openbravo.com/file_download.php?file_id=10450&type=bug
png
Issue History
2012-05-15 17:18alostaleNew Issue
2012-05-15 17:18alostaleAssigned To => alostale
2012-05-15 17:18alostaleModules => Core
2012-05-15 17:18alostaleRelationship addedrelated to 0020485
2012-05-17 12:08mtaalNote Added: 0048728
2012-06-01 07:55alostaleTag Attached: Performance
2012-06-01 07:55alostaleRelationship addedduplicate of 0020611
2012-06-01 08:00alostaleRelationship replacedrelated to 0020611
2012-06-05 15:36hgbotCheckin
2012-06-05 15:36hgbotNote Added: 0049621
2012-06-05 15:36hgbotCheckin
2012-06-05 15:36hgbotNote Added: 0049623
2012-06-05 15:36hgbotCheckin
2012-06-05 15:36hgbotNote Added: 0049624
2012-09-24 12:53AugustoMauchNote Added: 0052334
2012-09-24 12:53AugustoMauchStatusnew => scheduled
2013-01-07 06:47eintelauIssue Monitored: eintelau
2017-02-15 12:38alostaleFile Added: CriteriaVsQuery.java
2017-02-15 12:42alostaleTriggers an Emergency Pack => No
2017-02-15 12:42alostaleDescription Updatedbug_revision_view_page.php?rev_id=14596#r14596
2017-02-15 12:42alostaleSteps to Reproduce Updatedbug_revision_view_page.php?rev_id=14598#r14598
2017-02-15 12:59alostaleFile Added: cpu-time.png
2017-02-15 13:00alostaleFile Added: lock-monitor.png
2017-02-15 13:03alostaleDescription Updatedbug_revision_view_page.php?rev_id=14599#r14599
2017-02-15 13:13alostaleDescription Updatedbug_revision_view_page.php?rev_id=14600#r14600
2017-02-15 13:17alostaleProposed Solution updated
2017-02-15 14:01alostaleRelationship addedrelated to 0035271
2017-02-16 09:53alostaleRelationship addedrelated to 0020666
2017-02-16 09:54alostaleSummarySlow OBCriteria => OBCriteria creates contention at JVM
2017-02-16 10:25hgbotCheckin
2017-02-16 10:25hgbotNote Added: 0094382
2017-02-16 10:25hgbotStatusscheduled => resolved
2017-02-16 10:25hgbotResolutionopen => fixed
2017-02-16 10:25hgbotFixed in SCM revision => http://code.openbravo.com/erp/devel/pi/rev/0e3c102ec91e2102fbdacc049b48986a400809b7 [^]
2017-02-16 10:26alostaleReview Assigned To => caristu
2017-02-20 09:10caristuNote Added: 0094452
2017-02-20 09:10caristuStatusresolved => closed
2017-02-20 09:10caristuFixed in Version => 3.0PR17Q2
2017-02-22 14:15hgbotCheckin
2017-02-22 14:15hgbotNote Added: 0094534
2017-03-15 20:19hudsonbotCheckin
2017-03-15 20:19hudsonbotNote Added: 0095189
2017-03-15 20:19hudsonbotCheckin
2017-03-15 20:19hudsonbotNote Added: 0095203
2017-10-06 10:42alostaleRelationship addedrelated to 0037015

Notes
(0048728)
mtaal   
2012-05-17 12:08   
Note, that in the 20485 issue the performance hit was in the classloading, apparently the hibernate Criteria logic does a lot of getMappedClass calls which calls classloaders which is very slow.

It can make sense to check this again, but ofcourse the issue can also be in other places...
(0049621)
hgbot   
2012-06-05 15:36   
Repository: erp/devel/pi
Changeset: 0f69dacb316f5bed970bc499faf8cdd9004b25f4
Author: Martin Taal <martin.taal <at> openbravo.com>
Date: Tue Jun 05 13:06:49 2012 +0200
URL: http://code.openbravo.com/erp/devel/pi/rev/0f69dacb316f5bed970bc499faf8cdd9004b25f4 [^]

Related to issue 20515: Slow OBCriteria
Change the way order by is done to follow the standard approach of hibernate Criteria, resulting in a better joining of the order by table/property

---
M src/org/openbravo/dal/service/OBCriteria.java
---
(0049623)
hgbot   
2012-06-05 15:36   
Repository: erp/devel/pi
Changeset: 551e33daa248aa2d1e4989cc4dae80263dcf8119
Author: Martin Taal <martin.taal <at> openbravo.com>
Date: Tue Jun 05 13:08:30 2012 +0200
URL: http://code.openbravo.com/erp/devel/pi/rev/551e33daa248aa2d1e4989cc4dae80263dcf8119 [^]

Related to issue 20515: Slow OBCriteria
Enhanced obcriteria testcase

---
M src-test/org/openbravo/test/dal/DalPerformanceCriteriaTest.java
---
(0049624)
hgbot   
2012-06-05 15:36   
Repository: erp/devel/pi
Changeset: f91001dc95e53f6a8fa6964e93e126a6def9adad
Author: Martin Taal <martin.taal <at> openbravo.com>
Date: Tue Jun 05 15:34:12 2012 +0200
URL: http://code.openbravo.com/erp/devel/pi/rev/f91001dc95e53f6a8fa6964e93e126a6def9adad [^]

Related to issue 20515: Slow OBCriteria
Some additional test queries and printing of results

---
M src-test/org/openbravo/test/dal/DalPerformanceCriteriaTest.java
---
(0052334)
AugustoMauch   
2012-09-24 12:53   
Effort: 10
Impact: high
Plan: mid
(0094382)
hgbot   
2017-02-16 10:25   
Repository: erp/devel/pi
Changeset: 0e3c102ec91e2102fbdacc049b48986a400809b7
Author: Asier Lostalé <asier.lostale <at> openbravo.com>
Date: Wed Feb 15 14:13:23 2017 +0100
URL: http://code.openbravo.com/erp/devel/pi/rev/0e3c102ec91e2102fbdacc049b48986a400809b7 [^]

fixed issue 20515: OBCriteria creates contention at JVM

  When using OBCriteria for same entity by several concurrent threads, there was
  contention at JVM due to locks in Class.forName(entityName).

  This Class.forName(entityName) always results in ClassNotFoundException because
  entityName is not an actual class name. Hibernate criteria can accept either an
  entity name or a class name. It has been changed to set class name instead of
  entity so that it finds the class in the classloader cache earlier removing contention.

  Note when criteria is used for scrolling, Hibernate's flow is different and it
  expects it to be an entity name, though in this case there's no contention because
  class is resolved by internal caches. This is also covered in the fix as exceptional
  case.

---
M src/org/openbravo/dal/service/OBCriteria.java
M src/org/openbravo/dal/service/OBDal.java
---
(0094452)
caristu   
2017-02-20 09:10   
Code Reviewed
(0094534)
hgbot   
2017-02-22 14:15   
Repository: erp/devel/pi
Changeset: 302ebfe140555c3c6bf7f49593199ffe644b8566
Author: Asier Lostalé <asier.lostale <at> openbravo.com>
Date: Wed Feb 22 14:14:23 2017 +0100
URL: http://code.openbravo.com/erp/devel/pi/rev/302ebfe140555c3c6bf7f49593199ffe644b8566 [^]

related with bug 20515: OBCriteria creates contention at JVM

  Same fix for missing createCriteria with alias.

---
M src/org/openbravo/dal/service/OBDal.java
---
(0095189)
hudsonbot   
2017-03-15 20:19   
A changeset related to this issue has been promoted main and to the
Central Repository, after passing a series of tests.

Promotion changeset: https://code.openbravo.com/erp/devel/main/rev/54e102bef53e [^]
Maturity status: Test
(0095203)
hudsonbot   
2017-03-15 20:19   
A changeset related to this issue has been promoted main and to the
Central Repository, after passing a series of tests.

Promotion changeset: https://code.openbravo.com/erp/devel/main/rev/54e102bef53e [^]
Maturity status: Test