You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2016/05/10 21:12:39 UTC

[1/3] incubator-geode git commit: GEODE-364: Amending Pool configuration to avoid the changing of connections, which could cause the test to fail.

Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-1376 9ea4e0720 -> b9b0757fa


GEODE-364: Amending Pool configuration to avoid the changing of connections, which could cause the test to fail.


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/bcf7c94f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/bcf7c94f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/bcf7c94f

Branch: refs/heads/feature/GEODE-1376
Commit: bcf7c94fb8247754eb2d73749d05be3a5011624c
Parents: f93c87f
Author: Udo Kohlmeyer <uk...@pivotal.io>
Authored: Wed May 11 05:47:10 2016 +1000
Committer: Udo Kohlmeyer <uk...@pivotal.io>
Committed: Wed May 11 05:51:44 2016 +1000

----------------------------------------------------------------------
 .../internal/cache/SingleHopStatsDUnitTest.java | 448 ++++++++-----------
 1 file changed, 198 insertions(+), 250 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bcf7c94f/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/SingleHopStatsDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/SingleHopStatsDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/SingleHopStatsDUnitTest.java
index 307f18f..671bdf7 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/SingleHopStatsDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/SingleHopStatsDUnitTest.java
@@ -16,20 +16,8 @@
  */
 package com.gemstone.gemfire.internal.cache;
 
-import java.io.IOException;
-import java.util.Map;
-import java.util.Properties;
-
-import org.junit.experimental.categories.Category;
-
-import com.gemstone.gemfire.cache.AttributesFactory;
-import com.gemstone.gemfire.cache.Cache;
-import com.gemstone.gemfire.cache.CacheFactory;
-import com.gemstone.gemfire.cache.DataPolicy;
-import com.gemstone.gemfire.cache.PartitionAttributesFactory;
-import com.gemstone.gemfire.cache.Region;
-import com.gemstone.gemfire.cache.RegionAttributes;
-import com.gemstone.gemfire.cache.Scope;
+import com.gemstone.gemfire.cache.*;
+import com.gemstone.gemfire.cache.client.ClientCache;
 import com.gemstone.gemfire.cache.client.Pool;
 import com.gemstone.gemfire.cache.client.PoolManager;
 import com.gemstone.gemfire.cache.client.internal.ClientMetadataService;
@@ -37,71 +25,43 @@ import com.gemstone.gemfire.cache.client.internal.ClientPartitionAdvisor;
 import com.gemstone.gemfire.cache.server.CacheServer;
 import com.gemstone.gemfire.cache30.CacheTestCase;
 import com.gemstone.gemfire.distributed.DistributedSystem;
-import com.gemstone.gemfire.distributed.Locator;
-import com.gemstone.gemfire.internal.AvailablePort;
 import com.gemstone.gemfire.internal.cache.execute.data.CustId;
 import com.gemstone.gemfire.internal.cache.execute.data.OrderId;
 import com.gemstone.gemfire.internal.cache.execute.data.ShipmentId;
 import com.gemstone.gemfire.internal.cache.tier.sockets.CacheServerTestUtil;
-import com.gemstone.gemfire.test.dunit.Assert;
-import com.gemstone.gemfire.test.dunit.DistributedTestUtils;
-import com.gemstone.gemfire.test.dunit.Host;
-import com.gemstone.gemfire.test.dunit.Invoke;
-import com.gemstone.gemfire.test.dunit.LogWriterUtils;
-import com.gemstone.gemfire.test.dunit.SerializableRunnable;
-import com.gemstone.gemfire.test.dunit.VM;
-import com.gemstone.gemfire.test.dunit.Wait;
-import com.gemstone.gemfire.test.dunit.WaitCriterion;
+import com.gemstone.gemfire.test.dunit.*;
 import com.gemstone.gemfire.test.junit.categories.FlakyTest;
+import com.jayway.awaitility.Awaitility;
+import org.junit.experimental.categories.Category;
 
-public class SingleHopStatsDUnitTest extends CacheTestCase {
+import java.io.IOException;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
 
-  private static final String Region_Name = "42010";
+public class SingleHopStatsDUnitTest extends CacheTestCase {
 
+  private final String Region_Name = "42010";
+  private final String ORDER_REGION_NAME = "ORDER";
+  private final String SHIPMENT_REGION_NAME = "SHIPMENT";
+  private final String CUSTOMER_REGION_NAME = "CUSTOMER";
   private VM member0 = null;
-
   private VM member1 = null;
-
   private VM member2 = null;
-
   private VM member3 = null;
 
-  private static Region region = null;
-
-  private static Region customerRegion = null;
-
-  private static Region orderRegion = null;
-
-  private static Region shipmentRegion = null;
-
-  private static Region regionWithResolver = null;
-
-  private static Cache cache = null;
-
-  private static final int locatorPort = 12345;
-
-  private static Locator locator = null;
-  
   private static long metaDataRefreshCount;
-  
   private static long nonSingleHopsCount;
-  
   private static long metaDataRefreshCount_Customer;
-  
   private static long nonSingleHopsCount_Customer;
-  
   private static long metaDataRefreshCount_Order;
-  
   private static long nonSingleHopsCount_Order;
-  
   private static long metaDataRefreshCount_Shipment;
-  
   private static long nonSingleHopsCount_Shipment;
-  
+
+
   public SingleHopStatsDUnitTest(String name) {
     super(name);
-
-    // TODO Auto-generated constructor stub
   }
 
   @Override
@@ -116,13 +76,13 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
   @Override
   public final void preTearDownCacheTestCase() throws Exception {
     // close the clients first
-    member0.invoke(() -> SingleHopStatsDUnitTest.closeCacheAndDisconnect());
-    member1.invoke(() -> SingleHopStatsDUnitTest.closeCacheAndDisconnect());
-    member2.invoke(() -> SingleHopStatsDUnitTest.closeCacheAndDisconnect());
-    member3.invoke(() -> SingleHopStatsDUnitTest.closeCacheAndDisconnect());
+    member0.invoke(() -> closeCacheAndDisconnect());
+    member1.invoke(() -> closeCacheAndDisconnect());
+    member2.invoke(() -> closeCacheAndDisconnect());
+    member3.invoke(() -> closeCacheAndDisconnect());
     closeCacheAndDisconnect();
   }
-  
+
   @Override
   public final void postTearDownCacheTestCase() throws Exception {
     try {
@@ -130,104 +90,95 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
       member1 = null;
       member2 = null;
       member3 = null;
-      cache = null;
-      Invoke.invokeInEveryVM(new SerializableRunnable() { public void run() { cache = null; } });
-
-    }
-    finally {
+    } finally {
       DistributedTestUtils.unregisterAllDataSerializersFromAllVms();
     }
   }
 
-  public static void closeCacheAndDisconnect() {
-    if (cache != null && !cache.isClosed()) {
-      cache.close();
-      cache.getDistributedSystem().disconnect();
+  private void closeCacheAndDisconnect() {
+    try {
+      Cache cache = CacheFactory.getAnyInstance();
+      if (cache != null && !cache.isClosed()) {
+        cache.close();
+        cache.getDistributedSystem().disconnect();
+      }
+    } catch (CacheClosedException e) {
     }
   }
 
   @Category(FlakyTest.class) // GEODE-364: random ports, time sensitive, waitForCriterions, magic numbers (113, 226)
   public void testClientStatsPR() {
-    VM server1 = member0;
-    VM server2 = member1;
-    VM server3 = member2;
-    VM client1 = member3;
-    
-    Integer port0 = (Integer)member0.invoke(() -> SingleHopStatsDUnitTest.createServerForStats( 0, 113,"No_Colocation"));
-    Integer port1 = (Integer)member1.invoke(() -> SingleHopStatsDUnitTest.createServerForStats( 0, 113,"No_Colocation"));
-    Integer port2 = (Integer)member2.invoke(() -> SingleHopStatsDUnitTest.createServerForStats( 0, 113,"No_Colocation"));
-     client1.invoke(() -> SingleHopStatsDUnitTest.createClient(port0, port1, port2,"No_Colocation"));
-     
+    Integer port0 = (Integer) member0.invoke(() -> createServerForStats(0, 113, "No_Colocation"));
+    Integer port1 = (Integer) member1.invoke(() -> createServerForStats(0, 113, "No_Colocation"));
+    Integer port2 = (Integer) member2.invoke(() -> createServerForStats(0, 113, "No_Colocation"));
+
+    member3.invoke(() -> createClient(port0, port1, port2, "No_Colocation"));
     createClient(port0, port1, port2, "No_Colocation");
 
-    client1.invoke(() -> SingleHopStatsDUnitTest.createPR("FirstClient", "No_Colocation"));
+    member3.invoke(() -> createPR("FirstClient", "No_Colocation"));
     createPR("SecondClient", "No_Colocation");
-    
-    client1.invoke(() -> SingleHopStatsDUnitTest.getPR("FirstClient", "No_Colocation"));
+
+    member3.invoke(() -> getPR("FirstClient", "No_Colocation"));
     getPR("SecondClient", "No_Colocation");
-    
-    client1.invoke(() -> SingleHopStatsDUnitTest.updatePR("FirstClient", "No_Colocation"));
+
+    member3.invoke(() -> updatePR("FirstClient", "No_Colocation"));
   }
-  
-  public void testClientStatsColocationPR(){
-    VM server1 = member0;
-    VM server2 = member1;
-    VM server3 = member2;
-    VM client1 = member3;
-    
-    Integer port0 = (Integer)member0.invoke(() -> SingleHopStatsDUnitTest.createServerForStats( 0, 4, "Colocation" ));
-    Integer port1 = (Integer)member1.invoke(() -> SingleHopStatsDUnitTest.createServerForStats( 0, 4, "Colocation" ));
-    Integer port2 = (Integer)member2.invoke(() -> SingleHopStatsDUnitTest.createServerForStats( 0, 4, "Colocation"));
-     client1.invoke(() -> SingleHopStatsDUnitTest.createClient(port0, port1, port2, "Colocation"));
+
+  public void testClientStatsColocationPR() {
+    Integer port0 = (Integer) member0.invoke(() -> createServerForStats(0, 4, "Colocation"));
+    Integer port1 = (Integer) member1.invoke(() -> createServerForStats(0, 4, "Colocation"));
+    Integer port2 = (Integer) member2.invoke(() -> createServerForStats(0, 4, "Colocation"));
+    member3.invoke(() -> createClient(port0, port1, port2, "Colocation"));
     createClient(port0, port1, port2, "Colocation");
 
-    client1.invoke(() -> SingleHopStatsDUnitTest.createPR("FirstClient", "Colocation"));
-    
-    client1.invoke(() -> SingleHopStatsDUnitTest.getPR("FirstClient", "Colocation"));
+    member3.invoke(() -> createPR("FirstClient", "Colocation"));
+
+    member3.invoke(() -> getPR("FirstClient", "Colocation"));
   }
 
-  
-  public static void createClient(int port0, int port1, int port2, String colocation) {
+  private void createClient(int port0, int port1, int port2, String colocation) {
     Properties props = new Properties();
     props = new Properties();
     props.setProperty("mcast-port", "0");
     props.setProperty("locators", "");
     CacheTestCase test = new SingleHopStatsDUnitTest(
         "SingleHopStatsDUnitTest");
-    DistributedSystem ds = test.getSystem(props);
-    cache = CacheFactory.create(ds);
+    DistributedSystem distributedSystem = test.getSystem(props);
+    Cache cache = CacheFactory.create(distributedSystem);
     assertNotNull(cache);
     CacheServerTestUtil.disableShufflingOfEndpoints();
     Pool p;
     try {
-      p = PoolManager.createFactory().addServer("localhost", port0).addServer(
-          "localhost", port1).addServer("localhost", port2)
+      p = PoolManager.createFactory().addServer("localhost", port0)
+          .addServer("localhost", port1).addServer("localhost", port2)
+          .setRetryAttempts(5)
+          .setMinConnections(1)
+          .setMaxConnections(1)
           .setSubscriptionEnabled(false)
           .create(Region_Name);
-    }
-    finally {
+    } finally {
       CacheServerTestUtil.enableShufflingOfEndpoints();
     }
-    createRegionInClientCache(p.getName(), colocation);
+    createRegionInClientCache(p.getName(), colocation, cache);
   }
 
-  public static int createServerForStats(int redundantCopies, int totalNoofBuckets, String colocation) {
+  private int createServerForStats(int redundantCopies, int totalNoofBuckets, String colocation) {
     CacheTestCase test = new SingleHopStatsDUnitTest(
         "SingleHopStatsDUnitTest");
-    cache = test.getCache();
+    Cache cache = test.getCache();
     CacheServer server = cache.addCacheServer();
-    int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
-    server.setPort(port);
+    server.setPort(0);
     server.setHostnameForClients("localhost");
     try {
       server.start();
-    }
-    catch (IOException e) {
+    } catch (IOException e) {
       Assert.fail("Failed to start server ", e);
     }
 
+    Region region = null;
+
     if (colocation.equals("No_Colocation")) {
-      if(totalNoofBuckets == 0){ //DR
+      if (totalNoofBuckets == 0) { //DR
         AttributesFactory attr = new AttributesFactory();
         attr.setScope(Scope.DISTRIBUTED_ACK);
         attr.setDataPolicy(DataPolicy.REPLICATE);
@@ -236,27 +187,27 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
         LogWriterUtils.getLogWriter().info(
             "Distributed Region " + Region_Name + " created Successfully :"
                 + region.toString());
-      }else{
-      PartitionAttributesFactory paf = new PartitionAttributesFactory();
-      paf.setRedundantCopies(redundantCopies).setTotalNumBuckets(
-          totalNoofBuckets);
-      AttributesFactory attr = new AttributesFactory();
-      attr.setPartitionAttributes(paf.create());
-      region = cache.createRegion(Region_Name, attr.create());
-      assertNotNull(region);
-      LogWriterUtils.getLogWriter().info(
-          "Partitioned Region " + Region_Name + " created Successfully :"
-              + region.toString());
+      } else {
+        PartitionAttributesFactory paf = new PartitionAttributesFactory();
+        paf.setRedundantCopies(redundantCopies).setTotalNumBuckets(
+            totalNoofBuckets);
+        AttributesFactory attr = new AttributesFactory();
+        attr.setPartitionAttributes(paf.create());
+        region = cache.createRegion(Region_Name, attr.create());
+        assertNotNull(region);
+        LogWriterUtils.getLogWriter().info(
+            "Partitioned Region " + Region_Name + " created Successfully :"
+                + region.toString());
       }
-    }
-    else {
+    } else {
       PartitionAttributesFactory paf = new PartitionAttributesFactory();
       paf.setRedundantCopies(redundantCopies).setTotalNumBuckets(
           totalNoofBuckets).setPartitionResolver(
-          new CustomerIDPartitionResolver("CustomerIDPartitionResolver"));
+          new CustomerIDPartitionResolver("CustomerIDPartitio"
+              + "nResolver"));
       AttributesFactory attr = new AttributesFactory();
       attr.setPartitionAttributes(paf.create());
-      customerRegion = cache.createRegion("CUSTOMER", attr.create());
+      Region customerRegion = cache.createRegion(CUSTOMER_REGION_NAME, attr.create());
       assertNotNull(customerRegion);
       LogWriterUtils.getLogWriter().info(
           "Partitioned Region CUSTOMER created Successfully :"
@@ -264,11 +215,11 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
 
       paf = new PartitionAttributesFactory();
       paf.setRedundantCopies(redundantCopies).setTotalNumBuckets(
-          totalNoofBuckets).setColocatedWith("CUSTOMER").setPartitionResolver(
+          totalNoofBuckets).setColocatedWith(CUSTOMER_REGION_NAME).setPartitionResolver(
           new CustomerIDPartitionResolver("CustomerIDPartitionResolver"));
       attr = new AttributesFactory();
       attr.setPartitionAttributes(paf.create());
-      orderRegion = cache.createRegion("ORDER", attr.create());
+      Region orderRegion = cache.createRegion(ORDER_REGION_NAME, attr.create());
       assertNotNull(orderRegion);
       LogWriterUtils.getLogWriter().info(
           "Partitioned Region ORDER created Successfully :"
@@ -276,20 +227,21 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
 
       paf = new PartitionAttributesFactory();
       paf.setRedundantCopies(redundantCopies).setTotalNumBuckets(
-          totalNoofBuckets).setColocatedWith("ORDER").setPartitionResolver(
+          totalNoofBuckets).setColocatedWith(ORDER_REGION_NAME).setPartitionResolver(
           new CustomerIDPartitionResolver("CustomerIDPartitionResolver"));
       attr = new AttributesFactory();
       attr.setPartitionAttributes(paf.create());
-      shipmentRegion = cache.createRegion("SHIPMENT", attr.create());
+      Region shipmentRegion = cache.createRegion(SHIPMENT_REGION_NAME, attr.create());
       assertNotNull(shipmentRegion);
       LogWriterUtils.getLogWriter().info(
           "Partitioned Region SHIPMENT created Successfully :"
               + shipmentRegion.toString());
     }
-    return port;
+    return server.getPort();
   }
-  
-  private static void createRegionInClientCache(String poolName, String colocation) {
+
+  private void createRegionInClientCache(String poolName, String colocation, Cache cache) {
+    Region region = null;
     if (colocation.equals("No_Colocation")) {
       AttributesFactory factory = new AttributesFactory();
       factory.setPoolName(poolName);
@@ -299,12 +251,11 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
       assertNotNull(region);
       LogWriterUtils.getLogWriter().info(
           "Region " + Region_Name + " created Successfully :" + region.toString());
-    }
-    else {
+    } else {
       AttributesFactory factory = new AttributesFactory();
       factory.setPoolName(poolName);
       RegionAttributes attrs = factory.create();
-      customerRegion = cache.createRegion("CUSTOMER", attrs);
+      Region customerRegion = cache.createRegion(CUSTOMER_REGION_NAME, attrs);
       assertNotNull(customerRegion);
       LogWriterUtils.getLogWriter().info(
           "Partitioned Region CUSTOMER created Successfully :"
@@ -313,7 +264,7 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
       factory = new AttributesFactory();
       factory.setPoolName(poolName);
       attrs = factory.create();
-      orderRegion = cache.createRegion("ORDER", attrs);
+      Region orderRegion = cache.createRegion(ORDER_REGION_NAME, attrs);
       assertNotNull(orderRegion);
       LogWriterUtils.getLogWriter().info(
           "Partitioned Region ORDER created Successfully :"
@@ -322,24 +273,27 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
       factory = new AttributesFactory();
       factory.setPoolName(poolName);
       attrs = factory.create();
-      shipmentRegion = cache.createRegion("SHIPMENT", attrs);
+      Region shipmentRegion = cache.createRegion("SHIPMENT", attrs);
       assertNotNull(shipmentRegion);
       LogWriterUtils.getLogWriter().info(
           "Partitioned Region SHIPMENT created Successfully :"
               + shipmentRegion.toString());
     }
   }
-  
-  public static void createPR(String fromClient,
-      String colocation) {
+
+  private void createPR(String fromClient, String colocation) {
+    GemFireCacheImpl cache = (GemFireCacheImpl) CacheFactory.getAnyInstance();
+    Region region = cache.getRegion(Region_Name);
+
+
     if (colocation.equals("No_Colocation")) {
       if (fromClient.equals("FirstClient")) {
-        
+
         System.out.println("first pass...");
         for (int i = 0; i < 113; i++) {
           region.create(new Integer(i), "create" + i);
         }
-        ClientMetadataService cms = ((GemFireCacheImpl)cache)
+        ClientMetadataService cms = ((GemFireCacheImpl) cache)
             .getClientMetadataService();
         final Map<String, ClientPartitionAdvisor> regionMetaData = cms
             .getClientPRMetadata_TEST_ONLY();
@@ -349,124 +303,121 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
         for (int i = 113; i < 226; i++) {
           region.create(new Integer(i), "create" + i);
         }
-        cms = ((GemFireCacheImpl)cache).getClientMetadataService();
+        cms = ((GemFireCacheImpl) cache).getClientMetadataService();
         // since PR metadata is fetched in a background executor thread
         // we need to wait for it to arrive for a bit
-        Wait.waitForCriterion(new WaitCriterion(){
-          public boolean done() {
-            return regionMetaData.size() == 1;
-          }
-          public String description() {
-            return "waiting for metadata to arrive: " + regionMetaData;
-          }
-          
-        }, 30000, 500, true);
+        Awaitility.await().timeout(120, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS)
+            .pollInterval(500, TimeUnit.MILLISECONDS).until(() -> {
+          return regionMetaData.size() == 1;
+        });
+
         assertTrue(regionMetaData.containsKey(region.getFullPath()));
-        ClientPartitionAdvisor prMetaData = regionMetaData.get(region
-            .getFullPath());
-        metaDataRefreshCount = ((LocalRegion)region).getCachePerfStats().getMetaDataRefreshCount();
-        nonSingleHopsCount = ((LocalRegion)region).getCachePerfStats().getNonSingleHopsCount();
+        regionMetaData.get(region.getFullPath());
+        metaDataRefreshCount = ((LocalRegion) region).getCachePerfStats().getMetaDataRefreshCount();
+        nonSingleHopsCount = ((LocalRegion) region).getCachePerfStats().getNonSingleHopsCount();
         assertTrue(metaDataRefreshCount != 0); // hops are not predictable
         assertTrue(nonSingleHopsCount != 0);
-      }
-      else {
+      } else {
         for (int i = 0; i < 226; i++) {
           region.create(new Integer(i), "create" + i);
         }
-        ClientMetadataService cms = ((GemFireCacheImpl)cache)
+        ClientMetadataService cms = ((GemFireCacheImpl) cache)
             .getClientMetadataService();
         Map<String, ClientPartitionAdvisor> regionMetaData = cms
             .getClientPRMetadata_TEST_ONLY();
         assertEquals(1, regionMetaData.size());
         assertTrue(regionMetaData.containsKey(region.getFullPath()));
-        ClientPartitionAdvisor prMetaData = regionMetaData.get(region
-            .getFullPath());
-        metaDataRefreshCount = ((LocalRegion)region).getCachePerfStats().getMetaDataRefreshCount();
-        nonSingleHopsCount = ((LocalRegion)region).getCachePerfStats().getNonSingleHopsCount();
+
+        regionMetaData.get(region.getFullPath());
+        metaDataRefreshCount = ((LocalRegion) region).getCachePerfStats().getMetaDataRefreshCount();
+        nonSingleHopsCount = ((LocalRegion) region).getCachePerfStats().getNonSingleHopsCount();
         assertTrue(metaDataRefreshCount != 0); // hops are not predictable
         assertTrue(nonSingleHopsCount != 0);
       }
+    } else {
+      createdColocatedPRData(cache);
     }
-    else {
-      for (int i = 0; i <= 20; i++) {
-        CustId custid = new CustId(i);
-        Customer customer = new Customer("name" + i, "Address" + i);
-        customerRegion.put(custid, customer);
-        for (int j = 1; j <= 10; j++) {
-          int oid = (i * 10) + j;
-          OrderId orderId = new OrderId(oid, custid);
-          Order order = new Order("OREDR" + oid);
-          orderRegion.put(orderId, order);
-          for (int k = 1; k <= 10; k++) {
-            int sid = (oid * 10) + k;
-            ShipmentId shipmentId = new ShipmentId(sid, orderId);
-            Shipment shipment = new Shipment("Shipment" + sid);
-            shipmentRegion.put(shipmentId, shipment);
-          }
+  }
+
+  private void createdColocatedPRData(GemFireCacheImpl cache) {
+    Region customerRegion = cache.getRegion(CUSTOMER_REGION_NAME);
+    Region orderRegion = cache.getRegion(ORDER_REGION_NAME);
+    Region shipmentRegion = cache.getRegion(SHIPMENT_REGION_NAME);
+    for (int i = 0; i <= 20; i++) {
+      CustId custid = new CustId(i);
+      Customer customer = new Customer("name" + i, "Address" + i);
+
+      customerRegion.put(custid, customer);
+      for (int j = 1; j <= 10; j++) {
+        int oid = (i * 10) + j;
+        OrderId orderId = new OrderId(oid, custid);
+        Order order = new Order(ORDER_REGION_NAME + oid);
+        orderRegion.put(orderId, order);
+        for (int k = 1; k <= 10; k++) {
+          int sid = (oid * 10) + k;
+          ShipmentId shipmentId = new ShipmentId(sid, orderId);
+          Shipment shipment = new Shipment("Shipment" + sid);
+          shipmentRegion.put(shipmentId, shipment);
         }
       }
-      ClientMetadataService cms = ((GemFireCacheImpl)cache)
-          .getClientMetadataService();
-      Map<String, ClientPartitionAdvisor> regionMetaData = cms
-          .getClientPRMetadata_TEST_ONLY();
-      assertEquals(3, regionMetaData.size());
-      assertTrue(regionMetaData.containsKey(customerRegion.getFullPath()));
-      ClientPartitionAdvisor prMetaData_Customer = regionMetaData
-          .get(customerRegion.getFullPath());
-      metaDataRefreshCount_Customer = ((LocalRegion)customerRegion).getCachePerfStats().getMetaDataRefreshCount();
-      nonSingleHopsCount_Customer = ((LocalRegion)customerRegion).getCachePerfStats().getNonSingleHopsCount();
-      assertTrue(metaDataRefreshCount_Customer != 0); // hops are not predictable
-      assertTrue(nonSingleHopsCount_Customer != 0);
-
-      ClientPartitionAdvisor prMetaData_Order = regionMetaData
-          .get(orderRegion.getFullPath());
-      metaDataRefreshCount_Order = ((LocalRegion)orderRegion).getCachePerfStats().getMetaDataRefreshCount();
-      nonSingleHopsCount_Order = ((LocalRegion)orderRegion).getCachePerfStats().getNonSingleHopsCount();
-      assertTrue(metaDataRefreshCount_Order == 0); 
-      assertTrue(nonSingleHopsCount_Order != 0);
-
-      ClientPartitionAdvisor prMetaData_Shipment = regionMetaData
-          .get(shipmentRegion.getFullPath());
-      metaDataRefreshCount_Shipment = ((LocalRegion)shipmentRegion).getCachePerfStats().getMetaDataRefreshCount();
-      nonSingleHopsCount_Shipment = ((LocalRegion)shipmentRegion).getCachePerfStats().getNonSingleHopsCount();
-      assertTrue(metaDataRefreshCount_Shipment == 0); 
-      assertTrue(nonSingleHopsCount_Shipment != 0);
     }
+    ClientMetadataService cms = cache.getClientMetadataService();
+    Map<String, ClientPartitionAdvisor> regionMetaData = cms
+        .getClientPRMetadata_TEST_ONLY();
+    assertEquals(3, regionMetaData.size());
+    assertTrue(regionMetaData.containsKey(customerRegion.getFullPath()));
+    regionMetaData.get(customerRegion.getFullPath());
+    metaDataRefreshCount_Customer = ((LocalRegion) customerRegion).getCachePerfStats().getMetaDataRefreshCount();
+    nonSingleHopsCount_Customer = ((LocalRegion) customerRegion).getCachePerfStats().getNonSingleHopsCount();
+    assertTrue(metaDataRefreshCount_Customer != 0); // hops are not predictable
+    assertTrue(nonSingleHopsCount_Customer != 0);
+
+    regionMetaData.get(orderRegion.getFullPath());
+    metaDataRefreshCount_Order = ((LocalRegion) orderRegion).getCachePerfStats().getMetaDataRefreshCount();
+    nonSingleHopsCount_Order = ((LocalRegion) orderRegion).getCachePerfStats().getNonSingleHopsCount();
+    assertTrue(metaDataRefreshCount_Order == 0);
+    assertTrue(nonSingleHopsCount_Order != 0);
+
+    regionMetaData.get(shipmentRegion.getFullPath());
+    metaDataRefreshCount_Shipment = ((LocalRegion) shipmentRegion).getCachePerfStats().getMetaDataRefreshCount();
+    nonSingleHopsCount_Shipment = ((LocalRegion) shipmentRegion).getCachePerfStats().getNonSingleHopsCount();
+    assertTrue(metaDataRefreshCount_Shipment == 0);
+    assertTrue(nonSingleHopsCount_Shipment != 0);
   }
-  
-  public static void getPR(String FromClient,
-      String colocation) {
+
+  private void getPR(String FromClient, String colocation) {
+    Cache cache = CacheFactory.getAnyInstance();
+    Region region = cache.getRegion(Region_Name);
+    Region customerRegion = cache.getRegion(CUSTOMER_REGION_NAME);
+    Region orderRegion = cache.getRegion(ORDER_REGION_NAME);
+    Region shipmentRegion = cache.getRegion("SHIPMENT");
     if (colocation.equals("No_Colocation")) {
       if (FromClient.equals("FirstClient")) {
         for (int i = 0; i < 226; i++) {
           region.get(new Integer(i));
         }
-        ClientMetadataService cms = ((GemFireCacheImpl)cache)
+        ClientMetadataService cms = ((GemFireCacheImpl) cache)
             .getClientMetadataService();
         Map<String, ClientPartitionAdvisor> regionMetaData = cms
             .getClientPRMetadata_TEST_ONLY();
         assertEquals(1, regionMetaData.size());
-        ClientPartitionAdvisor prMetaData = regionMetaData.get(region
-            .getFullPath());
-        assertEquals(metaDataRefreshCount , ((LocalRegion)region).getCachePerfStats().getMetaDataRefreshCount());
-        assertEquals(nonSingleHopsCount , ((LocalRegion)region).getCachePerfStats().getNonSingleHopsCount());
-      }
-      else {
+        regionMetaData.get(region.getFullPath());
+        assertEquals(metaDataRefreshCount, ((LocalRegion) region).getCachePerfStats().getMetaDataRefreshCount());
+        assertEquals(nonSingleHopsCount, ((LocalRegion) region).getCachePerfStats().getNonSingleHopsCount());
+      } else {
         for (int i = 0; i < 226; i++) {
           region.get(new Integer(i));
         }
-        ClientMetadataService cms = ((GemFireCacheImpl)cache)
+        ClientMetadataService cms = ((GemFireCacheImpl) cache)
             .getClientMetadataService();
         Map<String, ClientPartitionAdvisor> regionMetaData = cms
             .getClientPRMetadata_TEST_ONLY();
         assertEquals(1, regionMetaData.size());
-        ClientPartitionAdvisor prMetaData = regionMetaData.get(region
-            .getFullPath());
-        assertEquals(metaDataRefreshCount , ((LocalRegion)region).getCachePerfStats().getMetaDataRefreshCount());
-        assertEquals(nonSingleHopsCount , ((LocalRegion)region).getCachePerfStats().getNonSingleHopsCount());
+        regionMetaData.get(region.getFullPath());
+        assertEquals(metaDataRefreshCount, ((LocalRegion) region).getCachePerfStats().getMetaDataRefreshCount());
+        assertEquals(nonSingleHopsCount, ((LocalRegion) region).getCachePerfStats().getNonSingleHopsCount());
       }
-    }
-    else {
+    } else {
       for (int i = 0; i <= 20; i++) {
         CustId custid = new CustId(i);
         customerRegion.get(custid);
@@ -481,45 +432,42 @@ public class SingleHopStatsDUnitTest extends CacheTestCase {
           }
         }
       }
-      ClientMetadataService cms = ((GemFireCacheImpl)cache)
+      ClientMetadataService cms = ((GemFireCacheImpl) cache)
           .getClientMetadataService();
       Map<String, ClientPartitionAdvisor> regionMetaData = cms
           .getClientPRMetadata_TEST_ONLY();
       assertEquals(3, regionMetaData.size());
       assertTrue(regionMetaData.containsKey(customerRegion.getFullPath()));
-      ClientPartitionAdvisor prMetaData_Customer = regionMetaData
-          .get(customerRegion.getFullPath());
-      assertEquals(metaDataRefreshCount_Customer , ((LocalRegion)customerRegion).getCachePerfStats().getMetaDataRefreshCount());
-      assertEquals(nonSingleHopsCount_Customer , ((LocalRegion)customerRegion).getCachePerfStats().getNonSingleHopsCount());
-
-      ClientPartitionAdvisor prMetaData_Order = regionMetaData
-          .get(orderRegion.getFullPath());
-      assertEquals(metaDataRefreshCount_Order , ((LocalRegion)orderRegion).getCachePerfStats().getMetaDataRefreshCount());
-      assertEquals(nonSingleHopsCount_Order , ((LocalRegion)orderRegion).getCachePerfStats().getNonSingleHopsCount());
-
-      ClientPartitionAdvisor prMetaData_Shipment = regionMetaData
-          .get(shipmentRegion.getFullPath());
-      assertEquals(metaDataRefreshCount_Shipment , ((LocalRegion)shipmentRegion).getCachePerfStats().getMetaDataRefreshCount());
-      assertEquals(nonSingleHopsCount_Shipment , ((LocalRegion)shipmentRegion).getCachePerfStats().getNonSingleHopsCount());
+      regionMetaData.get(customerRegion.getFullPath());
+      assertEquals(metaDataRefreshCount_Customer, ((LocalRegion) customerRegion).getCachePerfStats().getMetaDataRefreshCount());
+      assertEquals(nonSingleHopsCount_Customer, ((LocalRegion) customerRegion).getCachePerfStats().getNonSingleHopsCount());
+
+      regionMetaData.get(orderRegion.getFullPath());
+      assertEquals(metaDataRefreshCount_Order, ((LocalRegion) orderRegion).getCachePerfStats().getMetaDataRefreshCount());
+      assertEquals(nonSingleHopsCount_Order, ((LocalRegion) orderRegion).getCachePerfStats().getNonSingleHopsCount());
+
+      regionMetaData.get(shipmentRegion.getFullPath());
+      assertEquals(metaDataRefreshCount_Shipment, ((LocalRegion) shipmentRegion).getCachePerfStats().getMetaDataRefreshCount());
+      assertEquals(nonSingleHopsCount_Shipment, ((LocalRegion) shipmentRegion).getCachePerfStats().getNonSingleHopsCount());
     }
   }
-  
-  public static void updatePR(String FromClient,
-      String colocation) {
+
+  private void updatePR(String FromClient, String colocation) {
+    Cache cache = CacheFactory.getAnyInstance();
+    Region region = cache.getRegion(Region_Name);
     if (colocation.equals("No_Colocation")) {
       if (FromClient.equals("FirstClient")) {
         for (int i = 0; i < 226; i++) {
           region.put(new Integer(i), "Update" + i);
         }
-        ClientMetadataService cms = ((GemFireCacheImpl)cache)
+        ClientMetadataService cms = ((GemFireCacheImpl) cache)
             .getClientMetadataService();
         Map<String, ClientPartitionAdvisor> regionMetaData = cms
             .getClientPRMetadata_TEST_ONLY();
         assertEquals(1, regionMetaData.size());
-        ClientPartitionAdvisor prMetaData = regionMetaData.get(region
-            .getFullPath());
-        assertEquals(metaDataRefreshCount , ((LocalRegion)region).getCachePerfStats().getMetaDataRefreshCount());
-        assertEquals(nonSingleHopsCount , ((LocalRegion)region).getCachePerfStats().getNonSingleHopsCount());
+        regionMetaData.get(region.getFullPath());
+        assertEquals(metaDataRefreshCount, ((LocalRegion) region).getCachePerfStats().getMetaDataRefreshCount());
+        assertEquals(nonSingleHopsCount, ((LocalRegion) region).getCachePerfStats().getNonSingleHopsCount());
       }
     }
   }


[3/3] incubator-geode git commit: Merge remote-tracking branch 'origin/develop' into feature/GEODE-1376

Posted by kl...@apache.org.
Merge remote-tracking branch 'origin/develop' into feature/GEODE-1376


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/b9b0757f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/b9b0757f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/b9b0757f

Branch: refs/heads/feature/GEODE-1376
Commit: b9b0757fae600851dfd5afa647bda2964ab40253
Parents: 9ea4e07 9a77054
Author: Kirk Lund <kl...@apache.org>
Authored: Tue May 10 14:12:29 2016 -0700
Committer: Kirk Lund <kl...@apache.org>
Committed: Tue May 10 14:12:29 2016 -0700

----------------------------------------------------------------------
 .../internal/security/GeodeSecurityUtil.java    |  69 ++-
 .../security/shiro/JMXShiroAuthenticator.java   |  13 +-
 .../internal/security/shiro/ShiroPrincipal.java |  39 ++
 .../internal/cache/SingleHopStatsDUnitTest.java | 448 ++++++++-----------
 4 files changed, 306 insertions(+), 263 deletions(-)
----------------------------------------------------------------------



[2/3] incubator-geode git commit: GEODE-1373: resolve the shiro subject in JMX AccessControlContext first

Posted by kl...@apache.org.
GEODE-1373: resolve the shiro subject in JMX AccessControlContext first


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/9a770540
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/9a770540
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/9a770540

Branch: refs/heads/feature/GEODE-1376
Commit: 9a770540795691fb16a5ef5fef6dbb64c7e5183d
Parents: bcf7c94
Author: Jinmei Liao <ji...@pivotal.io>
Authored: Mon May 9 21:31:02 2016 -0700
Committer: Jinmei Liao <ji...@pivotal.io>
Committed: Tue May 10 13:06:12 2016 -0700

----------------------------------------------------------------------
 .../internal/security/GeodeSecurityUtil.java    | 69 ++++++++++++++++----
 .../security/shiro/JMXShiroAuthenticator.java   | 13 +++-
 .../internal/security/shiro/ShiroPrincipal.java | 39 +++++++++++
 3 files changed, 108 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/9a770540/geode-core/src/main/java/com/gemstone/gemfire/internal/security/GeodeSecurityUtil.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/security/GeodeSecurityUtil.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/security/GeodeSecurityUtil.java
index 1f1f4eb..6e10f3f 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/security/GeodeSecurityUtil.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/security/GeodeSecurityUtil.java
@@ -17,12 +17,15 @@
 
 package com.gemstone.gemfire.internal.security;
 
+import java.security.AccessController;
+import java.util.Set;
 import java.util.concurrent.Callable;
 
 import com.gemstone.gemfire.cache.operations.OperationContext;
 import com.gemstone.gemfire.cache.operations.OperationContext.OperationCode;
 import com.gemstone.gemfire.cache.operations.OperationContext.Resource;
 import com.gemstone.gemfire.internal.logging.LogService;
+import com.gemstone.gemfire.internal.security.shiro.ShiroPrincipal;
 import com.gemstone.gemfire.management.internal.security.ResourceOperation;
 import com.gemstone.gemfire.management.internal.security.ResourceOperationContext;
 import com.gemstone.gemfire.security.AuthenticationFailedException;
@@ -41,9 +44,15 @@ public class GeodeSecurityUtil {
 
   private static Logger logger = LogService.getLogger();
 
-  public static void login(String username, String password){
-    if(!isShiroConfigured())
-      return;
+  /**
+   *
+   * @param username
+   * @param password
+   * @return null if security is not enabled, otherwise return a shiro subject
+   */
+  public static Subject login(String username, String password){
+    if(!isSecured())
+      return null;
 
     Subject currentUser = SecurityUtils.getSubject();
 
@@ -56,13 +65,52 @@ public class GeodeSecurityUtil {
       logger.info(e.getMessage(), e);
       throw new AuthenticationFailedException(e.getMessage(), e);
     }
+
+    return currentUser;
+  }
+
+  /**
+   * It first looks the shiro subject in AccessControlContext since JMX will use multiple threads to process operations from the same client.
+   * then it looks into Shiro's thead context.
+   *
+   * @return the shiro subject, null if security is not enabled
+   */
+  public static Subject getSubject(){
+    if(!isSecured())
+      return null;
+
+    Subject currentUser = null;
+
+    // First try get the principal out of AccessControlContext instead of Shiro's Thread context
+    // since threads can be shared between JMX clients.
+    javax.security.auth.Subject jmxSubject =
+      javax.security.auth.Subject.getSubject(AccessController.getContext());
+
+    if(jmxSubject!=null){
+      Set<ShiroPrincipal> principals = jmxSubject.getPrincipals(ShiroPrincipal.class);
+      if(principals.size()>0){
+        ShiroPrincipal principal = principals.iterator().next();
+        currentUser = principal.getSubject();
+        ThreadContext.bind(currentUser);
+        return currentUser;
+      }
+    }
+
+    // in other cases like admin rest call or pulse authorization
+    currentUser = SecurityUtils.getSubject();
+
+    if(currentUser==null || currentUser.getPrincipal()==null){
+      throw new GemFireSecurityException("Error: Anonymous User");
+    }
+
+    return currentUser;
   }
 
   public static void logout(){
-    if(!isShiroConfigured())
+    Subject currentUser = getSubject();
+    if(currentUser==null)
       return;
 
-    Subject currentUser = SecurityUtils.getSubject();
     try {
       logger.info("Logging out "+currentUser.getPrincipal());
       currentUser.logout();
@@ -76,10 +124,10 @@ public class GeodeSecurityUtil {
   }
 
   public static Callable associateWith(Callable callable){
-    if(!isShiroConfigured())
+    Subject currentUser = getSubject();
+    if(currentUser==null)
       return callable;
 
-    Subject currentUser = SecurityUtils.getSubject();
     return currentUser.associateWith(callable);
   }
 
@@ -140,11 +188,10 @@ public class GeodeSecurityUtil {
     if(context.getResource()== Resource.NULL && context.getOperationCode()== OperationCode.NULL)
       return;
 
-    if(!isShiroConfigured())
+    Subject currentUser = getSubject();
+    if(currentUser==null)
       return;
 
-
-    Subject currentUser = SecurityUtils.getSubject();
     try {
       currentUser.checkPermission(context);
     }
@@ -154,7 +201,7 @@ public class GeodeSecurityUtil {
     }
   }
 
-  private static boolean isShiroConfigured(){
+  private static boolean isSecured(){
     try{
       SecurityUtils.getSecurityManager();
     }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/9a770540/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/JMXShiroAuthenticator.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/JMXShiroAuthenticator.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/JMXShiroAuthenticator.java
index 4a4cc28..984a2bf 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/JMXShiroAuthenticator.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/JMXShiroAuthenticator.java
@@ -18,6 +18,7 @@ package com.gemstone.gemfire.internal.security.shiro;
 
 import static com.gemstone.gemfire.management.internal.security.ResourceConstants.*;
 
+import java.security.Principal;
 import java.util.Collections;
 import java.util.Properties;
 import javax.management.Notification;
@@ -50,9 +51,17 @@ public class JMXShiroAuthenticator implements JMXAuthenticator, NotificationList
       throw new SecurityException(WRONGE_CREDENTIALS_MESSAGE);
     }
 
-    GeodeSecurityUtil.login(username, password);
+    org.apache.shiro.subject.Subject shiroSubject = GeodeSecurityUtil.login(username, password);
+    Principal principal;
 
-    return new Subject(true, Collections.singleton(new JMXPrincipal(username)), Collections.EMPTY_SET,
+    if(shiroSubject==null){
+      principal = new JMXPrincipal(username);
+    }
+    else{
+      principal = new ShiroPrincipal(shiroSubject);
+    }
+
+    return new Subject(true, Collections.singleton(principal), Collections.EMPTY_SET,
       Collections.EMPTY_SET);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/9a770540/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/ShiroPrincipal.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/ShiroPrincipal.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/ShiroPrincipal.java
new file mode 100644
index 0000000..8413ebb
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/security/shiro/ShiroPrincipal.java
@@ -0,0 +1,39 @@
+/*
+ * 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.security.shiro;
+
+import java.security.Principal;
+
+import org.apache.shiro.subject.Subject;
+
+public class ShiroPrincipal implements Principal {
+  private Subject subject;
+
+  public ShiroPrincipal(Subject subject){
+    this.subject = subject;
+  }
+
+  @Override
+  public String getName() {
+    return subject.toString();
+  }
+
+  public Subject getSubject(){
+    return subject;
+  }
+}