From do-not-reply at jboss.org Fri Sep 2 18:12:25 2011 Content-Type: multipart/mixed; boundary="===============3168000382052769911==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: gatein-commits at lists.jboss.org Subject: [gatein-commits] gatein SVN: r7295 - in components/wsrp/branches/clustering: consumer/src/main/java/org/gatein/wsrp/consumer/registry and 4 other directories. Date: Fri, 02 Sep 2011 18:12:25 -0400 Message-ID: <201109022212.p82MCPFt024146@svn01.web.mwc.hst.phx2.redhat.com> --===============3168000382052769911== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/ws= rp/consumer/registry/ConsumerCache.java Modified: components/wsrp/branches/clustering/admin-gui/src/main/java/org/gatein/w= srp/admin/ui/ConsumerManagerBean.java components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/ws= rp/consumer/registry/AbstractConsumerRegistry.java components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/ws= rp/consumer/registry/ConsumerRegistry.java components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/ws= rp/consumer/registry/ConsumerRegistryTestCase.java components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/ws= rp/test/support/MockConsumerRegistry.java components/wsrp/branches/clustering/jcr-impl/src/main/java/org/gatein/ws= rp/consumer/registry/JCRConsumerRegistry.java components/wsrp/branches/clustering/jcr-impl/src/test/java/org/gatein/ws= rp/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 implement= ation is in-memory, it'll be possible to create distributed versions. - Added ConsumerRegistry.getConfiguredConsumerNumber() method to improve pe= rformance. - 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/g= atein/wsrp/admin/ui/ConsumerManagerBean.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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() =3D=3D 0; } = public List getConsumers() Modified: components/wsrp/branches/clustering/consumer/src/main/java/org/ga= tein/wsrp/consumer/registry/AbstractConsumerRegistry.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/w= srp/consumer/registry/AbstractConsumerRegistry.java 2011-09-02 14:38:36 UTC= (rev 7294) +++ components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/w= srp/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 Chris Laprun @@ -61,6 +63,13 @@ = private static final Logger log =3D LoggerFactory.getLogger(AbstractCon= sumerRegistry.class); = + private ConsumerCache consumers =3D new InMemoryConsumerCache(); + + public void setConsumerCache(ConsumerCache consumers) + { + this.consumers =3D 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 registr= y is not persisted. producerInfo.setRegistry(this); = - return new WSRPConsumerImpl(producerInfo, migrationService); + final WSRPConsumerImpl consumer =3D new WSRPConsumerImpl(producerInf= o, 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 producerInfos =3D getProducerInfosFromStorage= (); = // load the configured producers @@ -270,15 +294,25 @@ { ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "consu= mer id", null); = - ProducerInfo info =3D loadProducerInfo(id); - if (info !=3D null) + // try cache first + WSRPConsumer consumer =3D consumers.getConsumer(id); + if (consumer !=3D null) { - return createConsumerFrom(info); + return consumer; } else { - return null; + ProducerInfo info =3D loadProducerInfo(id); + if (info !=3D 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 registe= r) { ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "Consu= mer identifier", "Registering or deregistering a Consumer"); @@ -423,15 +462,12 @@ = protected List getConsumers(boolean startConsumers) { - Iterator infos =3D getProducerInfosFromStorage(); - List consumers =3D new ArrayList(); - while (infos.hasNext()) + final Collection consumerz =3D consumers.getConsumers(= ); + for (WSRPConsumer consumer : consumerz) { - ProducerInfo info =3D infos.next(); - WSRPConsumer consumer =3D createConsumerFrom(info); - consumers.add(consumer); if (startConsumers) { + final ProducerInfo info =3D consumer.getProducerInfo(); if (info.isActive() && !consumer.isActive()) { try @@ -447,7 +483,7 @@ } } = - return consumers; + return new ArrayList(consumerz); } = protected class ProducerInfoIterator implements Iterator @@ -474,4 +510,34 @@ throw new UnsupportedOperationException("remove not supported on = this iterator implementation"); } } + + protected class InMemoryConsumerCache implements ConsumerCache + { + private Map consumers =3D new HashMap(11); + + public Collection 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/gatei= n/wsrp/consumer/registry/ConsumerCache.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/w= srp/consumer/registry/ConsumerCache.java (rev 0) +++ components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/w= srp/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 Chris Laprun */ +public interface ConsumerCache +{ + Collection 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/ga= tein/wsrp/consumer/registry/ConsumerRegistry.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/w= srp/consumer/registry/ConsumerRegistry.java 2011-09-02 14:38:36 UTC (rev 72= 94) +++ components/wsrp/branches/clustering/consumer/src/main/java/org/gatein/w= srp/consumer/registry/ConsumerRegistry.java 2011-09-02 22:12:24 UTC (rev 72= 95) @@ -90,4 +90,6 @@ boolean containsConsumer(String id); = Collection getConfiguredConsumersIds(); + + int getConfiguredConsumerNumber(); } Modified: components/wsrp/branches/clustering/consumer/src/test/java/org/ga= tein/wsrp/consumer/registry/ConsumerRegistryTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/w= srp/consumer/registry/ConsumerRegistryTestCase.java 2011-09-02 14:38:36 UTC= (rev 7294) +++ components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/w= srp/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 =3D 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/ga= tein/wsrp/test/support/MockConsumerRegistry.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/w= srp/test/support/MockConsumerRegistry.java 2011-09-02 14:38:36 UTC (rev 729= 4) +++ components/wsrp/branches/clustering/consumer/src/test/java/org/gatein/w= srp/test/support/MockConsumerRegistry.java 2011-09-02 22:12:24 UTC (rev 729= 5) @@ -173,4 +173,9 @@ { return consumers.keySet(); } + + public int getConfiguredConsumerNumber() + { + return consumers.size(); + } } Modified: components/wsrp/branches/clustering/jcr-impl/src/main/java/org/ga= tein/wsrp/consumer/registry/JCRConsumerRegistry.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/branches/clustering/jcr-impl/src/main/java/org/gatein/w= srp/consumer/registry/JCRConsumerRegistry.java 2011-09-02 14:38:36 UTC (rev= 7294) +++ components/wsrp/branches/clustering/jcr-impl/src/main/java/org/gatein/w= srp/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 =3D ProducerInfosMappin= g.NODE_NAME; + static final String PRODUCER_INFOS_PATH =3D ProducerInfosMapping.NODE_N= AME; = public static final List mappingClasses =3D new ArrayList= (6); private InputStream configurationIS; @@ -244,12 +244,8 @@ ChromatticSession session =3D persister.getSession(); try { - final Session jcrSession =3D session.getJCRSession(); + final RowIterator rows =3D getProducerInfoIds(session); = - final Query query =3D jcrSession.getWorkspace().getQueryManager()= .createQuery("select producerid from wsrp:producerinfo", Query.SQL); - final QueryResult queryResult =3D query.execute(); - final RowIterator rows =3D queryResult.getRows(); - final long size =3D rows.getSize(); if (size =3D=3D 0) { @@ -279,6 +275,35 @@ } } = + private RowIterator getProducerInfoIds(ChromatticSession session) throw= s RepositoryException + { + final Session jcrSession =3D session.getJCRSession(); + + final Query query =3D jcrSession.getWorkspace().getQueryManager().cr= eateQuery("select producerid from wsrp:producerinfo", Query.SQL); + final QueryResult queryResult =3D query.execute(); + return queryResult.getRows(); + } + + @Override + public int getConfiguredConsumerNumber() + { + ChromatticSession session =3D persister.getSession(); + try + { + final RowIterator ids =3D getProducerInfoIds(session); + + return (int)ids.getSize(); + } + catch (RepositoryException e) + { + throw new RuntimeException(e); + } + finally + { + persister.closeSession(false); + } + } + private ProducerInfosMapping getProducerInfosMapping(ChromatticSession = session) { ProducerInfosMapping producerInfosMapping =3D session.findByPath(Pro= ducerInfosMapping.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 mappings; Modified: components/wsrp/branches/clustering/jcr-impl/src/test/java/org/ga= tein/wsrp/consumer/registry/JCRConsumerRegistryTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/branches/clustering/jcr-impl/src/test/java/org/gatein/w= srp/consumer/registry/JCRConsumerRegistryTestCase.java 2011-09-02 14:38:36 = UTC (rev 7294) +++ components/wsrp/branches/clustering/jcr-impl/src/test/java/org/gatein/w= srp/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.Co= nsumerRegistryTestCase 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 =3D Math.round(Math.abs(100000 * Math.random())); - String workspaceName =3D "/wsrp-jcr-test" + random; + workspaceName =3D "/wsrp-jcr-test" + random; BaseChromatticPersister persister =3D new BaseChromatticPersister(wo= rkspaceName) { @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 =3D ((JCRConsumerRegistry)regist= ry).getPersister(); + final Session session =3D persister.getSession().getJCRSession(); + final Node rootNode =3D session.getRootNode(); + final NodeIterator nodes =3D 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 --===============3168000382052769911==--