[exo-jcr-commits] exo-jcr SVN: r1373 - in jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query: lucene and 1 other directory.
do-not-reply at jboss.org
do-not-reply at jboss.org
Wed Jan 13 05:14:17 EST 2010
Author: nfilotto
Date: 2010-01-13 05:14:17 -0500 (Wed, 13 Jan 2010)
New Revision: 1373
Added:
jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexUpdateMonitorListener.java
Modified:
jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JBossCacheIndexInfos.java
jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JbossCacheIndexUpdateMonitor.java
jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/DefaultIndexUpdateMonitor.java
jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexUpdateMonitor.java
jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/MultiIndex.java
Log:
EXOJCR-387: Make UpdateInProgress observable
Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JBossCacheIndexInfos.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JBossCacheIndexInfos.java 2010-01-13 09:34:22 UTC (rev 1372)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JBossCacheIndexInfos.java 2010-01-13 10:14:17 UTC (rev 1373)
@@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.Serializable;
+import java.util.Map;
import java.util.Set;
/**
@@ -111,8 +112,7 @@
}
/**
- *
- * @param mode
+ * @see org.exoplatform.services.jcr.impl.core.query.IndexerIoModeListener#onChangeMode(org.exoplatform.services.jcr.impl.core.query.IndexerIoMode)
*/
public void onChangeMode(IndexerIoMode mode)
{
@@ -164,8 +164,22 @@
{
if (!event.isPre() && event.getFqn().equals(namesFqn))
{
- // read from cache to update lists
- Set<String> set = (Set<String>)cache.get(namesFqn, LIST_KEY);
+ Set<String> set = null;
+ Map<?, ?> data = event.getData();
+ if (data == null)
+ {
+ log.warn("The data map is empty");
+ }
+ else
+ {
+ set = (Set<String>)data.get(namesFqn);
+ }
+ if (set == null)
+ {
+ log.warn("The data cannot be found, we will try to get it from the cache");
+ // read from cache to update lists
+ set = (Set<String>)cache.get(namesFqn, LIST_KEY);
+ }
if (set != null)
{
setNames(set);
Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JbossCacheIndexUpdateMonitor.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JbossCacheIndexUpdateMonitor.java 2010-01-13 09:34:22 UTC (rev 1372)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JbossCacheIndexUpdateMonitor.java 2010-01-13 10:14:17 UTC (rev 1373)
@@ -20,7 +20,9 @@
import org.exoplatform.services.jcr.impl.core.query.IndexerIoMode;
import org.exoplatform.services.jcr.impl.core.query.IndexerIoModeHandler;
+import org.exoplatform.services.jcr.impl.core.query.IndexerIoModeListener;
import org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitor;
+import org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitorListener;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.jboss.cache.Cache;
@@ -29,15 +31,22 @@
import org.jboss.cache.Node;
import org.jboss.cache.lock.LockManager;
import org.jboss.cache.lock.LockType;
+import org.jboss.cache.notifications.annotation.CacheListener;
+import org.jboss.cache.notifications.annotation.NodeModified;
+import org.jboss.cache.notifications.event.NodeModifiedEvent;
import java.io.Serializable;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
/**
* @author <a href="mailto:Sergey.Kabashnyuk at exoplatform.org">Sergey Kabashnyuk</a>
* @version $Id: exo-jboss-codetemplates.xml 34360 2009-07-22 23:58:59Z ksm $
*
*/
-public class JbossCacheIndexUpdateMonitor implements IndexUpdateMonitor
+ at CacheListener
+public class JbossCacheIndexUpdateMonitor implements IndexUpdateMonitor, IndexerIoModeListener
{
/**
* Logger instance for this class
@@ -53,12 +62,19 @@
public final IndexerIoModeHandler modeHandler;
/**
+ * The list of all the listeners
+ */
+ private final List<IndexUpdateMonitorListener> listeners;
+
+ /**
* @param cache instance of JbossCache that is used to deliver index names
*/
public JbossCacheIndexUpdateMonitor(Cache<Serializable, Object> cache, IndexerIoModeHandler modeHandler)
{
this.cache = cache;
this.modeHandler = modeHandler;
+ this.listeners = new CopyOnWriteArrayList<IndexUpdateMonitorListener>();
+ modeHandler.addIndexerIoModeListener(this);
Node<Serializable, Object> cacheRoot = cache.getRoot();
// prepare cache structures
@@ -69,10 +85,33 @@
{
setUpdateInProgress(false);
}
-
+ else
+ {
+ // Currently READ_ONLY is set, so new lists should be fired to multiIndex.
+ cache.addCacheListener(this);
+ }
}
/**
+ * @see org.exoplatform.services.jcr.impl.core.query.IndexerIoModeListener#onChangeMode(org.exoplatform.services.jcr.impl.core.query.IndexerIoMode)
+ */
+ public void onChangeMode(IndexerIoMode mode)
+ {
+ if (mode == IndexerIoMode.READ_WRITE)
+ {
+ // In READ_WRITE, the value of UpdateInProgress is changed locally so no need to listen
+ // to the cache
+ cache.removeCacheListener(this);
+ }
+ else
+ {
+ // In READ_ONLY, the value of UpdateInProgress will be changed remotely, so we have
+ // no need but to listen to the cache to be notified when the value changes
+ cache.addCacheListener(this);
+ }
+ }
+
+ /**
* @see org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitor#getUpdateInProgress()
*/
public boolean getUpdateInProgress()
@@ -109,8 +148,7 @@
}
catch (InterruptedException e)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.warn("An error occurs while tryning to lock the node " + name, e);
}
return false;
}
@@ -125,6 +163,10 @@
throw new IllegalStateException("Unable to set updateInProgress value in IndexerIoMode.READ_ONLY mode");
}
cache.put(PARAMETER_ROOT, PARAMETER_NAME, new Boolean(updateInProgress));
+ for (IndexUpdateMonitorListener listener : listeners)
+ {
+ listener.onUpdateInProgressChange(updateInProgress);
+ }
}
/**
@@ -137,4 +179,45 @@
lm.unlock(Fqn.fromRelativeFqn(PARAMETER_ROOT, Fqn.fromString(name)), cache.getInvocationContext()
.getGlobalTransaction());
}
+
+ /**
+ * @see org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitor#addIndexUpdateMonitorListener(org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitorListener)
+ */
+ public void addIndexUpdateMonitorListener(IndexUpdateMonitorListener listener)
+ {
+ listeners.add(listener);
+ }
+
+ /**
+ * Called when a node of the cache has been modified. It will be used to trigger events
+ * when the value of <code>updateInProgress</code> has been changed remotely
+ * @param event the event
+ */
+ @NodeModified
+ public void cacheNodeModified(NodeModifiedEvent event)
+ {
+ if (!event.isPre() && event.getFqn().equals(PARAMETER_ROOT))
+ {
+ Object value = null;
+ Map<?, ?> data = event.getData();
+ if (data == null)
+ {
+ log.warn("The data map is empty");
+ }
+ else
+ {
+ value = data.get(PARAMETER_NAME);
+ }
+ if (value == null)
+ {
+ log.warn("The data cannot be found, we will try to get it from the cache");
+ value = cache.get(PARAMETER_ROOT, PARAMETER_NAME);
+ }
+ boolean updateInProgress = value != null ? (Boolean)value : false;
+ for (IndexUpdateMonitorListener listener : listeners)
+ {
+ listener.onUpdateInProgressChange(updateInProgress);
+ }
+ }
+ }
}
Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/DefaultIndexUpdateMonitor.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/DefaultIndexUpdateMonitor.java 2010-01-13 09:34:22 UTC (rev 1372)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/DefaultIndexUpdateMonitor.java 2010-01-13 10:14:17 UTC (rev 1373)
@@ -20,6 +20,8 @@
import org.jboss.cache.lock.LockType;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@@ -29,16 +31,21 @@
*/
public class DefaultIndexUpdateMonitor implements IndexUpdateMonitor
{
- private AtomicBoolean updateInProgress;
-
+ private final AtomicBoolean updateInProgress;
+
/**
+ * The list of all the listeners
+ */
+ private final List<IndexUpdateMonitorListener> listeners;
+
+ /**
* @param semaphore
*/
public DefaultIndexUpdateMonitor()
{
super();
- this.updateInProgress = new AtomicBoolean();
- this.updateInProgress.set(false);
+ this.updateInProgress = new AtomicBoolean(false);
+ this.listeners = new CopyOnWriteArrayList<IndexUpdateMonitorListener>();
}
/**
@@ -57,6 +64,10 @@
public void setUpdateInProgress(boolean updateInProgress)
{
this.updateInProgress.set(updateInProgress);
+ for (IndexUpdateMonitorListener listener : listeners)
+ {
+ listener.onUpdateInProgressChange(updateInProgress);
+ }
}
/**
@@ -82,4 +93,12 @@
return false;
}
+ /**
+ * @see org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitor#addIndexUpdateMonitorListener(org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitorListener)
+ */
+ public void addIndexUpdateMonitorListener(IndexUpdateMonitorListener listener)
+ {
+ listeners.add(listener);
+ }
+
}
Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexUpdateMonitor.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexUpdateMonitor.java 2010-01-13 09:34:22 UTC (rev 1372)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexUpdateMonitor.java 2010-01-13 10:14:17 UTC (rev 1373)
@@ -38,6 +38,12 @@
void setUpdateInProgress(boolean updateInProgress);
/**
+ * Adds a new listener of type {@link IndexUpdateMonitorListener}
+ * @param listener the listener to add
+ */
+ void addIndexUpdateMonitorListener(IndexUpdateMonitorListener listener);
+
+ /**
* Returns true if the node is locked (either for reading or writing) by anyone, and false otherwise.
* @param name
* @return
Added: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexUpdateMonitorListener.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexUpdateMonitorListener.java (rev 0)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/IndexUpdateMonitorListener.java 2010-01-13 10:14:17 UTC (rev 1373)
@@ -0,0 +1,18 @@
+package org.exoplatform.services.jcr.impl.core.query.lucene;
+
+/**
+ * This interface allows you to execute a given action when the state of the {@link IndexUpdateMonitor}
+ * changes.
+ *
+ * @author <a href="mailto:nicolas.filotto at exoplatform.com">Nicolas Filotto</a>
+ * @version $Id$
+ *
+ */
+public interface IndexUpdateMonitorListener
+{
+ /**
+ * This method is called when the flag <code>updateInProgress</code> changes of value
+ * @param updateInProgress the new value of the flag
+ */
+ void onUpdateInProgressChange(boolean updateInProgress);
+}
Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/MultiIndex.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/MultiIndex.java 2010-01-13 09:34:22 UTC (rev 1372)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/MultiIndex.java 2010-01-13 10:14:17 UTC (rev 1373)
@@ -77,7 +77,7 @@
* thread and reader threads is done using {@link #updateMonitor} and
* {@link #updateInProgress}.
*/
-public class MultiIndex implements IndexerIoModeListener
+public class MultiIndex implements IndexerIoModeListener, IndexUpdateMonitorListener
{
/**
@@ -246,6 +246,7 @@
this.indexNames.read();
modeHandler.addIndexerIoModeListener(this);
+ indexUpdateMonitor.addIndexUpdateMonitorListener(this);
// as of 1.5 deletable file is not used anymore
removeDeletable();
@@ -261,9 +262,9 @@
this.indexingQueue = new IndexingQueue(store);
// copy current index names
Set<String> currentNames = new HashSet<String>(indexNames.getNames());
-
+
// open persistent indexes
- for (String name:currentNames)
+ for (String name : currentNames)
{
// only open if it still exists
// it is possible that indexNames still contains a name for
@@ -2398,7 +2399,7 @@
updateMonitor.notifyAll();
releaseMultiReader();
}
-
+
this.redoLog = new RedoLog(indexDir);
redoLogApplied = redoLog.hasEntries();
@@ -2488,4 +2489,30 @@
}
}
}
+
+ /**
+ * @see org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitorListener#onUpdateInProgressChange(boolean)
+ */
+ public void onUpdateInProgressChange(boolean updateInProgress)
+ {
+ if (modeHandler.getMode() == IndexerIoMode.READ_ONLY)
+ {
+ if (!updateInProgress)
+ {
+ // wake up the sleeping threads
+ try
+ {
+ synchronized (updateMonitor)
+ {
+ updateMonitor.notifyAll();
+ releaseMultiReader();
+ }
+ }
+ catch (IOException e)
+ {
+ log.error("An erro occurs while trying to wake up the sleeping threads", e);
+ }
+ }
+ }
+ }
}
More information about the exo-jcr-commits
mailing list