You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by sb...@apache.org on 2015/11/21 01:58:52 UTC

incubator-geode git commit: GEODE-578: Allow cluster-config to work with cache.xml

Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-578 [created] b8e1fd224


GEODE-578: Allow cluster-config to work with cache.xml

Before starting a cache-server or creating a region, 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/b8e1fd22
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/b8e1fd22
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/b8e1fd22

Branch: refs/heads/feature/GEODE-578
Commit: b8e1fd2248f0e7b16e40d4ce81e45c27994e172c
Parents: 8552461
Author: Zach Gardner <za...@st5k.com>
Authored: Fri Nov 20 16:55:28 2015 -0800
Committer: Zach Gardner <za...@st5k.com>
Committed: Fri Nov 20 16:55:28 2015 -0800

----------------------------------------------------------------------
 .../internal/cache/xmlcache/CacheCreation.java  |  65 ++++--
 .../cache/xmlcache/CacheCreationJUnitTest.java  | 213 +++++++++++++++++++
 2 files changed, 266 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b8e1fd22/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..0238dd6 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,19 +559,50 @@ public class CacheCreation implements InternalCache {
     Integer serverPort = CacheServerLauncher.getServerPort();
     String serverBindAdd = CacheServerLauncher.getServerBindAddress();
     Boolean disableDefaultServer = CacheServerLauncher.disableDefaultServer.get();
+    startBridgeServers(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();
+      if (cache.getRegion(r.getName()) != null) {
+        getLogger().info("Skipped initializing declarative region since one already exists (perhaps through cluster configuration)");
+        continue;
+      }
+      r.createRoot(cache);
+    }
+  }
+
+  protected void startBridgeServers(List declarativeCacheServer, Cache cache, Integer serverPort, String serverBindAdd, Boolean disableDefaultServer) {
+
+    //Is it that the cacheserver configured in the XML is always parameterized?
+    //Why cant the user define port in the XML
+    // Eg:
+    //   <cache-server port="50505"/> - We might annoy user throwing the exception
+    if (declarativeCacheServer.size() > 1
         && (serverPort != null || serverBindAdd != null)) {
       throw new RuntimeException(
           LocalizedStrings.CacheServerLauncher_SERVER_PORT_MORE_THAN_ONE_CACHE_SERVER
               .toLocalizedString());
     }
-    
-    if (this.getCacheServers().isEmpty()
+
+
+    //Creating a default cache server should not be the responsibility of cache creation
+    //In case if there is no XML configuration - We dont create a cache server, so how does the client gets one?
+    if (declarativeCacheServer.isEmpty()
         && (serverPort != null || serverBindAdd != null)
         && (disableDefaultServer == null || !disableDefaultServer)) {
       boolean existingCacheServer = false;
-      
+
       List<CacheServer> cacheServers = cache.getCacheServers();
       if (cacheServers != null) {
         for(CacheServer cacheServer : cacheServers) {
@@ -586,13 +613,27 @@ public class CacheCreation implements InternalCache {
       }
       
       if (!existingCacheServer) {
-        this.getCacheServers().add(new CacheServerCreation(cache, false));
+        declarativeCacheServer.add(new CacheServerCreation((GemFireCacheImpl)cache, false));
       }
     }
     
-    for (Iterator iter = this.getCacheServers().iterator(); iter.hasNext();) {
+    for (Iterator iter = declarativeCacheServer.iterator(); iter.hasNext();) {
       CacheServerCreation bridge = (CacheServerCreation)iter.next();
-      
+
+      boolean startServer = true;
+      List<CacheServer> cacheServers = cache.getCacheServers();
+      if (cacheServers != null) {
+        for (CacheServer cacheServer : cacheServers) {
+          if (bridge.getPort() == cacheServer.getPort()) {
+            startServer = false;
+          }
+        }
+      }
+
+      if (!startServer) {
+        continue;
+      }
+
       CacheServerImpl impl = (CacheServerImpl)cache.addCacheServer();
       impl.configureFrom(bridge);
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b8e1fd22/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..a81befd
--- /dev/null
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/cache/xmlcache/CacheCreationJUnitTest.java
@@ -0,0 +1,213 @@
+package com.gemstone.gemfire.internal.cache.xmlcache;
+
+import com.gemstone.gemfire.cache.Region;
+import com.gemstone.gemfire.cache.server.CacheServer;
+import com.gemstone.gemfire.internal.cache.CacheServerImpl;
+import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
+import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
+
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+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 declarativeRegionIsNotCreatedIfOneExistsAlready() {
+    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(mock(Region.class));
+
+    cacheCreation.initializeRegions(declarativeRegions, cache);
+
+    verify(declarativeRegion, never()).createRoot(cache);
+  }
+
+  @Test
+  //we dont know the desired behaviour
+  public void defaultCacheServerIsCreatedWithDefaultPortWhenNoDeclarativeServerIsConfigured() {
+    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.startBridgeServers(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.startBridgeServers(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.startBridgeServers(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.startBridgeServers(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.startBridgeServers(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.startBridgeServers(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.startBridgeServers(cacheCreation.getCacheServers(), cache, configuredServerPort, configuredServerBindAddress, disableDefaultCacheServer);
+  }
+}