Author: chris.laprun(a)jboss.com
Date: 2011-09-02 18:12:24 -0400 (Fri, 02 Sep 2011)
New Revision: 7295
Added:
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/ConsumerCache.java
Modified:
components/wsrp/branches/clustering/admin-gui/src/main/java/org/gatein/wsrp/admin/ui/ConsumerManagerBean.java
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/ConsumerRegistry.java
components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/wsrp/consumer/registry/ConsumerRegistryTestCase.java
components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/wsrp/test/support/MockConsumerRegistry.java
components/wsrp/branches/clustering/jcr-impl/src/main/java/org/gatein/wsrp/consumer/registry/JCRConsumerRegistry.java
components/wsrp/branches/clustering/jcr-impl/src/test/java/org/gatein/wsrp/consumer/registry/JCRConsumerRegistryTestCase.java
Log:
- Added ConsumerCache interface to allow for local caching of consumers to avoid having to
go to the persistence layer all the time. Default implementation is in-memory, it'll
be possible to create distributed versions.
- Added ConsumerRegistry.getConfiguredConsumerNumber() method to improve performance.
- Fixed JCR test issues by removing the nodes that were created in tearDown so that tests
can start with a fresh state.
Modified:
components/wsrp/branches/clustering/admin-gui/src/main/java/org/gatein/wsrp/admin/ui/ConsumerManagerBean.java
===================================================================
---
components/wsrp/branches/clustering/admin-gui/src/main/java/org/gatein/wsrp/admin/ui/ConsumerManagerBean.java 2011-09-02
14:38:36 UTC (rev 7294)
+++
components/wsrp/branches/clustering/admin-gui/src/main/java/org/gatein/wsrp/admin/ui/ConsumerManagerBean.java 2011-09-02
22:12:24 UTC (rev 7295)
@@ -93,7 +93,7 @@
public boolean isConsumersEmpty()
{
- return getRegistry().getConfiguredConsumers().isEmpty();
+ return getRegistry().getConfiguredConsumerNumber() == 0;
}
public List<WSRPConsumer> getConsumers()
Modified:
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java
===================================================================
---
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java 2011-09-02
14:38:36 UTC (rev 7294)
+++
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java 2011-09-02
22:12:24 UTC (rev 7295)
@@ -40,8 +40,10 @@
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
@@ -61,6 +63,13 @@
private static final Logger log =
LoggerFactory.getLogger(AbstractConsumerRegistry.class);
+ private ConsumerCache consumers = new InMemoryConsumerCache();
+
+ public void setConsumerCache(ConsumerCache consumers)
+ {
+ this.consumers = consumers;
+ }
+
public FederatingPortletInvoker getFederatingPortletInvoker()
{
return federatingPortletInvoker;
@@ -127,7 +136,11 @@
}
deactivateConsumer(consumer);
+
delete(info);
+
+ // remove from cache
+ consumers.removeConsumer(id);
}
else
{
@@ -158,7 +171,12 @@
// make sure we set the registry after loading from DB since registry is not
persisted.
producerInfo.setRegistry(this);
- return new WSRPConsumerImpl(producerInfo, migrationService);
+ final WSRPConsumerImpl consumer = new WSRPConsumerImpl(producerInfo,
migrationService);
+
+ // cache consumer
+ consumers.putConsumer(producerInfo.getId(), consumer);
+
+ return consumer;
}
public void activateConsumerWith(String id) throws ConsumerException
@@ -213,6 +231,10 @@
federatingPortletInvoker.unregisterInvoker(oldId);
federatingPortletInvoker.registerInvoker(producerInfo.getId(), consumer);
}
+
+ // update cache
+ consumers.removeConsumer(oldId);
+ consumers.putConsumer(producerInfo.getId(), consumer);
}
return oldId;
@@ -225,6 +247,8 @@
public void reloadConsumers()
{
+ consumers.clear();
+
Iterator<ProducerInfo> producerInfos = getProducerInfosFromStorage();
// load the configured producers
@@ -270,15 +294,25 @@
{
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "consumer
id", null);
- ProducerInfo info = loadProducerInfo(id);
- if (info != null)
+ // try cache first
+ WSRPConsumer consumer = consumers.getConsumer(id);
+ if (consumer != null)
{
- return createConsumerFrom(info);
+ return consumer;
}
else
{
- return null;
+ ProducerInfo info = loadProducerInfo(id);
+ if (info != null)
+ {
+ return createConsumerFrom(info);
+ }
+ else
+ {
+ return null;
+ }
}
+
}
public boolean containsConsumer(String id)
@@ -324,6 +358,11 @@
};
}
+ public int getConfiguredConsumerNumber()
+ {
+ return getConfiguredConsumersIds().size();
+ }
+
public void registerOrDeregisterConsumerWith(String id, boolean register)
{
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "Consumer
identifier", "Registering or deregistering a Consumer");
@@ -423,15 +462,12 @@
protected List<WSRPConsumer> getConsumers(boolean startConsumers)
{
- Iterator<ProducerInfo> infos = getProducerInfosFromStorage();
- List<WSRPConsumer> consumers = new ArrayList<WSRPConsumer>();
- while (infos.hasNext())
+ final Collection<WSRPConsumer> consumerz = consumers.getConsumers();
+ for (WSRPConsumer consumer : consumerz)
{
- ProducerInfo info = infos.next();
- WSRPConsumer consumer = createConsumerFrom(info);
- consumers.add(consumer);
if (startConsumers)
{
+ final ProducerInfo info = consumer.getProducerInfo();
if (info.isActive() && !consumer.isActive())
{
try
@@ -447,7 +483,7 @@
}
}
- return consumers;
+ return new ArrayList<WSRPConsumer>(consumerz);
}
protected class ProducerInfoIterator implements Iterator<ProducerInfo>
@@ -474,4 +510,34 @@
throw new UnsupportedOperationException("remove not supported on this
iterator implementation");
}
}
+
+ protected class InMemoryConsumerCache implements ConsumerCache
+ {
+ private Map<String, WSRPConsumer> consumers = new HashMap<String,
WSRPConsumer>(11);
+
+ public Collection<WSRPConsumer> getConsumers()
+ {
+ return consumers.values();
+ }
+
+ public WSRPConsumer getConsumer(String id)
+ {
+ return consumers.get(id);
+ }
+
+ public WSRPConsumer removeConsumer(String id)
+ {
+ return consumers.remove(id);
+ }
+
+ public void putConsumer(String id, WSRPConsumer consumer)
+ {
+ consumers.put(id, consumer);
+ }
+
+ public void clear()
+ {
+ consumers.clear();
+ }
+ }
}
Added:
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/ConsumerCache.java
===================================================================
---
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/ConsumerCache.java
(rev 0)
+++
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/ConsumerCache.java 2011-09-02
22:12:24 UTC (rev 7295)
@@ -0,0 +1,42 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2011, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.consumer.registry;
+
+import org.gatein.wsrp.WSRPConsumer;
+
+import java.util.Collection;
+
+/** @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a> */
+public interface ConsumerCache
+{
+ Collection<WSRPConsumer> getConsumers();
+
+ WSRPConsumer getConsumer(String id);
+
+ WSRPConsumer removeConsumer(String id);
+
+ void putConsumer(String id, WSRPConsumer consumer);
+
+ void clear();
+}
Modified:
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/ConsumerRegistry.java
===================================================================
---
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/ConsumerRegistry.java 2011-09-02
14:38:36 UTC (rev 7294)
+++
components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/wsrp/consumer/registry/ConsumerRegistry.java 2011-09-02
22:12:24 UTC (rev 7295)
@@ -90,4 +90,6 @@
boolean containsConsumer(String id);
Collection<String> getConfiguredConsumersIds();
+
+ int getConfiguredConsumerNumber();
}
Modified:
components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/wsrp/consumer/registry/ConsumerRegistryTestCase.java
===================================================================
---
components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/wsrp/consumer/registry/ConsumerRegistryTestCase.java 2011-09-02
14:38:36 UTC (rev 7294)
+++
components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/wsrp/consumer/registry/ConsumerRegistryTestCase.java 2011-09-02
22:12:24 UTC (rev 7295)
@@ -89,6 +89,8 @@
assertNotNull(ids);
assertEquals(1, ids.size());
assertTrue(ids.contains(id));
+
+ assertEquals(1, registry.getConfiguredConsumerNumber());
}
public void testGetConsumer()
@@ -119,11 +121,13 @@
WSRPConsumer consumer = registry.createConsumer(id, null, null);
assertEquals(consumer, registry.getConsumer(id));
assertTrue(registry.containsConsumer(id));
+ assertEquals(1, registry.getConfiguredConsumerNumber());
registry.destroyConsumer(id);
assertFalse(registry.containsConsumer(id));
assertNull(registry.getConsumer(id));
+ assertEquals(0, registry.getConfiguredConsumerNumber());
}
public void testUpdateProducerInfo()
@@ -143,6 +147,7 @@
assertEquals(info, consumer.getProducerInfo());
assertEquals(consumer, registry.getConsumer("bar"));
assertTrue(registry.containsConsumer("bar"));
+ assertEquals(1, registry.getConfiguredConsumerNumber());
}
public void testStoppingShouldntStartConsumers() throws Exception
Modified:
components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/wsrp/test/support/MockConsumerRegistry.java
===================================================================
---
components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/wsrp/test/support/MockConsumerRegistry.java 2011-09-02
14:38:36 UTC (rev 7294)
+++
components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/wsrp/test/support/MockConsumerRegistry.java 2011-09-02
22:12:24 UTC (rev 7295)
@@ -173,4 +173,9 @@
{
return consumers.keySet();
}
+
+ public int getConfiguredConsumerNumber()
+ {
+ return consumers.size();
+ }
}
Modified:
components/wsrp/branches/clustering/jcr-impl/src/main/java/org/gatein/wsrp/consumer/registry/JCRConsumerRegistry.java
===================================================================
---
components/wsrp/branches/clustering/jcr-impl/src/main/java/org/gatein/wsrp/consumer/registry/JCRConsumerRegistry.java 2011-09-02
14:38:36 UTC (rev 7294)
+++
components/wsrp/branches/clustering/jcr-impl/src/main/java/org/gatein/wsrp/consumer/registry/JCRConsumerRegistry.java 2011-09-02
22:12:24 UTC (rev 7295)
@@ -61,7 +61,7 @@
private ChromatticPersister persister;
private boolean loadFromXMLIfNeeded;
private final String rootNodePath;
- private static final String PRODUCER_INFOS_PATH = ProducerInfosMapping.NODE_NAME;
+ static final String PRODUCER_INFOS_PATH = ProducerInfosMapping.NODE_NAME;
public static final List<Class> mappingClasses = new ArrayList<Class>(6);
private InputStream configurationIS;
@@ -244,12 +244,8 @@
ChromatticSession session = persister.getSession();
try
{
- final Session jcrSession = session.getJCRSession();
+ final RowIterator rows = getProducerInfoIds(session);
- final Query query =
jcrSession.getWorkspace().getQueryManager().createQuery("select producerid from
wsrp:producerinfo", Query.SQL);
- final QueryResult queryResult = query.execute();
- final RowIterator rows = queryResult.getRows();
-
final long size = rows.getSize();
if (size == 0)
{
@@ -279,6 +275,35 @@
}
}
+ private RowIterator getProducerInfoIds(ChromatticSession session) throws
RepositoryException
+ {
+ final Session jcrSession = session.getJCRSession();
+
+ final Query query =
jcrSession.getWorkspace().getQueryManager().createQuery("select producerid from
wsrp:producerinfo", Query.SQL);
+ final QueryResult queryResult = query.execute();
+ return queryResult.getRows();
+ }
+
+ @Override
+ public int getConfiguredConsumerNumber()
+ {
+ ChromatticSession session = persister.getSession();
+ try
+ {
+ final RowIterator ids = getProducerInfoIds(session);
+
+ return (int)ids.getSize();
+ }
+ catch (RepositoryException e)
+ {
+ throw new RuntimeException(e);
+ }
+ finally
+ {
+ persister.closeSession(false);
+ }
+ }
+
private ProducerInfosMapping getProducerInfosMapping(ChromatticSession session)
{
ProducerInfosMapping producerInfosMapping =
session.findByPath(ProducerInfosMapping.class, PRODUCER_INFOS_PATH);
@@ -343,6 +368,16 @@
return pim;
}
+ /**
+ * For tests
+ *
+ * @return
+ */
+ ChromatticPersister getPersister()
+ {
+ return persister;
+ }
+
private static class MappingToProducerInfoIterator implements
Iterator<ProducerInfo>
{
private Iterator<ProducerInfoMapping> mappings;
Modified:
components/wsrp/branches/clustering/jcr-impl/src/test/java/org/gatein/wsrp/consumer/registry/JCRConsumerRegistryTestCase.java
===================================================================
---
components/wsrp/branches/clustering/jcr-impl/src/test/java/org/gatein/wsrp/consumer/registry/JCRConsumerRegistryTestCase.java 2011-09-02
14:38:36 UTC (rev 7294)
+++
components/wsrp/branches/clustering/jcr-impl/src/test/java/org/gatein/wsrp/consumer/registry/JCRConsumerRegistryTestCase.java 2011-09-02
22:12:24 UTC (rev 7295)
@@ -26,7 +26,12 @@
import org.chromattic.api.ChromatticBuilder;
import org.gatein.pc.federation.impl.FederatingPortletInvokerService;
import org.gatein.wsrp.jcr.BaseChromatticPersister;
+import org.gatein.wsrp.jcr.ChromatticPersister;
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Session;
+
/**
* This is essentially the same class as
org.gatein.wsrp.state.consumer.ConsumerRegistryTestCase in WSRP consumer
* module tests.
@@ -36,6 +41,9 @@
*/
public class JCRConsumerRegistryTestCase extends ConsumerRegistryTestCase
{
+
+ private String workspaceName;
+
/**
* Incremented for each test so that we can append it to the workspace name and work
with a "clean" DB for each
* test.
@@ -45,7 +53,7 @@
protected void setUp() throws Exception
{
final long random = Math.round(Math.abs(100000 * Math.random()));
- String workspaceName = "/wsrp-jcr-test" + random;
+ workspaceName = "/wsrp-jcr-test" + random;
BaseChromatticPersister persister = new BaseChromatticPersister(workspaceName)
{
@Override
@@ -62,6 +70,23 @@
}
@Override
+ protected void tearDown() throws Exception
+ {
+ // remove node containing consumer informations so that we can start with a clean
state
+ final ChromatticPersister persister =
((JCRConsumerRegistry)registry).getPersister();
+ final Session session = persister.getSession().getJCRSession();
+ final Node rootNode = session.getRootNode();
+ final NodeIterator nodes = rootNode.getNodes();
+ while (nodes.hasNext())
+ {
+ nodes.nextNode().remove();
+ }
+
+ // then save
+ persister.closeSession(true);
+ }
+
+ @Override
public void testStoppingShouldntStartConsumers() throws Exception
{
// override to bypass this test as I couldn't find a way to make it work
properly (i.e. how to inject a Mock