@Test public void testListRemoteCachePeersBeforeAnythingHasBeenSetReturnsAnEmptyUnmodifiableList() { invokeInitAndCheckDiscoveryServiceHasBeenStarted(); final Ehcache cache = context.mock(Ehcache.class); context.checking(new Expectations() {{ allowing(cache).getName(); will(returnValue("myCache")); }}); // Now list the peers, which should create a new entry in the map and return a list of CachePeer's List<CachePeer> listRemoteCachePeers = peerProvider.listRemoteCachePeers(cache); Assert.assertTrue(listRemoteCachePeers.isEmpty()); try { listRemoteCachePeers.add(context.mock(CachePeer.class, "dummyCachePeer")); Assert.fail("Did not expect to be able to add a member to the cache peers list that was returned"); } catch (UnsupportedOperationException e) { // OK } }
/** * Override this method which does RMI lookups */ @Override public CachePeer lookupRemoteCachePeer(String url) throws MalformedURLException, NotBoundException, RemoteException { CachePeer cachePeer = cachePeersPerUrlMap.get(url); Assert.assertNotNull("The cache peer for URL '" + url + "' has not been set up in the test", cachePeer); return cachePeer; }
private static void displayCacheInfo(PrintWriter writer, CacheManager cm) { writer.println(cm.getName() + " EHCACHE INFORMATION:"); writer.println("-------------------:"); writer.println("Manager Status: " + cm.getStatus()); writer.println("DiskStore Path: " + cm.getConfiguration().getDiskStoreConfiguration().getPath()); writer.println(); writer.println("Caches: "); Map<String, CacheManagerPeerProvider> peerProvs = cm.getCacheManagerPeerProviders(); String[] cacheNames = cm.getCacheNames(); for(String n : cacheNames) { Ehcache c = cm.getCache(n); writer.println("\t" + c.getName() + " @" + c.hashCode()); writer.println("\tCache Status : " + c.getStatus()); writer.println("\tMax Heap : " + c.getCacheConfiguration().getMaxBytesLocalHeap()/(1024 * 1024) + "MB"); writer.println("\tMax Entries : " + c.getCacheConfiguration().getMaxEntriesLocalHeap()); writer.println("\tStatistics : " + getStats(c)); for (CacheManagerPeerProvider peerProv : peerProvs.values()) { List peers = peerProv.listRemoteCachePeers(c); for(Object o : peers) { CachePeer cp = (CachePeer) o; try { writer.println("\tReplicating with: " + cp.getUrl()); } catch (RemoteException e) {} } } writer.println(); } }
@Override public List<CachePeer> listRemoteCachePeers(final Ehcache cache) throws CacheException { // Ignore, only used for RMI return Collections.emptyList(); }
public final List<CachePeer> listRemoteCachePeers(Ehcache cache) throws CacheException { List<CachePeer> remoteCachePeers; String cacheName = cache.getName(); // First attempt to extract any cached peers list cachePeersLock.readLock().lock(); try { remoteCachePeers = cachePeers.get(cacheName); } finally { cachePeersLock.readLock().unlock(); } // Return anything we have if (remoteCachePeers != null) { return remoteCachePeers; } // Try to update, get the write lock for our cache peers map cachePeersLock.writeLock().lock(); try { // Daah-ble check! This may have been updated by another thread before we got the write lock. remoteCachePeers = cachePeers.get(cacheName); if (remoteCachePeers != null) { return remoteCachePeers; } // OK at this point we have the write lock and definitely need to create the list remoteCachePeers = new ArrayList<CachePeer>(); // Get the read lock for the peer URL's list peerUrlsLock.readLock().lock(); try { for (String rmiUrl : peerUrls) { String cacheRmiUrl = createFullyQualifiedRmiCacheUrl(rmiUrl, cacheName); CachePeer cachePeer = null; try { cachePeer = lookupRemoteCachePeer(cacheRmiUrl); remoteCachePeers.add(cachePeer); } catch (Exception e) { if (LOG.isDebugEnabled()) { LOG.debug("Exception looking up RMI URL {}. This may be normal if a node has gone offline, " + "or it may indicate network connectivity issues.", rmiUrl, e); } } } } finally { // Release read lock peerUrlsLock.readLock().unlock(); } // Finally set this up in the map before releasing the write lock, making it available to other threads remoteCachePeers = Collections.unmodifiableList(remoteCachePeers); cachePeers.put(cacheName, remoteCachePeers); } finally { cachePeersLock.writeLock().unlock(); } return remoteCachePeers; }
@Override public CachePeer lookupRemoteCachePeer(String url) throws MalformedURLException, NotBoundException, RemoteException { LOG.debug("Lookup remote cache peer URL {}", url); return (CachePeer)Naming.lookup(url); }
@Test public void testListRemoteCachePeersReturnsAListofPeersPerCacheAndUpdatesTheInternalMap() { invokeInitAndCheckDiscoveryServiceHasBeenStarted(); // Mock some stuff up final Ehcache cache = context.mock(Ehcache.class); final CachePeer googleCacheRemote = context.mock(CachePeer.class, "googleCacheRemote"); final CachePeer yahooCacheRemote = context.mock(CachePeer.class, "yahooCacheRemote"); // We are expecting RMI Naming.lookup calls to these URL's for the cache final String cacheName = "myCache"; peerProvider.addCachePeerPerUrl("//www.google.com:61616/" + cacheName, googleCacheRemote); peerProvider.addCachePeerPerUrl("//www.yahoo.com:61618/" + cacheName, yahooCacheRemote); context.checking(new Expectations() {{ allowing(discoveryServiceConfig).getRmiListenerPort(); will(returnValue(61616)); allowing(cache).getName(); will(returnValue(cacheName)); }}); // Make sure that the initial list is empty Assert.assertTrue(((Map<String,List<CachePeer>>)WhiteboxImpl.getInternalState(peerProvider, CACHE_PEERS_MAP_VARIABLE_NAME)).isEmpty()); // Set up a list of hosts. Because these hosts are actually resolved they need to be real. Set<CachePeerHost> cachePeerHosts = new HashSet<>(); cachePeerHosts.add(new CachePeerHost("www.google.com", 61616)); cachePeerHosts.add(new CachePeerHost("www.yahoo.com", 61618)); // Set the hosts peerProvider.setCachePeerHosts(cachePeerHosts); // Now list the peers, which should create a new entry in the map and return a list of CachePeer's List<CachePeer> listRemoteCachePeers = peerProvider.listRemoteCachePeers(cache); Assert.assertEquals(2, listRemoteCachePeers.size()); Assert.assertTrue(listRemoteCachePeers.contains(googleCacheRemote)); Assert.assertTrue(listRemoteCachePeers.contains(yahooCacheRemote)); try { listRemoteCachePeers.add(context.mock(CachePeer.class, "dummyCachePeer")); Assert.fail("Did not expect to be able to add a member to the cache peers list that was returned"); } catch (UnsupportedOperationException e) { // OK } // Check that the cache has also been populated with this list Map<String,List<CachePeer>> cachePeers = (Map<String,List<CachePeer>>)WhiteboxImpl.getInternalState(peerProvider, CACHE_PEERS_MAP_VARIABLE_NAME); Assert.assertEquals(1, cachePeers.size()); Assert.assertEquals(listRemoteCachePeers, cachePeers.get(cacheName)); // Another invocation should return exactly the same object, i.e. this list should not be recreated Assert.assertEquals(listRemoteCachePeers.hashCode(), peerProvider.listRemoteCachePeers(cache).hashCode()); }
@Test public void testListRemoteCachePeersCreatesANewListWhenAHostHasBeenRemoved() { invokeInitAndCheckDiscoveryServiceHasBeenStarted(); // Mock some stuff up final Ehcache cache = context.mock(Ehcache.class); final CachePeer googleCacheRemote = context.mock(CachePeer.class, "googleCacheRemote"); final CachePeer yahooCacheRemote = context.mock(CachePeer.class, "yahooCacheRemote"); // We are expecting RMI Naming.lookup calls to these URL's for the cache final String cacheName = "myCache"; peerProvider.addCachePeerPerUrl("//www.google.com:61616/" + cacheName, googleCacheRemote); peerProvider.addCachePeerPerUrl("//www.yahoo.com:61618/" + cacheName, yahooCacheRemote); context.checking(new Expectations() {{ allowing(discoveryServiceConfig).getRmiListenerPort(); will(returnValue(61616)); allowing(cache).getName(); will(returnValue(cacheName)); }}); // Make sure that the initial list is empty Assert.assertTrue(((Map<String,List<CachePeer>>)WhiteboxImpl.getInternalState(peerProvider, CACHE_PEERS_MAP_VARIABLE_NAME)).isEmpty()); // Set up a list of hosts. Because these hosts are actually resolved they need to be real. Set<CachePeerHost> cachePeerHosts = new HashSet<>(); CachePeerHost googleCachePeerHost = new CachePeerHost("www.google.com", 61616); cachePeerHosts.add(googleCachePeerHost); CachePeerHost yahooCachePeerHost = new CachePeerHost("www.yahoo.com", 61618); cachePeerHosts.add(yahooCachePeerHost); // Set the hosts peerProvider.setCachePeerHosts(cachePeerHosts); // Now list the peers, which should create a new entry in the map and return a list of CachePeer's List<CachePeer> listRemoteCachePeers = peerProvider.listRemoteCachePeers(cache); Assert.assertEquals(2, listRemoteCachePeers.size()); Assert.assertTrue(listRemoteCachePeers.contains(googleCacheRemote)); Assert.assertTrue(listRemoteCachePeers.contains(yahooCacheRemote)); // Now remove google cache peer Assert.assertTrue(cachePeerHosts.remove(googleCachePeerHost)); peerProvider.setCachePeerHosts(cachePeerHosts); listRemoteCachePeers = peerProvider.listRemoteCachePeers(cache); Assert.assertEquals(1, listRemoteCachePeers.size()); Assert.assertTrue(listRemoteCachePeers.contains(yahooCacheRemote)); }
private void addCachePeerPerUrl(String url, CachePeer cachePeer) { cachePeersPerUrlMap.put(url, cachePeer); }