Author: bpapez
Date: Wed Jun 6 17:47:34 2007
New Revision: 17483
URL: https://svndev.jahia.net/websvn/listing.php?sc=3D1&rev=3D17483&repname=
=3Djahia
Log:
JAHIA-1375: use concurrency API (Latch and Semaphore) instead of own concur=
rency handling
Modified:
branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/exceptions/JahiaEx=
ception.java
branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/exceptions/JahiaSe=
ssionExpirationException.java
branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/PageGen=
eratorQueue.java
branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
CacheReadValve.java
branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
CacheWriteValve.java
branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
EngineValve.java
branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/etc/spring/applica=
tioncontext-manager.xml
Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/exceptions/J=
ahiaException.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/java/org/jahia/exceptions/JahiaException.java&rev=3D17483&=
repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/exceptions/JahiaEx=
ception.java (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/exceptions/JahiaEx=
ception.java Wed Jun 6 17:47:34 2007
@@ -87,6 +87,8 @@
/** Engine Validation Errors */
public static final int ENGINE_VALIDATION_ERROR =3D 27;
=
+ /** Service unavailable ERROR */
+ public static final int UNAVAILABLE_ERROR =3D 28;
=
/** Archive File error code **/
public static final int ENTRY_NOT_FOUND =3D
30;
Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/exceptions/J=
ahiaSessionExpirationException.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/java/org/jahia/exceptions/JahiaSessionExpirationException.=
java&rev=3D17483&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/exceptions/JahiaSe=
ssionExpirationException.java (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/exceptions/JahiaSe=
ssionExpirationException.java Wed Jun 6 17:47:34 2007
@@ -32,8 +32,6 @@
{
//--------------------------------------------------------------------=
-----
/** Default constructor
- *
- * @param pageID The not-found requested page ID.
*/
public JahiaSessionExpirationException ()
{
Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/P=
ageGeneratorQueue.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/java/org/jahia/operations/PageGeneratorQueue.java&rev=3D17=
483&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/PageGen=
eratorQueue.java (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/PageGen=
eratorQueue.java Wed Jun 6 17:47:34 2007
@@ -16,8 +16,12 @@
* limitations under the License.
*/package org.jahia.operations;
=
-import org.jahia.services.cache.GroupCacheKey;
import org.apache.commons.collections.FastHashMap;
+import org.jahia.bin.Jahia;
+import org.jahia.settings.SettingsBean;
+
+import EDU.oswego.cs.dl.util.concurrent.Semaphore;
+import EDU.oswego.cs.dl.util.concurrent.WaiterPreferenceSemaphore;
=
import java.util.Map;
import java.util.HashMap;
@@ -30,25 +34,18 @@
* To change this template use File | Settings | File Templates.
*/
public class PageGeneratorQueue {
- private Map generatedPage =3D new FastHashMap(2503);
private Map notCacheablePage =3D new FastHashMap(2503);
private Map generatingPage =3D new HashMap(10);
- private int maxPagesToGenerateInParallel =3D 25;
+ private int maxPagesToGenerateInParallel =3D Jahia.getSettings().getMa=
xParallelProcessings();
+ private long pageGenerationWaitTime =3D Jahia.getSettings().getPageGen=
erationWaitTime();
+ private Semaphore availableProcessings =3D null;
=
public PageGeneratorQueue() {
}
-
+ =
public PageGeneratorQueue(int maxPagesToGenerateInParallel) {
this.maxPagesToGenerateInParallel =3D maxPagesToGenerateInParallel;
- }
-
- public void removeGeneratedPage(GroupCacheKey entryKey) {
- generatedPage.remove(entryKey);
- }
-
- public Map getGeneratedPage() {
- return generatedPage;
- }
+ } =
=
public Map getNotCacheablePage() {
return notCacheablePage;
@@ -58,11 +55,22 @@
return generatingPage;
}
=
+ public Semaphore getAvailableProcessings() {
+ if (availableProcessings !=3D null)
+ return availableProcessings;
+ synchronized (this) {
+ if (availableProcessings !=3D null)
+ return availableProcessings; =
+ availableProcessings =3D new WaiterPreferenceSemaphore(getMaxP=
agesToGenerateInParallel()); =
+ }
+ return availableProcessings;
+ }
+ =
public int getMaxPagesToGenerateInParallel() {
return maxPagesToGenerateInParallel;
}
=
- public void setMaxPagesToGenerateInParallel(int maxPagesToGenerateInPa=
rallel) {
- this.maxPagesToGenerateInParallel =3D maxPagesToGenerateInParallel;
+ public long getPageGenerationWaitTime() {
+ return pageGenerationWaitTime;
}
}
Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/v=
alves/CacheReadValve.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/java/org/jahia/operations/valves/CacheReadValve.java&rev=
=3D17483&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
CacheReadValve.java (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
CacheReadValve.java Wed Jun 6 17:47:34 2007
@@ -177,7 +177,6 @@
}
} else {
if (logger.isDebugEnabled()) logger.debug("!!!!!! Coul=
d not find HTML page in cache!!!!!!!!!!!!!!!!!!");
- generatorQueue.removeGeneratedPage(entryKey);
}
}
ValveContext.valveResources.set(new PageState(iscacheable, ent=
ryKey));
Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/v=
alves/CacheWriteValve.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/java/org/jahia/operations/valves/CacheWriteValve.java&rev=
=3D17483&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
CacheWriteValve.java (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
CacheWriteValve.java Wed Jun 6 17:47:34 2007
@@ -187,8 +187,6 @@
if(!notCacheablePage.containsKey(entryKey))
htmlCache.putCacheEntry(entryKey, newEntry, true);
if (logger.isDebugEnabled()) logger.debug("Added HTML page int=
o the cache.");
- generatorQueue.getGeneratingPage().remove(entryKey);
- generatorQueue.getGeneratedPage().put(entryKey,entryKey);
} else {
if (logger.isDebugEnabled()) logger.debug("Bypassing HTML cach=
e storage");
if (htmlCache !=3D null && jParams.getUser() !=3D null) {
@@ -199,7 +197,6 @@
workflowState,
jParams.getUserAgent (), =
jParams.getScheme());
- generatorQueue.getGeneratingPage().remove(entryKey);
if (jParams.getCacheExpirationDelay() =3D=3D 0 && !notCach=
eablePage.containsKey(entryKey)) {
notCacheablePage.put(entryKey, entryKey);
}
Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/v=
alves/EngineValve.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/java/org/jahia/operations/valves/EngineValve.java&rev=3D17=
483&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
EngineValve.java (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/operations/valves/=
EngineValve.java Wed Jun 6 17:47:34 2007
@@ -16,9 +16,12 @@
* limitations under the License.
*/package org.jahia.operations.valves;
=
+import java.util.Map;
+
import org.jahia.data.JahiaData;
import org.jahia.engines.JahiaEngine;
import org.jahia.exceptions.JahiaException;
+import org.jahia.exceptions.JahiaServerOverloadedException;
import org.jahia.params.ProcessingContext;
import org.jahia.params.SessionState;
import org.jahia.pipelines.PipelineException;
@@ -31,6 +34,8 @@
import org.jahia.hibernate.manager.SpringContextSingleton;
import org.jahia.services.cache.GroupCacheKey;
=
+import EDU.oswego.cs.dl.util.concurrent.Latch;
+
/**
* <p>Title: </p>
* <p>Description: </p>
@@ -43,6 +48,7 @@
public class EngineValve implements Valve {
private static org.apache.log4j.Logger logger =3D
org.apache.log4j.Logger.getLogger(EngineValve.class);
+ =
private PageGeneratorQueue generatorQueue;
=
public EngineValve() {
@@ -52,108 +58,133 @@
ProcessingContext processingContext =3D (ProcessingContext) contex=
t;
// Get the engine instance (can throw a JahiaException)
String engineName =3D processingContext.getEngine();
- int retryCount =3D 0;
- while (retryCount < processingContext.settings().getPageGeneration=
MaxRetryCount()) {
- // So we does not found the page in the cache
- // check if the page is already generating or is generated
- // force generation
- PageState state =3D (PageState) ValveContext.valveResources.ge=
t();
- boolean haveToBeenGenerate =3D false;
- GroupCacheKey entryKey =3D null;
- if(state!=3D null) {
- entryKey =3D state.getKey();
- if (!state.isCacheable() || generatorQueue.getNotCacheable=
Page().containsKey(entryKey))
- haveToBeenGenerate =3D true;
- } else {
- haveToBeenGenerate =3D true;
+
+ // So we did not find the page in the cache
+ // check if the page is already generating or is generated
+ // force generation
+ Latch latch =3D null;
+
+ PageState state =3D (PageState) ValveContext.valveResources.get();
+ boolean mustBeWaited =3D false;
+ GroupCacheKey entryKey =3D null;
+ if (state !=3D null
+ && state.isCacheable()
+ && !generatorQueue.getNotCacheablePage().containsKey(
+ state.getKey())) {
+ entryKey =3D state.getKey();
+ Map generatingPages =3D generatorQueue.getGeneratingPage();
+ synchronized (generatingPages) {
+ if (!generatingPages.containsKey(entryKey)) {
+ latch =3D new Latch();
+ generatingPages.put(entryKey, latch);
+ } else {
+ latch =3D (Latch) generatingPages.get(entryKey);
+ mustBeWaited =3D true;
+ }
}
- if (!haveToBeenGenerate) {
- synchronized (generatorQueue) {
- if (!generatorQueue.getGeneratingPage().containsKey(en=
tryKey) && !generatorQueue.getGeneratedPage().containsKey(entryKey) &&
- generatorQueue.getGeneratingPage().size() < genera=
torQueue.getMaxPagesToGenerateInParallel()) {
- // generate almost 3 pages in parallel
- generatorQueue.getGeneratingPage().put(entryKey, e=
ntryKey);
- generatorQueue.removeGeneratedPage(entryKey);
- haveToBeenGenerate =3D true;
+ try {
+ if (mustBeWaited) {
+ if (!latch.attempt(generatorQueue.getPageGenerationWai=
tTime())){
+ throw new JahiaServerOverloadedException();
}
+ if (CacheReadValve.checkCache(processingContext))
+ return;
}
+ } catch (InterruptedException ie) {
+ logger.debug("The waiting thread has been interrupted :", =
ie);
+ throw new PipelineException(ie);
+ } catch (Throwable je) {
+ throw new PipelineException(je);
+ }
+ }
+ boolean semaphoreAcquired =3D false;
+ try {
+ if (!generatorQueue.getAvailableProcessings().attempt(
+ generatorQueue.getPageGenerationWaitTime())){
+ throw new JahiaServerOverloadedException();
+ } else {
+ semaphoreAcquired =3D true;
}
- if (haveToBeenGenerate) {
- try {
- JahiaEngine theEngine =3D getEngineInstance(engineName=
);
- // logger.debug("Engine=3D" + engineName);
-
- if (theEngine =3D=3D null) {
- logger.debug("Could not get the engine [" + engine=
Name +
- "] instance.");
- return;
- }
=
- JahiaData jData =3D null;
- if (theEngine.needsJahiaData(processingContext)) {
- // logger.debug("Engine need JahiaData");
- if (processingContext.getPage() !=3D null) {
- jData =3D new JahiaData(processingContext);
- } else {
- jData =3D new JahiaData(processingContext, fal=
se);
- }
- }
+ JahiaEngine theEngine =3D getEngineInstance(engineName);
+ // logger.debug("Engine=3D" + engineName);
=
- if (jData =3D=3D null) {
- // at least create a jData with minimum data for t=
aglibs usage everywhere
- jData =3D new JahiaData(processingContext, false);
- }
+ if (theEngine =3D=3D null) {
+ logger.debug("Could not get the engine [" + engineName
+ + "] instance.");
+ return;
+ }
=
- // for JSp
- processingContext.setAttribute("org.jahia.data.JahiaDa=
ta",
- jData);
-
- theEngine.handleActions(processingContext, jData);
- logger.debug("Operation handled for engine " + process=
ingContext.getEngine());
-
- // destroys request-dependant objects
- jData =3D null;
- SessionState session =3D processingContext.getSessionS=
tate();
- // save last engine name in session
- session.setAttribute(ProcessingContext.SESSION_LAST_EN=
GINE_NAME, processingContext.getEngine());
- // save last requested page id in session
- session.setAttribute(ProcessingContext.SESSION_LAST_RE=
QUESTED_PAGE_ID, new Integer(processingContext.getPageID()));
- } catch (Throwable je) {
- generatorQueue.getGeneratingPage().remove(entryKey);
- throw new PipelineException(je);
- } finally {
- ServicesRegistry.getInstance().getJahiaEventService().=
fireAggregatedEvents();
+ JahiaData jData =3D null;
+ if (theEngine.needsJahiaData(processingContext)) {
+ // logger.debug("Engine need JahiaData");
+ if (processingContext.getPage() !=3D null) {
+ jData =3D new JahiaData(processingContext);
+ } else {
+ jData =3D new JahiaData(processingContext, false);
}
- ValveContext.valveResources.set(state);
- valveContext.invokeNext(context);
- break;
- } else if (!haveToBeenGenerate && state.isCacheable()) {
- try {
- Thread.sleep(processingContext.settings().getPageGener=
ationLoopWaitTime());
- logger.debug("Waiting for page generation for " + proc=
essingContext.settings().getPageGenerationLoopWaitTime() + "ms...");
- if (CacheReadValve.checkCache(processingContext))
- return;
- } catch (InterruptedException e) {
- logger.error("Exception during sleep ", e);
+ }
+
+ if (jData =3D=3D null) {
+ // at least create a jData with minimum data for taglibs u=
sage everywhere
+ jData =3D new JahiaData(processingContext, false);
+ }
+
+ // for JSp
+ processingContext.setAttribute("org.jahia.data.JahiaData", jDa=
ta);
+
+ theEngine.handleActions(processingContext, jData);
+ logger.debug("Operation handled for engine "
+ + processingContext.getEngine());
+
+ // destroys request-dependant objects
+ jData =3D null;
+ SessionState session =3D processingContext.getSessionState();
+ // save last engine name in session
+ session.setAttribute(ProcessingContext.SESSION_LAST_ENGINE_NAM=
E,
+ processingContext.getEngine());
+ // save last requested page id in session
+ session.setAttribute(
+ ProcessingContext.SESSION_LAST_REQUESTED_PAGE_ID,
+ new Integer(processingContext.getPageID()));
+ =
+ ValveContext.valveResources.set(state);
+ valveContext.invokeNext(context);
+ =
+ } catch (InterruptedException ie) {
+ logger.debug("The waiting thread has been interrupted :", ie);
+ throw new PipelineException(ie);
+ } catch (Throwable je) {
+ throw new PipelineException(je);
+ } finally {
+ if (semaphoreAcquired){
+ generatorQueue.getAvailableProcessings().release();
+ }
+ if (latch !=3D null) {
+ latch.release();
+ }
+ if (state !=3D null && state.getKey() !=3D null) {
+ synchronized (generatorQueue.getGeneratingPage()) {
+ generatorQueue.getGeneratingPage().remove(state.getKey=
());
}
}
- retryCount++;
- }
- if (retryCount >=3D processingContext.settings().getPageGeneration=
MaxRetryCount()) {
- logger.warn("Maximum waiting loop time reached, aborting reque=
st !");
+ ServicesRegistry.getInstance().getJahiaEventService()
+ .fireAggregatedEvents();
}
}
+ =
public void initialize() {
}
- /** Retrieve the requested engine instance.
- *
- * @param name the engine name
- *
- * @return the reference to the engine, or <code>null</code> when the=
engine name
- * is unknown in the Engines Registry.
- *
+ /**
+ * Retrieve the requested engine instance.
+ * =
+ * @param name
+ * the engine name
+ * =
+ * @return the reference to the engine, or <code>null</code> when the =
engine name is unknown in the Engines Registry.
+ * =
* @throws JahiaException
- * when the Engines Registry reference could not be retrieved.
+ * when the Engines Registry reference could not be retrie=
ved.
*/
private JahiaEngine getEngineInstance (String name)
throws JahiaException {
Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/etc/spring/a=
pplicationcontext-manager.xml
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/webapp/WEB-INF/etc/spring/applicationcontext-manager.xml&r=
ev=3D17483&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/etc/spring/applica=
tioncontext-manager.xml (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/etc/spring/applica=
tioncontext-manager.xml Wed Jun 6 17:47:34 2007
@@ -624,7 +624,6 @@
</bean>
=
<bean id=3D"org.jahia.operations.PageGeneratorQueue" class=3D"org.jahi=
a.operations.PageGeneratorQueue">
- <property name=3D"maxPagesToGenerateInParallel"><value>${pageGener=
ationMaxParallel}</value></property>
</bean>
=
<!-- Nicol=C3=83=C2=A1s Charczewski - Neoris Argentina - added 07/04/2=
006 - begin -->
_______________________________________________
cvs_list mailing list
[email protected]
http://lists.jahia.org/cgi-bin/mailman/listinfo/cvs_list