Repository: incubator-geode Updated Branches: refs/heads/develop 1b0905c9d -> 6e252309e
GEODE-578: Allow cluster-config to work with cache.xml Before starting a cache-server, verify that one has not been created by cluster config. skip if already created. Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/6e252309 Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/6e252309 Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/6e252309 Branch: refs/heads/develop Commit: 6e252309ed86add5b89e7d3eb6d8d950ff7db08e Parents: 1b0905c Author: Swapnil Bawaskar <sbawas...@pivotal.io> Authored: Wed Nov 25 11:43:16 2015 -0800 Committer: Swapnil Bawaskar <sbawas...@pivotal.io> Committed: Wed Nov 25 11:45:37 2015 -0800 ---------------------------------------------------------------------- .../internal/cache/xmlcache/CacheCreation.java | 72 ++++--- .../cache/xmlcache/CacheCreationJUnitTest.java | 207 +++++++++++++++++++ 2 files changed, 256 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/6e252309/gemfire-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java index 14076b5..ed67ca4 100644 --- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java +++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreation.java @@ -547,11 +547,7 @@ public class CacheCreation implements InternalCache { cache.setRegionAttributes(id, attrs); } - Iterator it = this.roots.values().iterator(); - while (it.hasNext()) { - RegionCreation r = (RegionCreation)it.next(); - r.createRoot(cache); - } + initializeRegions(this.roots, cache); cache.readyDynamicRegionFactory(); @@ -563,38 +559,76 @@ public class CacheCreation implements InternalCache { Integer serverPort = CacheServerLauncher.getServerPort(); String serverBindAdd = CacheServerLauncher.getServerBindAddress(); Boolean disableDefaultServer = CacheServerLauncher.disableDefaultServer.get(); + startCacheServers(this.getCacheServers(), cache, serverPort, serverBindAdd, disableDefaultServer); + cache.setBackupFiles(this.backups); + cache.addDeclarableProperties(this.declarablePropertiesMap); + runInitializer(); + cache.setInitializer(getInitializer(), getInitializerProps()); - if (this.getCacheServers().size() > 1 + // UnitTest CacheXml81Test.testCacheExtension + // Create all extensions + extensionPoint.fireCreate(cache); + } + + protected void initializeRegions(Map declarativeRegions, Cache cache) { + Iterator it = declarativeRegions.values().iterator(); + while (it.hasNext()) { + RegionCreation r = (RegionCreation)it.next(); + r.createRoot(cache); + } + } + + /** + * starts declarative cache servers if a server is not running on the port already. + * Also adds a default server to the param declarativeCacheServers if a serverPort is specified. + */ + protected void startCacheServers(List declarativeCacheServers, Cache cache, Integer serverPort, String serverBindAdd, Boolean disableDefaultServer) { + + if (declarativeCacheServers.size() > 1 && (serverPort != null || serverBindAdd != null)) { throw new RuntimeException( LocalizedStrings.CacheServerLauncher_SERVER_PORT_MORE_THAN_ONE_CACHE_SERVER .toLocalizedString()); } - - if (this.getCacheServers().isEmpty() + + if (declarativeCacheServers.isEmpty() && (serverPort != null || serverBindAdd != null) && (disableDefaultServer == null || !disableDefaultServer)) { boolean existingCacheServer = false; - + List<CacheServer> cacheServers = cache.getCacheServers(); if (cacheServers != null) { for(CacheServer cacheServer : cacheServers) { - if (serverPort == cacheServer.getPort() && cacheServer.getBindAddress().equals(serverBindAdd)) { + if (serverPort == cacheServer.getPort()) { existingCacheServer = true; } } } if (!existingCacheServer) { - this.getCacheServers().add(new CacheServerCreation(cache, false)); + declarativeCacheServers.add(new CacheServerCreation((GemFireCacheImpl)cache, false)); } } - for (Iterator iter = this.getCacheServers().iterator(); iter.hasNext();) { - CacheServerCreation bridge = (CacheServerCreation)iter.next(); - + for (Iterator iter = declarativeCacheServers.iterator(); iter.hasNext();) { + CacheServerCreation declaredCacheServer = (CacheServerCreation)iter.next(); + + boolean startServer = true; + List<CacheServer> cacheServers = cache.getCacheServers(); + if (cacheServers != null) { + for (CacheServer cacheServer : cacheServers) { + if (declaredCacheServer.getPort() == cacheServer.getPort()) { + startServer = false; + } + } + } + + if (!startServer) { + continue; + } + CacheServerImpl impl = (CacheServerImpl)cache.addCacheServer(); - impl.configureFrom(bridge); + impl.configureFrom(declaredCacheServer); if (serverPort != null && serverPort != CacheServer.DEFAULT_PORT) { impl.setPort(serverPort); @@ -614,14 +648,6 @@ public class CacheCreation implements InternalCache { .toLocalizedString(impl), ex); } } - cache.setBackupFiles(this.backups); - cache.addDeclarableProperties(this.declarablePropertiesMap); - runInitializer(); - cache.setInitializer(getInitializer(), getInitializerProps()); - - // UnitTest CacheXml81Test.testCacheExtension - // Create all extensions - extensionPoint.fireCreate(cache); } /** http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/6e252309/gemfire-core/src/test/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreationJUnitTest.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreationJUnitTest.java b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreationJUnitTest.java new file mode 100644 index 0000000..2cdf80c --- /dev/null +++ b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreationJUnitTest.java @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.internal.cache.xmlcache; + +import com.gemstone.gemfire.cache.server.CacheServer; +import com.gemstone.gemfire.internal.cache.CacheServerImpl; +import com.gemstone.gemfire.internal.cache.GemFireCacheImpl; + + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.Mockito.*; + + +/** + * Created by pivotal on 11/19/15. + */ +public class CacheCreationJUnitTest { + + @Mock + GemFireCacheImpl cache; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void declarativeRegionIsCreated() { + CacheCreation cacheCreation = new CacheCreation(); + + RegionCreation declarativeRegion = mock(RegionCreation.class); + when(declarativeRegion.getName()).thenReturn("testRegion"); + + Map declarativeRegions = new HashMap(); + declarativeRegions.put("testRegion", declarativeRegion); + + when(cache.getRegion("testRegion")).thenReturn(null); + + cacheCreation.initializeRegions(declarativeRegions, cache); + + verify(declarativeRegion, times(1)).createRoot(cache); + } + + @Test + public void defaultCacheServerIsNotCreatedWithDefaultPortWhenNoDeclarativeServerIsConfigured() { + Boolean disableDefaultCacheServer = false; + Integer configuredServerPort = null; + String configuredServerBindAddress = null; + + CacheCreation cacheCreation = new CacheCreation(); + + CacheServerImpl mockServer = mock(CacheServerImpl.class); + when(cache.addCacheServer()).thenReturn(mockServer); + + List<CacheServer> cacheServers = new ArrayList<>(); + when(cache.getCacheServers()).thenReturn(cacheServers); + + cacheCreation.startCacheServers(cacheCreation.getCacheServers(), cache, configuredServerPort, configuredServerBindAddress, disableDefaultCacheServer); + + verify(cache, never()).addCacheServer(); + } + + @Test + public void defaultCacheServerIsNotCreatedWhenDisableDefaultCacheServerIsTrue() { + Boolean disableDefaultCacheServer = true; + Integer configuredServerPort = null; + String configuredServerBindAddress = null; + + CacheCreation cacheCreation = new CacheCreation(); + + CacheServerImpl mockServer = mock(CacheServerImpl.class); + when(cache.addCacheServer()).thenReturn(mockServer); + + List<CacheServer> cacheServers = new ArrayList<>(); + when(cache.getCacheServers()).thenReturn(cacheServers); + + cacheCreation.startCacheServers(cacheCreation.getCacheServers(), cache, configuredServerPort, configuredServerBindAddress, disableDefaultCacheServer); + + verify(cache, never()).addCacheServer(); + } + + @Test + public void defaultCacheServerIsCreatedWithConfiguredPortWhenNoDeclarativeServerIsConfigured() { + Boolean disableDefaultCacheServer = false; + Integer configuredServerPort = 9999; + String configuredServerBindAddress = null; + + CacheCreation cacheCreation = new CacheCreation(); + + CacheServerImpl mockServer = mock(CacheServerImpl.class); + when(cache.addCacheServer()).thenReturn(mockServer); + + List<CacheServer> cacheServers = new ArrayList<>(); + when(cache.getCacheServers()).thenReturn(cacheServers); + + cacheCreation.startCacheServers(cacheCreation.getCacheServers(), cache, configuredServerPort, configuredServerBindAddress, disableDefaultCacheServer); + + verify(cache, times(1)).addCacheServer(); + verify(mockServer).setPort(9999); + } + + @Test + public void declarativeCacheServerIsCreatedWithConfiguredServerPort() { + Integer configuredServerPort = 9999; + String configuredServerBindAddress = null; + Boolean disableDefaultCacheServer = false; + + CacheCreation cacheCreation = new CacheCreation(); + CacheServerCreation br1 = new CacheServerCreation(cacheCreation, false); + br1.setPort(8888); + cacheCreation.getCacheServers().add(br1); + + CacheServerImpl mockServer = mock(CacheServerImpl.class); + when(cache.addCacheServer()).thenReturn(mockServer); + + cacheCreation.startCacheServers(cacheCreation.getCacheServers(), cache, configuredServerPort, configuredServerBindAddress, disableDefaultCacheServer); + + verify(cache, times(1)).addCacheServer(); + verify(mockServer).setPort(configuredServerPort); + } + + @Test + public void cacheServerCreationIsSkippedWhenAServerExistsForAGivenPort() { + Integer configuredServerPort = null; + String configuredServerBindAddress = null; + Boolean disableDefaultCacheServer = false; + + CacheCreation cacheCreation = new CacheCreation(); + CacheServerCreation br1 = new CacheServerCreation(cacheCreation, false); + br1.setPort(40406); + cacheCreation.getCacheServers().add(br1); + + CacheServerImpl mockServer = mock(CacheServerImpl.class); + when(cache.addCacheServer()).thenReturn(mockServer); + when(mockServer.getPort()).thenReturn(40406); + + List<CacheServer> cacheServers = new ArrayList<>(); + cacheServers.add(mockServer); + + when(cache.getCacheServers()).thenReturn(cacheServers); + + cacheCreation.startCacheServers(cacheCreation.getCacheServers(), cache, configuredServerPort, configuredServerBindAddress, disableDefaultCacheServer); + + verify(cache, never()).addCacheServer(); + + } + + @Test + public void userCanCreateMultipleCacheServersDeclaratively() { + Integer configuredServerPort = null; + String configuredServerBindAddress = null; + Boolean disableDefaultCacheServer = false; + + CacheCreation cacheCreation = new CacheCreation(); + CacheServerCreation br1 = new CacheServerCreation(cacheCreation, false); + br1.setPort(40406); + CacheServerCreation br2 = new CacheServerCreation(cacheCreation, false); + br1.setPort(40407); + cacheCreation.getCacheServers().add(br1); + cacheCreation.getCacheServers().add(br2); + + CacheServerImpl mockServer = mock(CacheServerImpl.class); + when(cache.addCacheServer()).thenReturn(mockServer); + + cacheCreation.startCacheServers(cacheCreation.getCacheServers(), cache, configuredServerPort, configuredServerBindAddress, disableDefaultCacheServer); + + verify(cache, times(2)).addCacheServer(); + verify(mockServer).configureFrom(br1); + verify(mockServer).configureFrom(br2); + } + + @Test(expected = RuntimeException.class) + public void shouldThrowExceptionWhenUserTriesToDeclareMultipleCacheServersWithPort() { + Integer configuredServerPort = 50505; + String configuredServerBindAddress = "localhost[50505]"; + Boolean disableDefaultCacheServer = false; + + CacheCreation cacheCreation = new CacheCreation(); + cacheCreation.getCacheServers().add(new CacheServerCreation(cacheCreation, false)); + cacheCreation.getCacheServers().add(new CacheServerCreation(cacheCreation, false)); + + cacheCreation.startCacheServers(cacheCreation.getCacheServers(), cache, configuredServerPort, configuredServerBindAddress, disableDefaultCacheServer); + } +}